diff options
author | Akshay <[email protected]> | 2021-04-30 17:55:19 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-04-30 17:55:19 +0100 |
commit | da1764a9f839e1d29b2bee12d35534f71f05c6ff (patch) | |
tree | d410c2191e571f9e6945ed88afd3faa1175675b3 /07 | |
parent | 9ff0cec8b507d9164d8828dcb2a87012e140fdf5 (diff) |
law & order
Diffstat (limited to '07')
-rw-r--r-- | 07/main.l | 21 | ||||
-rw-r--r-- | 07/main.y | 34 |
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 | %{ | ||
2 | extern 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 | |||
19 | int 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> | ||
4 | int yylex(); | ||
5 | int 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 | |||
14 | S: | ||
15 | E { printf("\n"); }; | ||
16 | E: | ||
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 | |||
27 | int main() { | ||
28 | yyparse(); | ||
29 | } | ||
30 | |||
31 | int yyerror(char *s) { | ||
32 | printf("error: %s", s); | ||
33 | exit(0); | ||
34 | } | ||