Parser for SWITCH Statements - YACC Program - Compiler Design



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:~$

5 comments:

  1. code not working wrong program

    ReplyDelete
  2. ya not working is a bad very bad unusefull program

    ReplyDelete
  3. it is giving following errors when run on windows
    sw.l:5:9: note: each undeclared identifier is reported only once for each function it appears in
    sw.l:6:9: error: 'CASE' undeclared (first use in this function)
    case {return CASE;}
    ^
    sw.l:7:8: error: 'BREAK' undeclared (first use in this function)
    break return BREAK;
    ^
    sw.l:8:8: error: 'DEFAULT' undeclared (first use in this function)
    default return DEFAULT;
    ^
    sw.l:9:8: error: 'NUM' undeclared (first use in this function)
    {digit}+ return NUM;
    ^
    sw.l:10:8: error: 'ID' undeclared (first use in this function)
    {alpha}({alpha}|{digit})* return ID;
    ^
    sw.l:11:8: error: 'LE' undeclared (first use in this function)
    "<=" return LE;
    ^
    sw.l:12:8: error: 'GE' undeclared (first use in this function)
    ">=" return GE;
    ^
    sw.l:13:8: error: 'EQ' undeclared (first use in this function)
    "==" return EQ;
    ^
    sw.l:14:8: error: 'NE' undeclared (first use in this function)
    "!=" return NE;
    ^
    sw.l:15:8: error: 'OR' undeclared (first use in this function)
    "||" return OR;
    ^
    sw.l:16:8: error: 'AND' undeclared (first use in this function)
    "&&" return AND;
    ^

    C:\Users\SONA\Desktop>flex sw.l

    C:\Users\SONA\Desktop>bison -dy sw.y
    conflicts: 2 shift/reduce

    C:\Users\SONA\Desktop>gcc lex.yy.c y.tab.c -o lexyacc
    sw.l: In function 'yylex':
    sw.l:5:9: error: 'SWITCH' undeclared (first use in this function)
    switch {return SWITCH;}
    ^
    sw.l:5:9: note: each undeclared identifier is reported only once for each function it appears in
    sw.l:6:9: error: 'CASE' undeclared (first use in this function)
    case {return CASE;}
    ^
    sw.l:7:8: error: 'BREAK' undeclared (first use in this function)
    break return BREAK;
    ^
    sw.l:8:8: error: 'DEFAULT' undeclared (first use in this function)
    default return DEFAULT;
    ^
    sw.l:9:8: error: 'NUM' undeclared (first use in this function)
    {digit}+ return NUM;
    ^
    sw.l:10:8: error: 'ID' undeclared (first use in this function)
    {alpha}({alpha}|{digit})* return ID;
    ^
    sw.l:11:8: error: 'LE' undeclared (first use in this function)
    "<=" return LE;
    ^
    sw.l:12:8: error: 'GE' undeclared (first use in this function)
    ">=" return GE;
    ^
    sw.l:13:8: error: 'EQ' undeclared (first use in this function)
    "==" return EQ;
    ^
    sw.l:14:8: error: 'NE' undeclared (first use in this function)
    "!=" return NE;
    ^
    sw.l:15:8: error: 'OR' undeclared (first use in this function)
    "||" return OR;
    ^
    sw.l:16:8: error: 'AND' undeclared (first use in this function)
    "&&" return AND;

    ReplyDelete
  4. im getting answer. thank u so much

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...