aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--12/input6
-rw-r--r--12/main.l44
-rw-r--r--12/main.y112
-rw-r--r--flake.nix1
4 files changed, 163 insertions, 0 deletions
diff --git a/12/input b/12/input
new file mode 100644
index 0000000..fa64ab2
--- /dev/null
+++ b/12/input
@@ -0,0 +1,6 @@
1int main(int a, int b, int c) {
2 if (a > b) {
3 for(;;);
4 a++;
5 }
6}
diff --git a/12/main.l b/12/main.l
new file mode 100644
index 0000000..2fa1b44
--- /dev/null
+++ b/12/main.l
@@ -0,0 +1,44 @@
1%{
2extern int yylval;
3%}
4
5%%
6"void" return VOID;
7"int" return INT;
8"float" return FLOAT;
9"double" return DOUBLE;
10"char" return CHAR;
11"unsigned" return UNSIGNED;
12"long" return LONG;
13"return" return RET;
14"for" return FOR;
15"if" return IF;
16"else" return ELSE;
17"," return COMMA;
18"(" return LPAREN;
19")" return RPAREN;
20"{" return LBRACE;
21"}" return RBRACE;
22";" return ENDL;
23"++" return INC;
24"--" return DEC;
25"+" return ADD;
26"-" return SUB;
27"*" return MUL;
28"/" return DIV;
29"=" return ASSIGN;
30"==" return EQ;
31"!=" return NEQ;
32"<" return LT;
33">" return GT;
34"<=" return LTE;
35">=" return GTE;
36[a-zA-Z_][a-zA-Z_0-9]* return ID;
37. ;
38\n ;
39<<EOF>> return 0;
40%%
41
42int yywrap() {
43 return 1;
44}
diff --git a/12/main.y b/12/main.y
new file mode 100644
index 0000000..5431c55
--- /dev/null
+++ b/12/main.y
@@ -0,0 +1,112 @@
1%{
2#include <stdio.h>
3#include <stdlib.h>
4int yylex();
5int yyerror(char *);
6%}
7
8%token ADD SUB MUL DIV INC DEC
9%token LT GT LTE GTE EQ NEQ ASSIGN
10%token VOID INT DOUBLE FLOAT CHAR
11%token LONG UNSIGNED
12%token COMMA LPAREN RPAREN LBRACE RBRACE
13%token ENDL
14%token FOR RET IF ELSE WHILE
15%token NUM ID
16
17%start function_definition
18
19%%
20type_modifier: LONG
21 | UNSIGNED
22 ;
23
24type_specifier: VOID
25 | CHAR
26 | INT
27 | FLOAT
28 | DOUBLE
29 | type_modifier type_specifier
30 ;
31
32argument: type_specifier ID
33 ;
34
35argument_list: argument
36 | argument COMMA argument_list
37 ;
38
39primary_expr: NUM
40 | ID
41 ;
42
43postfix_expr: postfix_expr INC
44 | postfix_expr DEC
45 | primary_expr
46 ;
47
48unary_expr: INC unary_expr
49 | DEC unary_expr
50 | postfix_expr
51 ;
52
53boolean_expr: unary_expr LT unary_expr
54 | unary_expr GT unary_expr
55 | unary_expr LTE unary_expr
56 | unary_expr GTE unary_expr
57 | unary_expr EQ unary_expr
58 | unary_expr NEQ unary_expr
59 ;
60
61assignment_expr: unary_expr ASSIGN unary_expr
62 | unary_expr ASSIGN boolean_expr
63 ;
64
65expression: unary_expr
66 | boolean_expr
67 | assignment_expr
68 ;
69
70for_expr: FOR LPAREN statement statement expression RPAREN statement
71 | FOR LPAREN statement statement RPAREN statement
72 ;
73
74while_expr: WHILE LPAREN expression RPAREN block
75 | WHILE LPAREN RPAREN block
76 ;
77
78if_expr: IF LPAREN expression RPAREN block
79 | IF LPAREN expression RPAREN block ELSE block
80 | IF LPAREN expression RPAREN block ELSE if_expr
81 ;
82
83block: LBRACE statement block_cont
84 ;
85
86block_cont: statement block_cont
87 | RBRACE
88 ;
89
90statement: RET expression ENDL
91 | expression ENDL
92 | block
93 | for_expr
94 | if_expr
95 | while_expr
96 | ENDL
97 ;
98
99function_definition: argument LPAREN argument_list RPAREN block
100 | argument LPAREN RPAREN block
101 ;
102%%
103
104int main() {
105 yyparse();
106 printf("valid expression");
107}
108
109int yyerror(char *s) {
110 printf("error: %s", s);
111 exit(0);
112}
diff --git a/flake.nix b/flake.nix
index 17cceb1..fb2e2cb 100644
--- a/flake.nix
+++ b/flake.nix
@@ -24,6 +24,7 @@
24 { name = "09"; yacc = true; } 24 { name = "09"; yacc = true; }
25 { name = "10"; yacc = true; } 25 { name = "10"; yacc = true; }
26 { name = "11"; yacc = true; } 26 { name = "11"; yacc = true; }
27 { name = "12"; yacc = true; }
27 ]; 28 ];
28 apps = builtins.listToAttrs (builtins.map 29 apps = builtins.listToAttrs (builtins.map
29 (def: 30 (def: