aboutsummaryrefslogtreecommitdiff
path: root/07
diff options
context:
space:
mode:
Diffstat (limited to '07')
-rw-r--r--07/main.l21
-rw-r--r--07/main.y34
2 files changed, 55 insertions, 0 deletions
diff --git a/07/main.l b/07/main.l
new file mode 100644
index 0000000..2f98161
--- /dev/null
+++ b/07/main.l
@@ -0,0 +1,21 @@
1%{
2extern int yylval;
3%}
4
5%%
6[0-9]+ {
7 yylval = atoi(yytext);
8 return NUM;
9}
10"+" return ADD;;
11"-" return SUB;
12"*" return MUL;
13"/" return DIV;
14"(" return LPAR;
15")" return RPAR;
16\n return 0;
17%%
18
19int yywrap() {
20 return 1;
21}
diff --git a/07/main.y b/07/main.y
new file mode 100644
index 0000000..26a2007
--- /dev/null
+++ b/07/main.y
@@ -0,0 +1,34 @@
1%{
2#include <stdio.h>
3#include <stdlib.h>
4int yylex();
5int yyerror(char *);
6%}
7
8%token NUM ADD SUB MUL DIV LPAR RPAR
9%left ADD SUB MUL DIV
10%right NEG
11
12%%
13
14S:
15 E { printf("\n"); };
16E:
17 E ADD E { printf("+ "); }
18 | E MUL E { printf("* "); }
19 | E SUB E { printf("- "); }
20 | E DIV E { printf("/ "); }
21 | LPAR E RPAR
22 | SUB E %prec NEG { printf("- "); }
23 | NUM { printf("%d ", yylval); }
24 ;
25%%
26
27int main() {
28 yyparse();
29}
30
31int yyerror(char *s) {
32 printf("error: %s", s);
33 exit(0);
34}