aboutsummaryrefslogtreecommitdiff
path: root/12/main.y
blob: 348890f2aa0079ec1480c18ea27d442b05e86036 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
%{
#include <stdio.h>
#include <stdlib.h>
int yylex();
int yyerror(char *);
%}

%token ADD SUB MUL DIV INC DEC
%token LT GT LTE GTE EQ NEQ ASSIGN
%token VOID INT DOUBLE FLOAT CHAR
%token LONG UNSIGNED
%token COMMA LPAREN RPAREN LBRACE RBRACE
%token ENDL
%token FOR RET IF ELSE WHILE
%token NUM ID

%start function_definition

%%
type_modifier: LONG
             | UNSIGNED
             ;

type_specifier: VOID
              | CHAR
              | INT
              | FLOAT
              | DOUBLE
              | type_modifier type_specifier
              ;

argument: type_specifier ID
        ;

argument_list: argument
             | argument COMMA argument_list
             ;

primary_expr: NUM
            | ID
            ;

postfix_expr: postfix_expr INC
            | postfix_expr DEC
            | primary_expr
            ;

unary_expr: INC unary_expr
          | DEC unary_expr
          | postfix_expr
          ;

boolean_expr: unary_expr LT unary_expr
            | unary_expr GT unary_expr
            | unary_expr LTE unary_expr
            | unary_expr GTE unary_expr
            | unary_expr EQ unary_expr
            | unary_expr NEQ unary_expr
            ;

assignment_expr: argument ASSIGN unary_expr
               | argument ASSIGN boolean_expr
               ;

func_call_expr: ID LPAREN expression_list RPAREN
         | ID LPAREN RPAREN
         ;

expression: unary_expr
          | boolean_expr
          | assignment_expr
          | func_call_expr
          ;

expression_list: expression
               | expression COMMA expression_list
               ;

for_expr: FOR LPAREN statement statement expression RPAREN block
        | FOR LPAREN statement statement RPAREN block
        ;

while_expr: WHILE LPAREN expression RPAREN block
          ;

if_expr: IF LPAREN expression RPAREN block
       | IF LPAREN expression RPAREN block ELSE block
       | IF LPAREN expression RPAREN block ELSE if_expr
       ;

block: LBRACE statement block_cont
     ;

block_cont: statement block_cont
          | RBRACE
          ;

return_stmt: RET ENDL
           | RET primary_expr ENDL
           ;

statement: expression ENDL
         | return_stmt
         | block
         | for_expr
         | if_expr
         | while_expr
         | ENDL
         ;

function_definition: argument LPAREN argument_list RPAREN block
                   | argument LPAREN RPAREN block
                   ;
%%

int main() {
    yyparse();
    printf("valid expression");
}

int yyerror(char *s) {
    printf("error: %s", s);
    exit(0);
}