parser: makefile ... (und rest von paulchen ss08 kopiert)
[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:          Funcdef ';' Program
13                 | Structdef ';' Program
14                 |
15                 ;
16  
17 Funcdef:          FUNC ID '(' Pars ')' Stats END
18                 | FUNC ID '(' ')' Stats END
19                 ;  
20  
21 Structdef:        STRUCT Ids END
22                 ;  
23
24 Ids:              ID Ids
25                 | 
26                 ;
27
28 Pars:             Pars ',' ID
29                 | ID
30                 ; 
31  
32 Stats:            Stat ';' Stats  
33                 |
34                 ;  
35  
36 Stat:             VAR ID ASSIGN Expr
37                 | Lexpr ASSIGN Expr
38                 | IF Bool THEN Stats END
39                 | IF Bool THEN Stats ELSE Stats END
40                 | WHILE Bool DO Stats END
41                 | Call
42                 | RETURN Expr
43                 ;
44  
45 Lexpr:            ID
46                 | Field
47                 ;
48  
49 Expr:             '-' Term
50                 | Term
51                 | Term Plusterm
52                 | Term Malterm
53                 ;
54
55 Plusterm:         '+' Term Plusterm
56                 | '+' Term
57                 ;
58
59 Malterm:          '*' Term Malterm
60                 | '*' Term
61                 ;
62  
63 Term:             '(' Expr ')'
64                 | ID
65                 | NUM
66                 | Call
67                 | Field
68                 ;
69
70 Bool:             Bterm
71                 | Bterm Orterm
72                 | NOT Bterm
73                 ;
74
75 Orterm:           OR Bterm Orterm
76                 | OR Bterm
77                 ;
78
79 Bterm:            Term GREATER Term
80                 | Term '=' Term
81                 | '(' Bool ')'
82                 ;
83
84 Field:            Term '.' ID
85                 ;
86
87 Call:             ID '(' Exprs ')'
88                 | ID '(' ')'
89                 ;
90
91 Exprs:            Expr
92                 | Exprs ',' Expr
93                 ;
94
95 %%
96
97 extern int yylex();
98 extern int yylineno;
99
100 int yyerror(char *error_text) {
101         fprintf(stderr,"Line %i: %s\n",yylineno, error_text);
102         exit(2);
103 }
104
105 int main(int argc, char **argv) {
106 /*      yydebug=1; */
107         yyparse();
108         return 0;
109 }
110