Program:
// Lex file: pos.l
DIGIT [0-9]
%%
{DIGIT}+ {yylval=atoi(yytext);return ID;}
[-+*/] {return yytext[0];}
. ;
\n yyterminate();
// Yacc File: pos.y
%{
#include<stdio.h>
#include<assert.h>
void push(int val);
%}
%token ID
%%
S : E {printf("= %d\n",top());}
;
E : E E '+' {push(pop()+pop());}
| E E '-' {int temp=pop();push(pop()-temp);}
| E E '*' {push(pop()*pop());}
| E E '/' {int temp=pop();push(pop()/temp);}
| ID {push(yylval);}
;
%%
#include"lex.yy.c"
int st[100];
int i=0;
void push(int val)
{
assert(i<100);
st[i++]=val;
}
int pop()
{
assert(i>0);
return st[--i];
}
int top()
{
assert(i>0);
return st[i-1];
}
int main()
{
yyparse();
return 0;
}
Output:
nn@linuxmint ~ $ lex pos.l
nn@linuxmint ~ $ yacc pos.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
5 5 -
= 0
nn@linuxmint ~ $
// Lex file: pos.l
DIGIT [0-9]
%%
{DIGIT}+ {yylval=atoi(yytext);return ID;}
[-+*/] {return yytext[0];}
. ;
\n yyterminate();
// Yacc File: pos.y
%{
#include<stdio.h>
#include<assert.h>
void push(int val);
%}
%token ID
%%
S : E {printf("= %d\n",top());}
;
E : E E '+' {push(pop()+pop());}
| E E '-' {int temp=pop();push(pop()-temp);}
| E E '*' {push(pop()*pop());}
| E E '/' {int temp=pop();push(pop()/temp);}
| ID {push(yylval);}
;
%%
#include"lex.yy.c"
int st[100];
int i=0;
void push(int val)
{
assert(i<100);
st[i++]=val;
}
int pop()
{
assert(i>0);
return st[--i];
}
int top()
{
assert(i>0);
return st[i-1];
}
int main()
{
yyparse();
return 0;
}
Output:
nn@linuxmint ~ $ lex pos.l
nn@linuxmint ~ $ yacc pos.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
5 5 -
= 0
nn@linuxmint ~ $
i was waitin' for this type of article and i have gained some useful information from this site. thanks for sharing this information.
ReplyDeletewww.n8fan.net
useless program
ReplyDeleteFOR LEX PROGRAM WAS INCOMPLETE.
ReplyDelete