Expression Evaluation YACC S6

%{
#include<stdio.h>
%}
%token DIGIT
%%
S    : E '\n' {printf("%d\n",$1);return 1;}
    ;
E    : E '+'    T    { $$ = $1 + $3 ; }
    | T
    ;
T    : T '*'    F    { $$ = $1 * $3; }
    | F
    ;
F    : '(' E    ')'    { $$=$2; }
    | DIGIT
    ;
%%
yylex(){
int c;
c=getchar();
if(isdigit(c))
{
    yylval=c-'0';
    return DIGIT;
}

return c;
}
main()
{
printf("Enter the exp:");
yyparse();   
return 0;
}

Output:
students@ccflab-desktop:~$ yacc aa.y
students@ccflab-desktop:~$ gcc y.tab.c -ly
students@ccflab-desktop:~$ ./a.out
Enter the exp:2+5*5
27
students@ccflab-desktop:~$

3 comments:

  1. /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/liby.a(yyerror.o): In function `yyerror':
    (.text+0x1c): undefined reference to `rpl_fprintf'
    collect2: ld returned 1 exit status

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...