Professional Documents
Culture Documents
Infix Code
Infix Code
%{
Lex file
#include "y.tab.h"
#include<stdlib.h>
#include<math.h>
void yyerro(char *s);
#undef yywrap
%}
%%
[a-z] {return NUMBER;}
[+\-*/()^]{ return *yytext; }
[\n] { return 0; }
[ \t] ; /* Ignore */
; /* Ignore */
%%
int yywrap()
{ return 1;
}
void yyerror(char *s)
{ printf("\nError \"%s\" at token : %s\n",s,yytext);
}
%{
Yacc file
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
typedef struct stack
{
char str[10];
}stack;
int sttop=0;
stack s[100];
%}
%union{
double no;
}
%token<no> NUMBER
%type<no> expr
%left '+' '-'
%left '*' '/'
%left '^'
%left '(' ')'
%%
//Infix_out
[root@localhost ~]# yacc -d infix.y
[root@localhost ~]# lex infix.l
[root@localhost ~]# gcc y.tab.c lex.yy.c -ll -lm -o infix
[root@localhost ~]# ./infix
[root@localhost ~]# ./infix
a*b+c
S->E
Accept
E->E+E
E->ID
E->E*E
E->ID
E->ID