Program:
(sw.l)
alpha [A-Za-z]
digit [0-9]
%%
[ \t\n]
switch return SWITCH;
case return CASE;
break return BREAK;
default return DEFAULT;
{digit}+ return NUM;
{alpha}({alpha}|{digit})* return ID;
"<=" return LE;
">=" return GE;
"==" return EQ;
"!=" return NE;
"||" return OR;
"&&" return AND;
. return yytext[0];
%%
(sw.y)
%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM SWITCH CASE DEFAULT BREAK LE GE EQ NE OR AND ELSE
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%left '+''-'
%left '*''/'
%right UMINUS
%left '!'
%%
S : ST {printf("Input accepted.\n");exit(0);}
;
ST : SWITCH '(' ID ')' '{' B '}'
;
B : C
| C D
;
C : C C
| CASE NUM ':' E ';'
| BREAK ';'
;
D : DEFAULT ':' E ';' BREAK ';'
;
E : ID'='E
| E'+'E
| E'-'E
| E'*'E
| E'/'E
| E'<'E
| E'>'E
| E LE E
| E GE E
| E EQ E
| E NE E
| E OR E
| E AND E
| ID
| NUM
;
%%
#include "lex.yy.c"
main()
{
printf("Enter the exp: ");
yyparse();
}
Output:
students@cselab-desktop:~$ lex sw.l
students@cselab-desktop:~$ yacc sw.y
conflicts: 2 shift/reduce
students@cselab-desktop:~$ gcc y.tab.c -ll -ly
students@cselab-desktop:~$ ./a.out
Enter the exp: switch(a)
{
case 1: a=1;
break;
case 2: a=2;
break;
default:a=0;
break;
}
Input accepted.
students@cselab-desktop:~$
Wrong Grammar
ReplyDeleteGreat and Useful Article.
ReplyDeleteJava Online Course
Java Online Training
Java EE training
Java Course in Chennai
Java Training in Chennai
Java Training Institutes in Chennai
Java Interview Questions
Java Interview Questions
code not working wrong program
ReplyDelete