aboutsummaryrefslogtreecommitdiff
path: root/10
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-04-30 17:52:13 +0100
committerAkshay <[email protected]>2021-04-30 17:52:13 +0100
commit8a57bedc04fff7a92ef6d2762cfe5fc17050f021 (patch)
tree192ea2a1bd2eec0c0f84a9cb7adc9b10d005e1db /10
parent30047205e440e9ad8984e1830b8b819b5fe1f9f7 (diff)
add yacc programs
Diffstat (limited to '10')
-rw-r--r--10/input1
-rw-r--r--10/main.l22
-rw-r--r--10/main.y33
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%{
2extern int yylval;
3%}
4
5d [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
20int 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>
4int yylex();
5int yyerror(char *);
6%}
7
8%token ADD SUB MUL DIV NUM
9%left ADD SUB MUL DIV
10
11%%
12R: E {
13 printf("result: %d", $$);
14 return 0;
15 }
16E: E ADD T { $$ = $1 + $3; }
17 | E SUB T { $$ = $1 - $3; }
18 | T
19 ;
20T: T MUL NUM { $$ = $1 * $3; }
21 | T DIV NUM { $$ = $1 / $3; }
22 | NUM { $$ = $1; }
23 ;
24%%
25
26int main() {
27 yyparse();
28}
29
30int yyerror(char *s) {
31 printf("error: %s", s);
32 exit(0);
33}