[11 - Antlr Parser] Implement more stuff

This commit is contained in:
2020-06-03 13:10:56 +02:00
parent bbe75b4f76
commit 6aa5e71f1e
7 changed files with 4461 additions and 31 deletions

View File

@@ -24,6 +24,18 @@ tokens {
INTCONST;
FLOATCONST;
WS;
MODIFIER;
UMINUS='-';
UPLUS='+';
UMULTIPLY='*';
UDIVIDE='/';
DECLIST;
PROGRAM;
DECL;
TYPE;
STAT;
STATLIST;
EXPR;
}
@parser::header {package de.dhbw.compiler.antlrxparser;}
@@ -35,10 +47,51 @@ ID: ('a'..'z' | 'A'..'Z')
(options {
greedy = true; // Lese alle möglichen Zeichen ein -> Zahlen in ID -> bleibt weiterhin ID
}: 'a'..'z' | 'A'..'Z' | '0'..'9')*;
INTCONST: ('0'..'9')+;
FLOATCONST: INTCONST '.' INTCONST;
INTCONST: ('0'..'9')+;
FLOATCONST: INTCONST '.' INTCONST;
STRINGCONST: '"' (ESCAPE | ~('\\' | '"'))* '"';
ESCAPE: '\\' ('\"' |'\'' | '\\');
BINOP: '+' | '-' | '*' | '/' | '<' | '>' | '=';
WS: ('\t' | ' ' | '\r' | '\n' | '\f')+ { skip(); };
// Parser stuff
program: 'TODO';
// -- Parser stuff
program: 'program' ID ';' decllist statlist '.' EOF -> ^(PROGRAM ID decllist statlist);
// Declaration
decllist: decl decllist -> ^(DECLIST decl decllist);
decl: modifier ID ':' type ';' -> ^(DECL modifier ID type);
modifier: mod='read' | mod='print' | mod='read print' -> ^(MODIFIER[mod]);
type: t='int' | t='float' | t='string' -> ^(TYPE[t]);
// Block
statlist: 'begin' (stat ';')* 'end' -> ^(STATLIST stat*);
stat: t=assignstat | t=condstat | t=whilestat | t=forstat -> ^(STAT[t])
| statlist
-> ^(STATLIST statlist);
assignstat: ID ':=' expr -> ^(':=' ID expr);
condstat: 'if' cond 'then' stat condElseStat? -> ^('if' cond stat condElseStat? );
condElseStat: 'else' stat -> ^('else' stat);
whilestat: 'while' '(' cond ')' stat -> ^('while' cond stat);
forstat: 'for' '(' assignstat ';' cond ';' assignstat ')' stat
-> ^('for' assignstat cond assignstat stat);
expr: expr2 UPLUS expr -> ^(UPLUS expr2 expr)
| expr2 UMINUS expr -> ^(UMINUS expr2 expr)
| expr2 -> ^(expr2);
expr2: expr3 UMULTIPLY expr2 -> ^(UMULTIPLY expr3 expr2)
| expr3 UDIVIDE expr2 -> ^(UDIVIDE expr3 expr2)
| expr3 -> ^(expr3);
expr3: number -> ^(number)
| UMINUS number -> ^(UMINUS number)
| t=STRINGCONST | t=ID -> ^($t)
| '(' expr ')' -> ^(expr);
number: INTCONST | FLOATCONST;
cond: expr cond2 -> ^(expr cond2);
cond2: BINOP expr -> ^(BINOP expr);