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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
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; | |
} |