#include <stdlib.h>
%}
-%start Program
-%token FUNC END STRUCT VAR IF THEN ELSE WHILE DO RETURN OR NOT
-%token ID NUM ASSIGN GREATER
+%start Program
+%token STRUCT END METHOD VAR IF THEN ELSE WHILE DO RETURN NOT OR THIS
+%token IDENT NUM
%%
-Program: Funcdef ';' Program
- | Structdef ';' Program
- |
- ;
-
-Funcdef: FUNC ID '(' Pars ')' Stats END
- | FUNC ID '(' ')' Stats END
- ;
-
-Structdef: STRUCT Ids END
- ;
-
-Ids: ID Ids
- |
- ;
-
-Pars: Pars ',' ID
- | ID
- ;
-
-Stats: Stat ';' Stats
- |
- ;
-
-Stat: VAR ID ASSIGN Expr
- | Lexpr ASSIGN Expr
- | IF Bool THEN Stats END
- | IF Bool THEN Stats ELSE Stats END
- | WHILE Bool DO Stats END
- | Call
- | RETURN Expr
- ;
-
-Lexpr: ID
- | Field
- ;
-
-Expr: '-' Term
- | Term
- | Term Plusterm
- | Term Malterm
- ;
-
-Plusterm: '+' Term Plusterm
- | '+' Term
- ;
-
-Malterm: '*' Term Malterm
- | '*' Term
- ;
-
-Term: '(' Expr ')'
- | ID
- | NUM
- | Call
- | Field
- ;
-
-Bool: Bterm
- | Bterm Orterm
- | NOT Bterm
- ;
-
-Orterm: OR Bterm Orterm
- | OR Bterm
- ;
-
-Bterm: Term GREATER Term
- | Term '=' Term
- | '(' Bool ')'
- ;
-
-Field: Term '.' ID
- ;
-
-Call: ID '(' Exprs ')'
- | ID '(' ')'
- ;
-
-Exprs: Expr
- | Exprs ',' Expr
- ;
+Program:
+ Methoddef ';' Program
+ | Structdef ';' Program
+ |
+ ;
+
+Structdef:
+ STRUCT Idents END
+ ;
+
+Methoddef:
+ METHOD IDENT '(' Idents ')' Statseq END
+ ;
+
+Idents:
+ IDENT Idents
+ |
+ ;
+
+Statseq:
+ Statement ';' Statseq
+ |
+ ;
+
+Statement:
+ Lexpr ':=' Expr
+ | VAR IDENT ':=' Expr
+ | Expr
+ | IF Expr THEN Statseq END
+ | IF Expr THEN Statseq ELSE Statseq END
+ | WHILE Expr DO Statseq END
+ | RETURN Expr
+ ;
+
+Lexpr:
+ IDENT
+ | Term '.' IDENT
+ ;
+
+Expr:
+ Term
+ | NOT Term
+ | Term Minusterm
+ | Term Multerm
+ | Term Orterm
+ | Term '<' Term
+ | Term '=' Term
+ ;
+
+Minusterm:
+ '-' Term Minusterm
+ | '-' Term
+ ;
+
+Multerm:
+ '*' Term Multerm
+ | '*' Term
+ ;
+
+Orterm:
+ OR Term Orterm
+ | OR Term
+ ;
+
+
+Term:
+ '(' Expr ')'
+ | NUM
+ | '-' NUM
+ | THIS
+ | IDENT
+ | Term '.' IDENT
+ | IDENT '(' Exprs ')'
+ | Term '.' IDENT '(' Exprs ')'
+ ;
+
+Exprs:
+ Expr ',' Exprs
+ | Expr
+ |
+ ;
%%
extern int yylex();
extern int yylineno;
-int yyerror(char *error_text) {
- fprintf(stderr,"Line %i: %s\n",yylineno, error_text);
+int yyerror(char *error_text)
+{
+ fprintf(stderr,"Zeile %i: %s\n", yylineno, error_text);
exit(2);
}
-int main(int argc, char **argv) {
-/* yydebug=1; */
+int main(int argc, char **argv)
+{
+ #if 0
+ yydebug=1;
+ #endif
yyparse();
return 0;
}