e6f2a6f056711e6439bfe6738e527e3ffc47a6f4
[uebersetzerbau-ss10.git] / parser / parser.y
1 %{
2         #include <stdio.h>
3         #include <stdlib.h>
4 %}
5
6 %start Program
7 %token STRUCT END METHOD VAR IF THEN ELSE WHILE DO RETURN NOT OR THIS
8 %token IDENT NUM
9
10 %%
11
12 Program:
13           Methoddef ';' Program
14         | Structdef ';' Program
15         |
16         ;
17
18 Structdef:
19           STRUCT Idents END
20         ;
21
22 Methoddef:
23           METHOD IDENT '(' Idents ')' Statseq END
24         ;
25
26 Idents:
27           IDENT Idents
28         |
29         ;
30
31 Statseq:
32           Statement ';' Statseq
33         |
34         ;
35
36 Statement:
37           Lexpr ':=' Expr
38         | VAR IDENT ':=' Expr
39         | Expr
40         | IF Expr THEN Statseq END
41         | IF Expr THEN Statseq ELSE Statseq END
42         | WHILE Expr DO Statseq END
43         | RETURN Expr
44         ;
45
46 Lexpr:
47           IDENT
48         | Term '.' IDENT
49         ;
50
51 Expr:
52           Term
53         | NOT Term
54         | Term Minusterm
55         | Term Multerm
56         | Term Orterm
57         | Term '<' Term
58         | Term '=' Term
59         ;
60
61 Minusterm:
62           '-' Term Minusterm
63         | '-' Term
64         ;
65
66 Multerm:
67           '*' Term Multerm
68         | '*' Term
69         ;
70
71 Orterm:
72           OR Term Orterm
73         | OR Term
74         ;
75
76
77 Term:
78           '(' Expr ')'
79         | NUM
80         | '-' NUM
81         | THIS
82         | IDENT
83         | Term '.' IDENT
84         | Methcall
85         ;
86
87 Methcall:
88           IDENT '(' Exprs ')'
89         | Term '.' IDENT '(' Exprs ')'
90         ;
91
92 /* beachte dass hier auch "nichts" vorkommen kann
93  * sonst waer ein aufruf der art 'f()' nicht
94  * moeglich (leere parameterliste) */
95 Exprs:
96         | Expr
97         | Exprs ',' Expr
98         ;
99
100 %%
101
102 extern int yylex();
103 extern int yylineno;
104
105 int yyerror(char *error_text)
106 {
107         fprintf(stderr,"Line %i: %s\n", yylineno, error_text);
108         exit(2);
109 }
110
111 int main(int argc, char **argv)
112 {
113         #if 0
114         yydebug=1;
115         #endif
116         yyparse();
117         return 0;
118 }
119