aboutsummaryrefslogtreecommitdiff
path: root/7
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 /7
parent30047205e440e9ad8984e1830b8b819b5fe1f9f7 (diff)
add yacc programs
Diffstat (limited to '7')
-rw-r--r--7/main.l22
-rw-r--r--7/main.y34
2 files changed, 56 insertions, 0 deletions
diff --git a/7/main.l b/7/main.l
new file mode 100644
index 0000000..7e1332f
--- /dev/null
+++ b/7/main.l
@@ -0,0 +1,22 @@
1%{
2#include "y.tab.h"
3extern int yylval;
4%}
5
6%%
7[0-9]+ {
8 yylval = atoi(yytext);
9 return NUM;
10}
11"+" return ADD;;
12"-" return SUB;
13"*" return MUL;
14"/" return DIV;
15"(" return LPAR;
16")" return RPAR;
17\n return 0;
18%%
19
20int yywrap() {
21 return 1;
22}
diff --git a/7/main.y b/7/main.y
new file mode 100644
index 0000000..26a2007
--- /dev/null
+++ b/7/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}