0% found this document useful (0 votes)
28 views23 pages

CD Lab 1

Compiler designing lab file
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views23 pages

CD Lab 1

Compiler designing lab file
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

1.

Write a lex program to check whether a given input is pre-increment or post


increment of the string.

%{
#include <stdio.h>
%}

%%
"++"[a-zA-Z_][a-zA-Z0-9_]* { printf("Pre-increment of the string\n"); }
[a-zA-Z_][a-zA-Z0-9_]*"++" { printf("Post-increment of the string\n"); }
. ;
%%

int main(void)
{
printf("Enter a string: ");
yylex();
return 0;
}

int yywrap(void)
{
return 1;
}

2. Write a lex program of a number which is divisible by 5.

%{
#include <stdio.h>
#include <stdlib.h>
%}

%%
[0-9]+ {
int number = atoi(yytext);
if (number % 5 == 0) {
printf("Number %d is divisible by 5\n", number);
} else {
printf("Number %d is not divisible by 5\n", number);
}
}
[ \t\n]+ ;
. ;
%%
int main(void)
{
printf("Enter a number: ");
yylex();
return 0;
}

int yywrap(void)
{
return 1;
}

3. Write a lex program to delete comments from a given source program.

%{
#include <stdio.h>
%}

%%
"//".* { /* Ignore single-line comments */ }
"/*"([^*]|(\*+[^*/]))*"*/" { /* Ignore multi-line comments */ }
.|\n { putchar(yytext[0]); }
%%

int main(void)
{
printf("Enter your source code :\n");
yylex();
return 0;
}

int yywrap(void)
{
return 1;
}

4. Write a lex program to find the length of word or string.

%{
#include <stdio.h>
#include <string.h>
%}
%%
[a-zA-Z]+ { printf("Word: %s, Length: %lu\n", yytext, strlen(yytext)); }
\"[^\"]*\" { printf("String: %s, Length: %lu\n", yytext, strlen(yytext) - 2); }
[ \t\n]+ ;
. ;
%%
int main(void)
{
printf("Enter text :\n");
yylex();
return 0;
}
int yywrap(void)
{
return 1;
}

5. Write a lex program to count the number of characters, words, and lines in a file.

%{
#include <stdio.h>
#include <stdlib.h>
%}

%%
[+-]?0+ { printf("Number %s is zero\n", yytext); }
[+]?[1-9][0-9]* { printf("Number %s is positive\n", yytext); }
[-][1-9][0-9]* { printf("Number %s is negative\n", yytext); }
[ \t\n]+ ;
. { printf("Invalid input: %s\n", yytext); }
%%

int main(void)
{
printf("Enter numbers :\n");
yylex();
return 0;
}

int yywrap(void)
{
return 1;
}

6. Write a lex program to implement a simple calculator.

%{
#include<stdio.h>
int a,b,c;
%}
%%
"+" {printf("Enter the numbers : ");scanf("%d %d",&a,&b);c=a+b; printf("%d\n",c);}
"-" {printf("Enter the numbers : ");scanf("%d %d",&a,&b);c=a-b;printf("%d\n",c);}
"*" {printf("Enter the numbers : ");scanf("%d %d",&a,&b);c=a*b;printf("%d\n",c);}
"%" {printf("Enter the numbers : ");scanf("%d %d",&a,&b);c=a%b;printf("%d\n",c);}
"/" {printf("Enter the numbers : ");scanf("%d %d",&a,&b);c=a/b;printf("%d\n",c);}
. ;
%%
main(){

printf("Enter the operation : ");


yylex();

}
int yywrap(){
return 1;
}

7. Write a lex program for identifiers.

%{
#include <stdio.h>
%}

%%
[a-zA-Z_][a-zA-Z0-9_]* { printf("Identifier: %s\n", yytext); }
[ \t\n]+ ;
. ;
%%
int main(void)
{
printf("Enter Text :\n");
yylex();
return 0;
}

