%{
%}
dig [0-9]+
%%
{dig} {
yylval.no = atoi(yytext);
return DIGIT;
}
[a-zA-Z][a-zA-Z0-9]* {
strcpy(yylval.str,yytext);
return ID;
}
"+" {return *yytext;}
"-" {return *yytext;}
"*" {return *yytext;}
"/" {return *yytext;}
"^" {return *yytext;}
"(" {return *yytext;}
")" {return *yytext;}
"\n" {return 0;}
%%
int yywrap()
{
return 1;
}
YACC PROGRAM
%{
#include
#include
#include
#include
%}
%union
{
int no;
char str[10];
}
%token DIGIT
%token ID
%left '+''-'
%left '*''/'
%right '^'
%left '(' ')'
%%
STMT:EXPR {printf("\n");}
;
EXPR:EXPR'+'EXPR { printf("+");}
|EXPR'-'EXPR { printf("-");}
|EXPR'*'EXPR { printf("*");}
|EXPR'/'EXPR { printf("/");}
|EXPR'^'EXPR { printf("^");}
|'('EXPR')'
|DIGIT { printf("\n%d",yylval.no);}
|ID { printf("\n%s",yylval.str);}
;
%%
yyerror()
{
printf("\n error");
}
#include "lex.yy.c"
int main()
{
yyparse();
return 0;
}
I require infix to prefix conversion.... pls upload it........
ReplyDelete