0% found this document useful (0 votes)
13 views4 pages

Ex 5,6, CD

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)
13 views4 pages

Ex 5,6, CD

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/ 4

s : FOR LPAREN assign SEMICOL cond

SEMICOL assign RPAREN s { printf("FOR


Ex 5: "); }
Lex code | WHILE LPAREN cond RPAREN s {
%{ printf("WHILE "); }
#include "y.tab.h" | DO s WHILE LPAREN cond RPAREN
%} SEMICOL { printf("DO-WHILE "); }
| assign SEMICOL
%% ;
for return FOR;
while return WHILE; assign : id EQ e { printf("= "); }
do return DO; ;
"(" return LPAREN;
")" return RPAREN; cond : val REL val { printf("REL "); }
"+" return PLUS; ;
"-" return MINUS;
"*" return MUL; e : e PLUS t { printf("+ "); }
"/" return DIV; | e MINUS t { printf("- "); }
"=" return EQ; |t
";" return SEMICOL; ;
0|[1-9][0-9]* return NUM;
[a-zA-Z_][a-zA-Z_0-9]* return ID; t : t MUL f { printf("* "); }
\<|\>|\>\=|\<\=|\=\=|\!\= return REL; | t DIV f { printf("/ "); }
[ \t\r\n] {} |f
. return yytext[0]; ;
%%
f : LPAREN e RPAREN
int yywrap() { return 1; } | val
;
Yacc code:
%{ val : id
#include <stdio.h> | NUM { printf("NUM "); }
;
extern int yylex();
void yyerror(const char* s); id : ID { printf("ID "); }
%} ;
%%
%token NUM ID
%token LPAREN RPAREN SEMICOL int main() {
%token FOR WHILE DO if (yyparse()) {
%token REL PLUS MINUS MUL DIV EQ printf("\nInvalid syntax\n");
} else {
%% printf("\nValid syntax\n");
}
return 0;
}

void yyerror(const char* s) {


fprintf(stderr, "Compiler error: %s\n", s);
}
input:
for (i = 0; i < 10; i = i + 1)
if (i % 2 == 0)
printf("even");
Ex 6: %token STR NUM ID
Lex code: %token LPAREN RPAREN COMMA SEMICOL
%{ %token IF ELSE
#include "y.tab.h" %token FOR WHILE DO
%} %token REL PLUS MINUS MUL DIV MOD
EQ
%%
if return IF; %%
else return ELSE; s : IF LPAREN cond RPAREN s {
printf("IF "); }
for return FOR; | IF LPAREN cond RPAREN s ELSE s
while return WHILE; { printf("IF-ELSE "); }
do return DO; | FOR LPAREN assign SEMICOL cond
SEMICOL assign RPAREN s {
"+" return PLUS; printf("FOR "); }
"-" return MINUS; | WHILE LPAREN cond RPAREN s {
"*" return MUL; printf("WHILE "); }
"/" return DIV; | DO s WHILE LPAREN cond RPAREN
"%" return MOD; SEMICOL { printf("DO-WHILE "); }
"=" return EQ;
"(" return LPAREN; | id LPAREN arg RPAREN SEMICOL {
")" return RPAREN; printf("CALL "); }
"," return COMMA; | assign SEMICOL
";" return SEMICOL; ;

\".*\" return STR; arg :


0|[1-9][0-9]* return NUM; | val COMMA arg
[a-zA-Z_][a-zA-Z_0-9]* return ID; | val
;
\<|\>|\>\=|\<\=|\=\=|\!\= return
REL; assign : id EQ e { printf("= "); }
;
[ \t\r\n] {}
cond : e REL e { printf("REL "); }
. return yytext[0]; ;
%%
e : e PLUS t { printf("+ "); }
int yywrap() { return 1; } | e MINUS t { printf("- "); }
| t
Yacc code: ;
%{
#include <stdio.h> t : t MUL f { printf("* "); }
| t DIV f { printf("/ "); }
extern int yylex(); | t MOD f { printf("%% "); }
void yyerror(const char* s); | f
%} ;
f : LPAREN e RPAREN
| val
;

val : id
| STR { printf("STR "); }
| NUM { printf("NUM "); }
;

id : ID { printf("ID "); }
;
%%

int main() {
if (yyparse()) {
printf("\nInvalid
syntax\n");
} else {
printf("\nValid
syntax\n");

}
return 0;
}

void yyerror(const char* s) {


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

Input:
for (i = 0; i < 5; i = i + 1) sum
= sum + i;

You might also like