parser: schaut fertig aus... muss nur noch getestet werden :D
[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 ASSIGN
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 ASSIGN Expr
38         | VAR IDENT ASSIGN 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 Plusterm
55         | Term Multerm
56         | Term Orterm
57         | Term '<' Term
58         | Term '=' Term
59         ;
60
61 Plusterm:
62           '+' Term Plusterm
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 Exprs:
93           Expr
94         | Exprs ',' Expr
95         ;
96
97 %%
98
99 extern int yylex();
100 extern int yylineno;
101
102 int yyerror(char *error_text)
103 {
104         fprintf(stderr,"Line %i: %s\n", yylineno, error_text);
105         exit(2);
106 }
107
108 int main(int argc, char **argv)
109 {
110         #if 0
111         yydebug=1;
112         #endif
113         yyparse();
114         return 0;
115 }
116