%{#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;
}
thanh you...........
ReplyDeleteit is very simple program ...n easy 2 understand......all lex n yacc programs r quite easy 2 understand........thank you ........thank you.......
ReplyDeleteeasily understanding code...
ReplyDeletecan 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???
ReplyDeleteWelcome Anonymous
ReplyDeleteWelcome Anonymous.
ReplyDeleteYup.. YACC is easily digesting dish,, ;)
Sure Anonymous.
ReplyDeleteI will try to update it..!!
why this line "num1:num1 num {$$=$1*10+$2;}" when it is not necessary?
ReplyDeleteOutput please.
ReplyDelete