Program:-"Beta version"-(Partial Output Only)
// Lex file: ptoi.l
DIGIT [0-9]
%%
{DIGIT}+ {yylval=atoi(yytext);return ID;}
[-+*/] {return yytext[0];}
. ;
\n yyterminate();
// Yacc file: ptoi.y
%{
#include<stdio.h>
#include<string.h>
void push();
char* top();
void a1(char* a);
%}
%token ID
%%
S : E { printf("= %s \n",top());}
;
E : E E '+' {a1(" + ");}
| E E '*' {a1(" * ");}
| E E '-' {a1(" - ");}
| E E '/' {a1(" / ");}
| ID {push();}
;
%%
#include"lex.yy.c"
char st[100][10];
int indx=0;
void push()
{
strcpy(st[indx++],yytext);
}
char* pop()
{
return st[--indx];
}
char* top()
{
return st[indx-1];
}
void a1(char* a)
{
char buffer[20];
char* c1=pop();
char* c2=pop();
bzero(buffer,20);
strcat(buffer,c2);
strcat(buffer,a);
strcat(buffer,c1);
strcpy(st[indx++],buffer);
}
main()
{
yyparse();
}
Output:
nn@linuxmint ~ $ lex ptoi.l
nn@linuxmint ~ $ yacc ptoi2.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
2 5 * 3 2 * +
= 2 * 5 + 3 * 2
nn@linuxmint ~ $ ./a.out
2 5 +
= 2 + 5
nn@linuxmint ~ $ ./a.out
2 3 *
= 2 * 3
nn@linuxmint ~ $ ./a.out
2 3 -
= 2 - 3
nn@linuxmint ~ $ ./a.out
2 5 /
= 2 / 5
nn@linuxmint ~ $
0 comments:
Post a Comment