Created
November 15, 2015 17:36
-
-
Save max-kov/4f0ecc202d45e35d09c9 to your computer and use it in GitHub Desktop.
stack calculator lex implementation
This file contains hidden or 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
%option noyywrap | |
%{ | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
%} | |
%% | |
int nums[100],i=0; | |
[\-0-9]*|[0-9]* { | |
i++; | |
nums[i]=(int) strtol(yytext, (char **)NULL, 10); | |
printf("%s = %d\n",yytext,nums[i]); | |
} | |
\+ { | |
printf("%d + %d = %d \n",nums[i],nums[i-1],nums[i-1]+nums[i]); | |
nums[i-1]=nums[i-1]+nums[i]; | |
i--; | |
} | |
\- { | |
printf("%d - %d = %d \n",nums[i],nums[i-1],nums[i-1]-nums[i]); | |
nums[i-1]=nums[i-1]-nums[i]; | |
i--; | |
} | |
\* { | |
printf("%d * %d = %d \n",nums[i],nums[i-1],nums[i-1]*nums[i]); | |
nums[i-1]=nums[i-1]*nums[i]; | |
i--; | |
} | |
\/ { | |
printf("%d / %d = %d \n",nums[i],nums[i-1],nums[i-1]/nums[i]); | |
nums[i-1]=nums[i-1]/nums[i]; | |
i--; | |
} | |
%% | |
int main(){ | |
yylex(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment