[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

@@ -1,6 +1,63 @@
T__8=8
FLOATCONST=4
ID=5
INTCONST=6
WS=7
'TODO'=8
T__23=23
T__24=24
T__25=25
T__26=26
T__27=27
T__28=28
T__29=29
T__30=30
T__31=31
T__32=32
T__33=33
T__34=34
T__35=35
T__36=36
T__37=37
T__38=38
T__39=39
T__40=40
T__41=41
T__42=42
BINOP=4
DECL=5
DECLIST=6
ESCAPE=7
EXPR=8
FLOATCONST=9
ID=10
INTCONST=11
MODIFIER=12
PROGRAM=13
STAT=14
STATLIST=15
STRINGCONST=16
TYPE=17
UDIVIDE=18
UMINUS=19
UMULTIPLY=20
UPLUS=21
WS=22
'('=23
')'=24
'*'=20
'+'=21
'-'=19
'.'=25
'/'=18
':'=26
':='=27
';'=28
'begin'=29
'else'=30
'end'=31
'float'=32
'for'=33
'if'=34
'int'=35
'print'=36
'program'=37
'read print'=38
'read'=39
'string'=40
'then'=41
'while'=42

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

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);