diff options
Diffstat (limited to '12')
-rw-r--r-- | 12/input | 6 | ||||
-rw-r--r-- | 12/main.l | 44 | ||||
-rw-r--r-- | 12/main.y | 112 |
3 files changed, 162 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 @@ | |||
1 | int 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 | %{ | ||
2 | extern 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 | |||
42 | int 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> | ||
4 | int yylex(); | ||
5 | int 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 | %% | ||
20 | type_modifier: LONG | ||
21 | | UNSIGNED | ||
22 | ; | ||
23 | |||
24 | type_specifier: VOID | ||
25 | | CHAR | ||
26 | | INT | ||
27 | | FLOAT | ||
28 | | DOUBLE | ||
29 | | type_modifier type_specifier | ||
30 | ; | ||
31 | |||
32 | argument: type_specifier ID | ||
33 | ; | ||
34 | |||
35 | argument_list: argument | ||
36 | | argument COMMA argument_list | ||
37 | ; | ||
38 | |||
39 | primary_expr: NUM | ||
40 | | ID | ||
41 | ; | ||
42 | |||
43 | postfix_expr: postfix_expr INC | ||
44 | | postfix_expr DEC | ||
45 | | primary_expr | ||
46 | ; | ||
47 | |||
48 | unary_expr: INC unary_expr | ||
49 | | DEC unary_expr | ||
50 | | postfix_expr | ||
51 | ; | ||
52 | |||
53 | boolean_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 | |||
61 | assignment_expr: unary_expr ASSIGN unary_expr | ||
62 | | unary_expr ASSIGN boolean_expr | ||
63 | ; | ||
64 | |||
65 | expression: unary_expr | ||
66 | | boolean_expr | ||
67 | | assignment_expr | ||
68 | ; | ||
69 | |||
70 | for_expr: FOR LPAREN statement statement expression RPAREN statement | ||
71 | | FOR LPAREN statement statement RPAREN statement | ||
72 | ; | ||
73 | |||
74 | while_expr: WHILE LPAREN expression RPAREN block | ||
75 | | WHILE LPAREN RPAREN block | ||
76 | ; | ||
77 | |||
78 | if_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 | |||
83 | block: LBRACE statement block_cont | ||
84 | ; | ||
85 | |||
86 | block_cont: statement block_cont | ||
87 | | RBRACE | ||
88 | ; | ||
89 | |||
90 | statement: RET expression ENDL | ||
91 | | expression ENDL | ||
92 | | block | ||
93 | | for_expr | ||
94 | | if_expr | ||
95 | | while_expr | ||
96 | | ENDL | ||
97 | ; | ||
98 | |||
99 | function_definition: argument LPAREN argument_list RPAREN block | ||
100 | | argument LPAREN RPAREN block | ||
101 | ; | ||
102 | %% | ||
103 | |||
104 | int main() { | ||
105 | yyparse(); | ||
106 | printf("valid expression"); | ||
107 | } | ||
108 | |||
109 | int yyerror(char *s) { | ||
110 | printf("error: %s", s); | ||
111 | exit(0); | ||
112 | } | ||