ff863819717629e6728f633a36b900aed38c0333
[uebersetzerbau-ss10.git] / parser / parser.y
1 %{
2         #include <stdio.h>
3         #include <stdlib.h>
4 %}
5
6 %start Program
7 %token FUNC END STRUCT VAR IF THEN ELSE WHILE DO RETURN OR NOT
8 %token ID NUM ASSIGN GREATER
9
10 %%
11
12 Program:
13           Funcdef ';' Program
14         | Structdef ';' Program
15         |
16         ;
17
18 Funcdef:
19           FUNC ID '(' Pars ')' Stats END
20         | FUNC ID '(' ')' Stats END
21         ;
22
23 Structdef:
24           STRUCT Ids END
25         ;
26
27 Ids:
28           ID Ids
29         |
30         ;
31
32 Pars:
33           Pars ',' ID
34         | ID
35         ;
36
37 Stats:
38           Stat ';' Stats
39         |
40         ;
41
42 Stat:
43           VAR ID ASSIGN Expr
44         | Lexpr ASSIGN Expr
45         | IF Bool THEN Stats END
46         | IF Bool THEN Stats ELSE Stats END
47         | WHILE Bool DO Stats END
48         | Call
49         | RETURN Expr
50         ;
51
52 Lexpr:
53           ID
54         | Field
55         ;
56
57 Expr:
58           '-' Term
59         | Term
60         | Term Plusterm
61         | Term Malterm
62         ;
63
64 Plusterm:
65           '+' Term Plusterm
66         | '+' Term
67         ;
68
69 Malterm:
70           '*' Term Malterm
71         | '*' Term
72         ;
73
74 Term:
75           '(' Expr ')'
76         | ID
77         | NUM
78         | Call
79         | Field
80         ;
81
82 Bool:
83           Bterm
84         | Bterm Orterm
85         | NOT Bterm
86         ;
87
88 Orterm:
89           OR Bterm Orterm
90         | OR Bterm
91         ;
92
93 Bterm:
94           Term GREATER Term
95         | Term '=' Term
96         | '(' Bool ')'
97         ;
98
99 Field:
100           Term '.' ID
101         ;
102
103 Call:
104           ID '(' Exprs ')'
105         | ID '(' ')'
106         ;
107
108 Exprs:
109           Expr
110         | Exprs ',' Expr
111         ;
112
113 %%
114
115 extern int yylex();
116 extern int yylineno;
117
118 int yyerror(char *error_text)
119 {
120         fprintf(stderr,"Line %i: %s\n", yylineno, error_text);
121         exit(2);
122 }
123
124 int main(int argc, char **argv)
125 {
126         #if 0
127         yydebug=1;
128         #endif
129         yyparse();
130         return 0;
131 }
132