%{#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;
}
#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 :
thanh you...........
it is very simple program ...n easy 2 understand......all lex n yacc programs r quite easy 2 understand........thank you ........thank you.......
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..
output plz???
Welcome Anonymous
Welcome Anonymous.
Yup.. YACC is easily digesting dish,, ;)
Sure Anonymous.
I will try to update it..!!
why this line "num1:num1 num {$$=$1*10+$2;}" when it is not necessary?
Output please.
Post a Comment