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