Sharecodepoint popular queries

Saturday, 23 February 2019

Program to recognize nested IF control statements and display the levels of nesting . | Compiler Design Lab Programs | Yacc Programs


Yacc Part :

%token IF RELOP S NUMBER ID
%{
 int count=0;
%}
%%
stmt : if_stmt { printf(“No of nested if statements=%d\n”,count); exit(0);}
 ;
if_stmt : IF ‘(‘ cond ‘)’ if_stmt {count++;}
 | S;
 ;
cond : x RELOP x
 ;
x : ID
 | NUMBER
 ;
%%
int yyerror(char *msg)
{
 printf(“Invalid Expression\n”);
 exit(0);
}
main ()
{
 printf(“Enter the statement”);
 yyparse();


Lex Part :

%{
 #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]; }
%%

0 comments:

Post a comment

 

Version 23.2.3.4 - 2016 - 2020 - © Sharecodepoint | Made in India. | Copyright Content | Facebook | Twitter | Instagram | Youtube