int yywrap(void)
{
return 1;
}

8. Write a lex program to recognize Arithmetic Expression.

%{
#include<stdio.h>
%}
%%
[a-z][/=][a-z0-9]([-+*%][0-9a-z])* {printf("\nArthematicExpression");}
.* {printf("\n Not an ArthematicExpression");}
%%
main(){
yylex();
}
int yywrap(){
return 1;
}

9. Write a lex program to check the given input is either keyword, number, identifier, or
special or any other token.

%{
#include <stdio.h>
#include <string.h>
%}
%option noyywrap
%%
int|float|char|void { printf("Keyword: %s\n", yytext); }
[0-9]+(\.[0-9]+)? { printf("Number: %s\n", yytext); }
[a-zA-Z_][a-zA-Z0-9_]* { printf("Identifier: %s\n", yytext); }
[(){}\[\],;] { printf("Special Character: %s\n", yytext); }
{[-+*/%^=<>!&|?:] { printf("Operator: %s\n", yytext); }
%%
int main() {
yylex();
return 0;
}
10. Write a yacc program to accept given grammar S->a.

Yacc Program :

%{
#include <stdio.h>
#include <stdlib.h>

void yyerror(const char *s);


int yylex(void);
%}

%token A

%%
S:
A { printf("Accepted\n"); }
;

%%

int main(void)
{
printf("Enter a string: ");
yyparse();
return 0;
}

void yyerror(const char *s)


{
fprintf(stderr, "Error: %s\n", s);
}
Lex Program :

%{
#include "y.tab.h"
%}

%%
a { return A; }
\n { return 0; }
. { return 0; }
(aa)+ { return 0; }

%%

int yywrap(void)
{
return 1;
}

11. Write a yacc program to accept given grammar S->CC, C->Cc|d.

Yacc Program :

%{
#include <stdio.h>
#include <stdlib.h>

void yyerror(const char *s);


int yylex(void);
%}

%token C_TOKEN D_TOKEN

%%
S:
C C { printf("Accepted\n"); }
;

C:
C C_TOKEN
| D_TOKEN
;

%%
int main(void)
{
printf("Enter a string: ");
yyparse();
return 0;
}

void yyerror(const char *s)


{
fprintf(stderr, "Error: %s\n", s);
}

Lex Program :

%{
#include "y.tab.h"
%}

%%
c { return C_TOKEN; }
d { return D_TOKEN; }
\n { return 0; }
. { return 0; }
(ddd)+ { return 0; }
%%

int yywrap(void)
{
return 1;
}

12. Write a yacc program to accept given grammer s->E+E|E*E|E/E|id.

Yacc Program :

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int yylex(void);
void yyerror(const char *s);
extern int yylval;
extern char yylval_str[];

%}

%token NUMBER ID
%left '+' '-'
%left '*' '/'

%%

S:E { printf("Accepted\n"); }
;

E : E '+' E
| E '-' E
| E '*' E
| E '/'
| ID { printf("Identifier: %s\n", yylval_str); }
| NUMBER { printf("Number: %d\n", yylval); }
;
%%

int main(void) {
yyparse();
return 0;
}

void yyerror(const char *s) {


fprintf(stderr, "Error: %s\n", s);
}

Lex Program :

%{
#include "y.tab.h"
%}

%%
[0-9]+ { yylval = atoi(yytext); return NUMBER; }
[a-zA-Z]+ { strcpy(yylval_str, yytext); return ID; }
[-+*/()] { return yytext[0]; }
[ \t\n] ;
. { printf("Invalid character: %s\n", yytext); }
%%

int yywrap(void) {
return 1;
}

13. Write a yacc program to accept the given language L= {a(n)/n>=1}.

%{
#include <stdio.h>
%}

%%
input : 'a' { printf("Accepted\n"); }
| input 'a' { printf("Accepted\n"); }
;

%%

int main() {
yyparse();
return 0;
}

