Professional Documents
Culture Documents
21BAI1391 Complier Design Lab 4
21BAI1391 Complier Design Lab 4
21BAI1391 Complier Design Lab 4
Compiler Design
Lab- 5
Code:
Lex Program: (sample.l)
%{
#include<stdio.h>
#include "y.tab.h"
%}
%%
[\t] ;
[\n] return 0;
. return yytext[0];
%%
int yywrap()
{
return 1;
%{
#include<stdio.h>
%}
%token NUMBER
%token VARIABLE
%%
S: VARIABLE'='E {
return 0;
E:E'+'E
|E'-'E
|E'*'E
|E'/'E
|E'%'E
|'('E')'
| NUMBER
| VARIABLE
;
%%
void main()
yyparse();
void yyerror()
}
OUTPUT:
Code:
Yacc program :
Lex program :
%{
#include “y.tab.h”
%}
%%
[a-zA-Z] { return LETTER ;}
[0-9] { return DIGIT ; }
[\n] { return NL ;}
[_] { return UND; }
. { return yytext[0]; }
%%
Output:
Q3. To write a YAAC program to recognize a valid control structures
syntax of c language
Code:
Yacc program :
%{
#include “y.tab.h”
%}
%%
“if” { return IF; }
[sS][0-9]* {return S;}
“<”|”>”|”==”|”!=”|”<=”|”>=” { return RELOP; }
[0-9]+ { return NUMBER; }
[a-zA-Z][a-zA-Z0-9_]* { return ID; }
\n { ; }
. { return yytext[0]; }
%%
Output: