CD Lab
CD Lab
SCIENCES, JABALPUR
PRACTICAL FILE
COMPILER DESIGN
(IS-702(B))
SESSION: 2023-24
SUBMITTED BY :....................
1
COMPILER DESIGN CB-502
INDEX
Sr. No. Title Date Sign
● Program code:-
Output Terminal: -
COMPILER DESIGN CB-502
EXPERIMENT 02
● Program code:-
%%
([a-zA-Z0-9])* {i++;}
"\n" {printf(" Number of words: %d\n", i); i = 0;}
%%
int yywrap(void){}
int main()
{
printf("Enter the String: ");
yylex();
return 0;
}
● Output Terminal: -
EXPERIMENT 03
● Program code:-
%{
#include<stdio.h>
%}
%option noyywrap
%%
^[a-z][a-z0-9_]*(@[A-Za-z]+)(\.[a-z]+)+ {printf("valid");}
.* {printf("invalid");}
%%
int main()
{
printf("Enter email:");
yylex();
return 0;
}
● Output Terminal:-
COMPILER DESIGN CB-502
EXPERIMENT 04
%%
[aAeEiIoOuU] { num_vowels++; }
[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ] { num_consonants++; }
[ \t\n];
.;
%%
int yywrap() {
return 1;
}
int main() {
char input[1000];
printf("Enter a string: ");
fgets(input, sizeof(input), stdin);
yy_scan_string(input);
yylex();
EXPERIMENT 05
EXPERIMENT 06
%}
%%
[0-9]+ {
yylval=atoi(yytext);
return NUMBER;
}
[\t] ;
[\n] return 0;
. return yytext[0];
%%
int yywrap()
{
return 1;
}
● Output Terminal: -
%{
#include<stdio.h>
int flag=0;
%}
%token NUMBER
%left '+' '-'
%left '*' '/' '%'
%left '(' ')'
%%
ArithmeticExpression: E{
printf("\nResult=%d\n", $$);
return 0;
};
E:E'+'E {$$=$1+$3;}
|E'-'E {$$=$1-$3;}
|E'*'E {$$=$1*$3;}
|E'/'E {$$=$1/$3;}
|E'%'E {$$=$1%$3;}
|'('E')' {$$=$2;}
|NUMBER {$$=$1;};
%%
void main()
{
printf("\nEnter Any Arithmetic Expression which can have operations Addition,
Subtraction, Multiplication, Division, Modulus and Round brackets:\n");
yyparse();
if(flag==0)
printf("\nEntered arithmetic expression is Valid\n\n");
}
void yyerror()
{
printf("\nEntered arithmetic expression is Invalid\n\n");
flag=1;
}
COMPILER DESIGN CB-502
EXPERIMENT 07
%%
[0-9] { yylval = *yytext - '0'; return NUM; }
[+*()$] { return *yytext; }
[ \t] ;
\n { return *yytext; }
. { printf("Invalid character %c\n", *yytext); }
%%
int yywrap() {
return 1;
}
● Output Terminal:-
File Name: ll1.y
%{
#include <stdio.h>
int yylex();
void yyerror(char *);
int size[5][6];
char m[5][6][3];
char s[30], stack[20];
int top = 0;
%}
%token NUM
%%
main: expr '$' { printf("SUCCESS\n"); }
;
expr: term expr1 {;}
;
expr1: '+' term expr1 {;}
|{;}
;
term: factor term1 {;}
;
term1: '*' factor term1 {;}
|{;}
;
factor: '(' expr ')' {;}
|NUM {;}
;
%%
void yyerror(char *s) {
}
int main() {
printf("Enter the input string: ");
yyparse();
return 0;
}
COMPILER DESIGN CB-502
EXPERIMENT 08
● Program code:-
%%
"." { return yytext[0]; }
"0" { yylval.n=0; return ZERO; }
"1" { yylval.n=1; return ONE; }
\n { return 0; }
. {;}
%%
int yywrap(){
return 1;
}
File Name: unitconvert.y
%{
#include<stdio.h>
void yyerror(char *);
int yylex();
int m1 = 0;
int m2 = 1;
%}
%token ONE
%token ZERO
%union {
float f;
int n;
}
%type <f>E
%type <n>A
%type <f>B
%type <n>ONE
%type <n>ZERO
%%
S: E {printf("ans: %f\n", $1);};
E: A {$$=$1;}
|A'.'B {
$$=$1+$3;
printf("decimal part: %d\n", $1);
printf("fractional part: %f\n", $3);
};
A: {$$ = 0;}
| ONE A { $$ =(1 << m1 ) | $2; m1++; };
| ZERO A { $$ = $2; m1++;};
;
B: {$$ = 0;}
| B ONE { $$ = $1 +1.0/(1 << m2); m2++;}
| B ZERO { $$=$1 ; m2++;}
;
%%
void yyerror(char *s){
printf("ERROR %s .\n", s); }
int main(){
printf("Enter binary number: ");
return yyparse();
}
COMPILER DESIGN CB-502
Output Terminal:-
EXPERIMENT 09
● Program code:-
● Output Terminal:-
COMPILER DESIGN CB-502
EXPERIMENT 10