void yyerror(const char *s) {


fprintf(stderr, "Error: %s\n", s);
}

14. Write a yacc program to accept the Octal numbers.

Yacc Program :

%{
#include <stdio.h>
#include <math.h>
%}

%token OCTAL

%%

input : OCTAL { printf("Accepted: %s\n", $1); }


;
%%

int main() {
yyparse();
return 0;
}

int yywrap(void) {
return 1;
}

int yyerror(const char *s) {


fprintf(stderr, "Error: %s\n", s);
return 0;
}

Lex Program :

%{
#include "y.tab.h"
%}

%%

[0-7]+ { yylval = atoi(yytext); return OCTAL; }


. ;

%%

int yywrap(void) {
return 1;
}

15. Write a yacc program to accept Palindrome string.

Yacc Program :

%{
#include <stdio.h>
#include <stdlib.h>
%}

%token CHAR
%token PALINDROME
%%

input : PALINDROME { printf("Accepted\n"); }


;

PALINDROME : CHAR
| CHAR PALINDROME CHAR
;

%%

int main() {
yyparse();
return 0;
}

int yyerror(const char *s) {


fprintf(stderr, "Error: %s\n", s);
return 0;
}

Lex Program :

%{
#include "y.tab.h"
%}

%%
[a-zA-Z0-9_] { yylval = *yytext; return CHAR; }
. ;
%%

int yywrap(void) {
return 1;
}

16. Write a yacc program for sentence validity.

%{
#include<stdio.h>
%}
%token letter
%%
start:letter S;
S:letter S|letter;
%%
int yylex(){
char x;
x=getchar();
if(x=='\n')
return 0;
else
return letter;
}
main(){
yyparse();
printf("valid sentence");
return 1;
}
int yyerror(){
printf("invalid");
exit(1);
}
17. Write a yacc program to recognize nested if control statements and display the levels
of nesting.

Yacc Program :

%{
#include <stdio.h>
#include <stdlib.h>

void yyerror(const char *s);


int yylex(void);

int nesting_level = 0;
int max_nesting_level = 0;

void increase_nesting_level() {
nesting_level++;
if (nesting_level > max_nesting_level) {
max_nesting_level = nesting_level;
}
}

void decrease_nesting_level() {
nesting_level--;
}
%}

%token IF ELSE

%%
program:
statements { printf("Max nesting level: %d\n", max_nesting_level); }
;

statements:
/* empty */
| statements statement
;

statement:
IF condition statement { increase_nesting_level(); decrease_nesting_level(); }
| IF condition statement ELSE statement { increase_nesting_level();
decrease_nesting_level(); }
| '{' statements '}'
;

condition:
/* empty */
;

%%

int main(void)
{
printf("Enter a sequence of nested if statements:\n");
yyparse();
return 0;
}

void yyerror(const char *s)


{
fprintf(stderr, "Error: %s\n", s);
}

Lex Program :

%{
#include "y.tab.h"
%}

%%
if { return IF; }
else { return ELSE; }
"{" { return '{'; }
"}" { return '}'; }
[ \t\n] { /* ignore whitespace */ }
. { /* ignore any other characters */ }
%%

int yywrap(void)
{
return 1;
}

18. write a yacc program the following regular expressions balanced parenthesis.

Yacc Program :

%{
#include <stdio.h>
#include <stdlib.h>

void yyerror(const char *s);


int yylex(void);
%}

%token OPEN CLOSE

%%
start:
balanced { printf("Input is balanced\n"); }
;

balanced:
/* empty */
| balanced OPEN balanced CLOSE
;

%%

int main(void)
{
printf("Enter a string: ");
yyparse();
return 0;
}

void yyerror(const char *s)


{
fprintf(stderr, "Error: %s\n", s);
}

Lex Program :

%{
#include "y.tab.h"
%}

%%
\( { return OPEN; }
\) { return CLOSE; }
\n { return 0; }
. { return 0; }
%%

