Prefix to infix Conversion – Using Lex

0

Algorithm : 

  Scan tokens from left to right

  if you find a operator push it into stack

 if you find a operand print it and also pop the operator from the top of the stock and print it

%{
char stack[100];
int tos=0;
void push(char ch);
%}
%%
[a-zA-Z] { printf("%c %c ",yytext[0],stack[--tos]); }
[+\*\/\(\)\-] { push(yytext[0]); }
%%
void push(char ch){
stack[tos++]=ch;
}
int yywrap(){
return 1;
}
int main()
{
yylex();
return 0;
}
view raw prefixtoinfix.l hosted with ❤ by GitHub