[11 - Antlr Parser] Fix Scanner tests
This commit is contained in:
@@ -25,10 +25,6 @@ tokens {
|
||||
FLOATCONST;
|
||||
WS;
|
||||
MODIFIER;
|
||||
UMINUS='-';
|
||||
UPLUS='+';
|
||||
UMULTIPLY='*';
|
||||
UDIVIDE='/';
|
||||
DECLIST;
|
||||
PROGRAM;
|
||||
DECL;
|
||||
@@ -36,6 +32,7 @@ tokens {
|
||||
STAT;
|
||||
STATLIST;
|
||||
EXPR;
|
||||
INVALID;
|
||||
}
|
||||
|
||||
@parser::header {package de.dhbw.compiler.antlrxparser;}
|
||||
@@ -48,31 +45,32 @@ ID: ('a'..'z' | 'A'..'Z')
|
||||
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' | ('1'..'9') ('0'..'9')*;
|
||||
FLOATCONST: INTCONST ('.' DIGIT*)? ('e'|'E')('+' |'-' )? INTCONST
|
||||
| INTCONST ('.' DIGIT*)?;
|
||||
|
||||
STRINGCONST: '"' (ESCAPE | ~('\\' | '"'))* '"';
|
||||
ESCAPE: '\\' ('\"' |'\'' | '\\');
|
||||
|
||||
BINOP: '+' | '-' | '*' | '/' | '<' | '>' | '=';
|
||||
WS: ('\t' | ' ' | '\r' | '\n' | '\f')+ { skip(); };
|
||||
|
||||
WS: ('\t' | ' ' | '\r' | '\n' | '\f')+ { skip(); };
|
||||
INVALID: .;
|
||||
|
||||
fragment DIGIT: ('0'..'9');
|
||||
|
||||
|
||||
// -- Parser stuff
|
||||
program: 'program' ID ';' decllist statlist '.' EOF -> ^(PROGRAM ID decllist statlist);
|
||||
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]);
|
||||
modifier: 'read' 'print'? | 'read' | 'print';
|
||||
type: 'int' | 'float' | 'string';
|
||||
|
||||
// Block
|
||||
statlist: 'begin' (stat ';')* 'end' -> ^(STATLIST stat*);
|
||||
stat: t=assignstat | t=condstat | t=whilestat | t=forstat -> ^(STAT[t])
|
||||
| statlist
|
||||
-> ^(STATLIST statlist);
|
||||
stat: assignstat | condstat | whilestat | forstat | statlist;
|
||||
assignstat: ID ':=' expr -> ^(':=' ID expr);
|
||||
condstat: 'if' cond 'then' stat condElseStat? -> ^('if' cond stat condElseStat? );
|
||||
condElseStat: 'else' stat -> ^('else' stat);
|
||||
@@ -80,18 +78,23 @@ 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)
|
||||
expr: expr2 '+' expr -> ^('+' expr2 expr)
|
||||
| expr2 '-' expr -> ^('-' expr2 expr)
|
||||
| expr2 -> ^(expr2);
|
||||
expr2: expr3 UMULTIPLY expr2 -> ^(UMULTIPLY expr3 expr2)
|
||||
| expr3 UDIVIDE expr2 -> ^(UDIVIDE expr3 expr2)
|
||||
expr2: expr3 '*' expr2 -> ^('*' expr3 expr2)
|
||||
| expr3 '/' expr2 -> ^('/' expr3 expr2)
|
||||
| expr3 -> ^(expr3);
|
||||
expr3: number -> ^(number)
|
||||
| UMINUS number -> ^(UMINUS number)
|
||||
| t=STRINGCONST | t=ID -> ^($t)
|
||||
| '(' expr ')' -> ^(expr);
|
||||
expr3: number | '-' number | STRINGCONST | '('! expr^ ')';
|
||||
number: INTCONST | FLOATCONST;
|
||||
|
||||
cond: expr cond2 -> ^(expr cond2);
|
||||
cond2: BINOP expr -> ^(BINOP expr);
|
||||
compOp: '<' | '>' | '=';
|
||||
cond: expr cond2;
|
||||
cond2: compOp expr;
|
||||
|
||||
// -- Dummy rules
|
||||
// Das sollte dafür sorgen, dass implizite Tokens auch ihre Tokens bekommen
|
||||
// TODO: Remove this
|
||||
minus: '-';
|
||||
div: '/';
|
||||
multiply: '*';
|
||||
plus: '+';
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
T__20=20
|
||||
T__21=21
|
||||
T__22=22
|
||||
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
|
||||
T__43=43
|
||||
T__44=44
|
||||
T__45=45
|
||||
DECL=4
|
||||
DECLIST=5
|
||||
DIGIT=6
|
||||
ESCAPE=7
|
||||
EXPR=8
|
||||
FLOATCONST=9
|
||||
ID=10
|
||||
INTCONST=11
|
||||
INVALID=12
|
||||
MODIFIER=13
|
||||
PROGRAM=14
|
||||
STAT=15
|
||||
STATLIST=16
|
||||
STRINGCONST=17
|
||||
TYPE=18
|
||||
WS=19
|
||||
'('=20
|
||||
')'=21
|
||||
'*'=22
|
||||
'+'=23
|
||||
'-'=24
|
||||
'.'=25
|
||||
'/'=26
|
||||
':'=27
|
||||
':='=28
|
||||
';'=29
|
||||
'<'=30
|
||||
'='=31
|
||||
'>'=32
|
||||
'begin'=33
|
||||
'else'=34
|
||||
'end'=35
|
||||
'float'=36
|
||||
'for'=37
|
||||
'if'=38
|
||||
'int'=39
|
||||
'print'=40
|
||||
'program'=41
|
||||
'read'=42
|
||||
'string'=43
|
||||
'then'=44
|
||||
'while'=45
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user