Pages

Saturday, October 8

YACC program to convert infix

%{#include<stdio.h>
#include<ctype.h>
%}
%token num
%left '+''-'
%left '*' '/'
%%
s:e'\n'{}
e:e'+'e{printf("+");}
|e'-'e{printf("-");}
|e'/'e{printf("/");}
|e'*'e{printf("*");}
|num1{printf("%d",$1);}
num1:num1 num {$$=$1*10+$2;}
|num
;
%%
yylex()
{
 int c;
 c=getchar();
if(isdigit(c))
{ yylval=c-'0';
 return num;
}return c;
}
int main()
{
 yyparse();
return 1;
}
int yyerror()
{
return 1;
}
int yywrap()
{
 return 1;
}

9 comments:

  1. thanh you...........

    ReplyDelete
  2. it is very simple program ...n easy 2 understand......all lex n yacc programs r quite easy 2 understand........thank you ........thank you.......

    ReplyDelete
  3. easily understanding code...
    can u please help the code for , syntax analyzer for declaration and definition of user defined function in c... using lex and yacc concept..

    ReplyDelete
  4. Welcome Anonymous.

    Yup.. YACC is easily digesting dish,, ;)

    ReplyDelete
  5. why this line "num1:num1 num {$$=$1*10+$2;}" when it is not necessary?

    ReplyDelete