int yywrap(void)
{
return 1;
}

19. Write a yacc program to convert given binary number to decimal number.

Yacc Program :
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void yyerror(const char *s);
int yylex(void);
int decimal_value;
%}
%token DIGIT
%%
start:
binary_number { printf("Decimal: %d\n", decimal_value); }
;
binary_number:
DIGIT { decimal_value = $1; }
| binary_number DIGIT { decimal_value = decimal_value * 2 + $2; }
;
%%
int main(void)
{
printf("Enter a binary number: ");
yyparse();
return 0;
}
void yyerror(const char *s)
{
fprintf(stderr, "Error: %s\n", s);
}
int char_to_digit(char c)
{
return c - '0';
}
Lex Program :
%{
#include "y.tab.h"
int char_to_digit(char c);
%}
%%
[01] { yylval = char_to_digit(yytext[0]); return DIGIT; }
\n { return 0; }
. { return 0; }
%%
int yywrap(void)
{
return 1;
}

20. Write a yacc program for identifier.

Yacc Program :
%{
#include <stdio.h>
#include <stdlib.h>
void yyerror(const char *s);
int yylex(void);
extern char *yytext;
%}
%token IDENTIFIER
%%
start:
IDENTIFIER { printf("Identifier: %s\n", yytext); }
;
%%
int main(void)
{
printf("Enter identifiers: ");
yyparse();
return 0;
}
void yyerror(const char *s)
{
fprintf(stderr, "Error: %s\n", s);
}

Lex Program :
%{
#include "y.tab.h"
%}
%%
[a-zA-Z_][a-zA-Z0-9_]* { return IDENTIFIER; }
\n { return 0; }
. { return 0; }
%%
int yywrap(void)
{
return 1;
}
21. Write a yacc program to implement calculator.

%{
#include <stdio.h>
#include <stdlib.h>
%}
%token NUMBER
%left '+' '-'
%left '*' '/'
%%
input: expr '\n' { printf("Result: %d\n", $1); }
;
expr: expr '+' expr
| expr '-' expr
| expr '*' expr
| expr '/' expr
| '(' expr ')'
| NUMBER
;
%%
int main() {
yyparse();
return 0;
}
int yyerror(const char *s) {
fprintf(stderr, "Error: %s\n", s);
return 0;
}
int yylex(void) {
int c = getchar();
if (c == '\n' || c == EOF) return 0;
if (isdigit(c)) {
ungetc(c, stdin);
scanf("%d", &yylval);
return NUMBER;
}
return c;
}

22. Write a lex program to find the number of vowels and consonants from given string.

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
%}
%option noyywrap
%%
[01]+[Bb] { printf("Binary\n"); }
[0-7]+[Oo] { printf("Octal\n"); }
[0-9]+ { printf("Decimal\n"); }
0[xX][0-9a-fA-F]+ { printf("Hexadecimal\n"); }
. { printf("Not recognized\n"); }
%%
int main() {
yylex();
return 0; }
23. Write a lex program to convert the given binary number into decimal number.

%{
#include <stdio.h>
#include <math.h>
%}
%%
[01]+ {
int binary = atoi(yytext);
int decimal = 0;
int power = 0;
while (binary > 0) {
int digit = binary % 10;
decimal += digit * pow(2, power);
binary /= 10;
power++;
}
printf("Decimal: %d\n", decimal);
}
. ;
%%
int main() {
yylex();
return 0;
}
24. Write a lex program to check whether a given input is identifier or not.

%{
#include <stdio.h>
%}
%%
^[a - z A - Z _][a - z A - Z 0 - 9 _] * { printf("Valid Identifier\n"); }
^[^a - z A - Z _] { printf("Invalid Identifier\n"); }
. ;
%%
int main(void) {
printf("Enter text: ");
yylex();
return 0;
}
int yywrap(void) {
return 1;
}

You might also like