[12 - Tree Grammar] Add suggested solution
This commit is contained in:
149
CC-Praxis-Antlr Baumgrammatiken/parseTree.dot
Normal file
149
CC-Praxis-Antlr Baumgrammatiken/parseTree.dot
Normal file
@@ -0,0 +1,149 @@
|
||||
digraph {
|
||||
|
||||
ordering=out;
|
||||
ranksep=.4;
|
||||
bgcolor="lightgrey"; node [shape=box, fixedsize=false, fontsize=12, fontname="Helvetica-bold", fontcolor="blue"
|
||||
width=.25, height=.25, color="black", fillcolor="white", style="filled, solid, bold"];
|
||||
edge [arrowsize=.5, color="black", style="bold"]
|
||||
|
||||
n0 [label="program"];
|
||||
n1 [label="test5"];
|
||||
n2 [label="DECLLIST"];
|
||||
n2 [label="DECLLIST"];
|
||||
n3 [label="DECL"];
|
||||
n3 [label="DECL"];
|
||||
n4 [label="x"];
|
||||
n5 [label="int"];
|
||||
n6 [label="read"];
|
||||
n7 [label="DECL"];
|
||||
n7 [label="DECL"];
|
||||
n8 [label="y"];
|
||||
n9 [label="float"];
|
||||
n10 [label="print"];
|
||||
n11 [label="DECL"];
|
||||
n11 [label="DECL"];
|
||||
n12 [label="z"];
|
||||
n13 [label="int"];
|
||||
n14 [label="STATLIST"];
|
||||
n14 [label="STATLIST"];
|
||||
n15 [label="while"];
|
||||
n15 [label="while"];
|
||||
n16 [label="<"];
|
||||
n16 [label="<"];
|
||||
n17 [label="x"];
|
||||
n18 [label="4"];
|
||||
n19 [label="STATLIST"];
|
||||
n19 [label="STATLIST"];
|
||||
n20 [label="for"];
|
||||
n20 [label="for"];
|
||||
n21 [label=":="];
|
||||
n21 [label=":="];
|
||||
n22 [label="z"];
|
||||
n23 [label="0"];
|
||||
n24 [label="<"];
|
||||
n24 [label="<"];
|
||||
n25 [label="z"];
|
||||
n26 [label="4"];
|
||||
n27 [label=":="];
|
||||
n27 [label=":="];
|
||||
n28 [label="z"];
|
||||
n29 [label="+"];
|
||||
n29 [label="+"];
|
||||
n30 [label="z"];
|
||||
n31 [label="1"];
|
||||
n32 [label=":="];
|
||||
n32 [label=":="];
|
||||
n33 [label="x"];
|
||||
n34 [label="+"];
|
||||
n34 [label="+"];
|
||||
n35 [label="x"];
|
||||
n36 [label="2"];
|
||||
n37 [label="if"];
|
||||
n37 [label="if"];
|
||||
n38 [label="="];
|
||||
n38 [label="="];
|
||||
n39 [label="x"];
|
||||
n40 [label="4"];
|
||||
n41 [label="STATLIST"];
|
||||
n41 [label="STATLIST"];
|
||||
n42 [label=":="];
|
||||
n42 [label=":="];
|
||||
n43 [label="x"];
|
||||
n44 [label="*"];
|
||||
n44 [label="*"];
|
||||
n45 [label="z"];
|
||||
n46 [label="+"];
|
||||
n46 [label="+"];
|
||||
n47 [label="x"];
|
||||
n48 [label="2"];
|
||||
n49 [label=":="];
|
||||
n49 [label=":="];
|
||||
n50 [label="x"];
|
||||
n51 [label="+"];
|
||||
n51 [label="+"];
|
||||
n52 [label="x"];
|
||||
n53 [label="10"];
|
||||
n54 [label=":="];
|
||||
n54 [label=":="];
|
||||
n55 [label="y"];
|
||||
n56 [label="100.e-3"];
|
||||
|
||||
n0 -> n1 // "program" -> "test5"
|
||||
n0 -> n2 // "program" -> "DECLLIST"
|
||||
n2 -> n3 // "DECLLIST" -> "DECL"
|
||||
n3 -> n4 // "DECL" -> "x"
|
||||
n3 -> n5 // "DECL" -> "int"
|
||||
n3 -> n6 // "DECL" -> "read"
|
||||
n2 -> n7 // "DECLLIST" -> "DECL"
|
||||
n7 -> n8 // "DECL" -> "y"
|
||||
n7 -> n9 // "DECL" -> "float"
|
||||
n7 -> n10 // "DECL" -> "print"
|
||||
n2 -> n11 // "DECLLIST" -> "DECL"
|
||||
n11 -> n12 // "DECL" -> "z"
|
||||
n11 -> n13 // "DECL" -> "int"
|
||||
n0 -> n14 // "program" -> "STATLIST"
|
||||
n14 -> n15 // "STATLIST" -> "while"
|
||||
n15 -> n16 // "while" -> "<"
|
||||
n16 -> n17 // "<" -> "x"
|
||||
n16 -> n18 // "<" -> "4"
|
||||
n15 -> n19 // "while" -> "STATLIST"
|
||||
n19 -> n20 // "STATLIST" -> "for"
|
||||
n20 -> n21 // "for" -> ":="
|
||||
n21 -> n22 // ":=" -> "z"
|
||||
n21 -> n23 // ":=" -> "0"
|
||||
n20 -> n24 // "for" -> "<"
|
||||
n24 -> n25 // "<" -> "z"
|
||||
n24 -> n26 // "<" -> "4"
|
||||
n20 -> n27 // "for" -> ":="
|
||||
n27 -> n28 // ":=" -> "z"
|
||||
n27 -> n29 // ":=" -> "+"
|
||||
n29 -> n30 // "+" -> "z"
|
||||
n29 -> n31 // "+" -> "1"
|
||||
n20 -> n32 // "for" -> ":="
|
||||
n32 -> n33 // ":=" -> "x"
|
||||
n32 -> n34 // ":=" -> "+"
|
||||
n34 -> n35 // "+" -> "x"
|
||||
n34 -> n36 // "+" -> "2"
|
||||
n19 -> n37 // "STATLIST" -> "if"
|
||||
n37 -> n38 // "if" -> "="
|
||||
n38 -> n39 // "=" -> "x"
|
||||
n38 -> n40 // "=" -> "4"
|
||||
n37 -> n41 // "if" -> "STATLIST"
|
||||
n41 -> n42 // "STATLIST" -> ":="
|
||||
n42 -> n43 // ":=" -> "x"
|
||||
n42 -> n44 // ":=" -> "*"
|
||||
n44 -> n45 // "*" -> "z"
|
||||
n44 -> n46 // "*" -> "+"
|
||||
n46 -> n47 // "+" -> "x"
|
||||
n46 -> n48 // "+" -> "2"
|
||||
n41 -> n49 // "STATLIST" -> ":="
|
||||
n49 -> n50 // ":=" -> "x"
|
||||
n49 -> n51 // ":=" -> "+"
|
||||
n51 -> n52 // "+" -> "x"
|
||||
n51 -> n53 // "+" -> "10"
|
||||
n37 -> n54 // "if" -> ":="
|
||||
n54 -> n55 // ":=" -> "y"
|
||||
n54 -> n56 // ":=" -> "100.e-3"
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
COMMENT=4
|
||||
DECL=5
|
||||
DECLLIST=6
|
||||
DIGIT=7
|
||||
FLOATCONST=8
|
||||
ID=9
|
||||
INTCONST=10
|
||||
INVALID=11
|
||||
LETTER=12
|
||||
OTHER=13
|
||||
POSDIGIT=14
|
||||
STATLIST=15
|
||||
STRINGCONST=16
|
||||
UMINUS=17
|
||||
WS=18
|
||||
ZERO=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
|
||||
@@ -0,0 +1,92 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Main-Klasse
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
package de.dhbw.compiler.antlrxtreegrammar;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.PrintWriter;
|
||||
import org.antlr.runtime.ANTLRInputStream;
|
||||
import org.antlr.runtime.CommonTokenStream;
|
||||
import org.antlr.runtime.tree.CommonTree;
|
||||
import org.antlr.runtime.tree.CommonTreeNodeStream;
|
||||
import org.antlr.runtime.tree.DOTTreeGenerator;
|
||||
import org.antlr.runtime.tree.Tree;
|
||||
import org.antlr.stringtemplate.StringTemplate;
|
||||
|
||||
public class AntlrXTreeGrammarMain {
|
||||
|
||||
public static void saveToGrapvizDOT(Tree tree, String name) throws FileNotFoundException {
|
||||
StringTemplate dot = (new DOTTreeGenerator()).toDOT(tree);
|
||||
PrintWriter out = new PrintWriter(name);
|
||||
out.println(dot.toString());
|
||||
out.close();
|
||||
}
|
||||
|
||||
private static final String TEST =
|
||||
"program test2;\n"+
|
||||
" x : int;\n"+
|
||||
" y : float;\n"+
|
||||
" z : string;\n"+
|
||||
"begin\n"+
|
||||
" x := 4+5+6.2;\n"+
|
||||
" y := 3.56+1.2e3+45.e-67+4e34+3E-1;\n"+
|
||||
" z := \"Hello \\\"World\\\"\" + \":\";\n"+
|
||||
" z := \"Peter\" + 4;\n"+
|
||||
" if 2<3 then abc := 5 else abc := 7;\n"+
|
||||
" while (x>y) y := y+1;\n"+
|
||||
" begin\n"+
|
||||
" for (x:=1; x<6; x:=x+1) y:=y+2;\n"+
|
||||
" end;\n"+
|
||||
"end.";
|
||||
|
||||
private static final String TEST2 =
|
||||
"program test2;\n"+
|
||||
"begin\n"+
|
||||
" x := 4-5*6*7+8/9;\n"+
|
||||
"end.";
|
||||
|
||||
private static final String BEISPIELFOLIEN =
|
||||
"program test5;\n"+
|
||||
" read x : int;\n"+
|
||||
" print y : float;\n"+
|
||||
" z : int;\n"+
|
||||
"begin\n"+
|
||||
" while (x<4) begin\n"+
|
||||
" for (z:=0; z<4; z:=z+1) x:=x+2;\n"+
|
||||
" if x=4 then begin\n"+
|
||||
" x:=z*(x+2);\n"+
|
||||
" x:=x+10;\n"+
|
||||
" end else y:=100.e-3;\n"+
|
||||
" end;\n"+
|
||||
"end.\n";
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
ANTLRInputStream input = new ANTLRInputStream(new ByteArrayInputStream(BEISPIELFOLIEN.getBytes()));
|
||||
XLexer lexer = new XLexer(input);
|
||||
XParser parser = new XParser(new CommonTokenStream(lexer));
|
||||
CommonTree tree = parser.program().getTree();
|
||||
|
||||
// Tut nichts
|
||||
XTreeGrammar treeGrammar = new XTreeGrammar(new CommonTreeNodeStream(tree));
|
||||
tree = treeGrammar.program().getTree();
|
||||
|
||||
AssignCount count = new AssignCount(new CommonTreeNodeStream(tree));
|
||||
AssignCount.program_return countResult = count.program();
|
||||
System.out.println(count.getCount());
|
||||
|
||||
// System.out.println(tree.toStringTree());
|
||||
|
||||
saveToGrapvizDOT(tree, "parseTree.dot");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Zählen von Zuweisungen
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
tree grammar AssignCount;
|
||||
|
||||
options {
|
||||
language = Java;
|
||||
output = AST;
|
||||
tokenVocab = X;
|
||||
ASTLabelType = CommonTree;
|
||||
}
|
||||
|
||||
@header {package de.dhbw.compiler.antlrxtreegrammar;}
|
||||
|
||||
@members {
|
||||
|
||||
private int count =0;
|
||||
|
||||
public int getCount() { return count; }
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Deklarationen
|
||||
decl: ^(DECL ID ('int' | 'float' | 'string') 'read'? 'print'?);
|
||||
|
||||
decllist: ^(DECLLIST decl*);
|
||||
|
||||
// Ausdr<64>cke
|
||||
expr: ^(('+' | '-' | '/' | '*') expr expr)
|
||||
| ^(UMINUS (INTCONST | FLOATCONST))
|
||||
| INTCONST | FLOATCONST | STRINGCONST | ID;
|
||||
|
||||
// Zuweisungen
|
||||
assignstat: ^(':=' ID expr) {count++;};
|
||||
|
||||
// Bedingungen
|
||||
cond: ^(('<' |'>' |'=' ) expr expr);
|
||||
|
||||
// Bedingte Zuweisung
|
||||
condstat: ^('if' cond stat stat?);
|
||||
|
||||
// Schleifen
|
||||
whilestat: ^('while' cond stat);
|
||||
forstat: ^('for' assignstat cond assignstat stat);
|
||||
|
||||
// Anweisungen
|
||||
stat: assignstat | condstat | whilestat | forstat | statlist;
|
||||
|
||||
statlist: ^(STATLIST stat*);
|
||||
|
||||
// Programme
|
||||
program: ^('program' ID decllist statlist);
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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
|
||||
COMMENT=4
|
||||
DECL=5
|
||||
DECLLIST=6
|
||||
DIGIT=7
|
||||
FLOATCONST=8
|
||||
ID=9
|
||||
INTCONST=10
|
||||
INVALID=11
|
||||
LETTER=12
|
||||
OTHER=13
|
||||
POSDIGIT=14
|
||||
STATLIST=15
|
||||
STRINGCONST=16
|
||||
UMINUS=17
|
||||
WS=18
|
||||
ZERO=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
|
||||
@@ -0,0 +1,89 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-W<>rttemberg Karlsruhe
|
||||
* Prof. Dr. J<>rn Eisenbiegler
|
||||
*
|
||||
* Vorlesung <20>bersetzerbau
|
||||
* Praxis ANTLR-Parser f<>r X
|
||||
* - Grammatik f<>r Scanner und Parser
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
grammar X;
|
||||
|
||||
options {
|
||||
language = Java;
|
||||
output = AST;
|
||||
ASTLabelType = CommonTree;
|
||||
}
|
||||
|
||||
// AST-Tokens
|
||||
tokens {
|
||||
DECL;
|
||||
STATLIST;
|
||||
DECLLIST;
|
||||
UMINUS;
|
||||
}
|
||||
|
||||
@parser::header {package de.dhbw.compiler.antlrxtreegrammar;}
|
||||
@lexer::header {package de.dhbw.compiler.antlrxtreegrammar;}
|
||||
|
||||
// Ignore Whitespace
|
||||
WS: ( '\t' | ' ' | '\r' | '\n' | '\f')+ { skip(); };
|
||||
COMMENT: '/*' .* '*/' { skip(); };
|
||||
|
||||
// Zeichensatz
|
||||
fragment LETTER: 'a'..'z' | 'A'..'Z';
|
||||
fragment POSDIGIT: '1'..'9';
|
||||
fragment ZERO: '0';
|
||||
fragment DIGIT: '0'..'9';
|
||||
fragment OTHER: ' ' | '.' | ':' | '\\"';
|
||||
|
||||
// Konstanten und Namen
|
||||
INTCONST: ZERO | (POSDIGIT DIGIT*);
|
||||
|
||||
FLOATCONST: (INTCONST ('.' DIGIT*)? ('e'|'E')('+' |'-' )? INTCONST) => INTCONST ('.' DIGIT*)? ('e'|'E')('+' |'-' )? INTCONST |
|
||||
INTCONST ('.' DIGIT*)?;
|
||||
|
||||
STRINGCONST: '\"' (LETTER|DIGIT|OTHER)* '\"';
|
||||
|
||||
ID: LETTER (LETTER|DIGIT)*;
|
||||
|
||||
INVALID: .;
|
||||
|
||||
// Deklarationen
|
||||
decl: ID ':' (type='int' | type='float' | type='string') ';' -> ^(DECL ID $type)
|
||||
| 'read' ID ':' (type='int' | type='float' | type='string') ';' -> ^(DECL ID $type 'read')
|
||||
| 'print' ID ':' (type='int' | type='float' | type='string') ';' -> ^(DECL ID $type 'print')
|
||||
| 'read' 'print' ID ':' (type='int' | type='float' | type='string') ';' -> ^(DECL ID $type 'read' 'print');
|
||||
|
||||
decllist: decl* -> ^(DECLLIST decl*);
|
||||
|
||||
// Ausdr<64>cke
|
||||
expr: multexpr (('+'^ | '-'^) multexpr)*;
|
||||
multexpr: simpleexpr (('*'^ | '/'^) simpleexpr)*;
|
||||
simpleexpr: '('! expr ')'!
|
||||
| INTCONST | '-' INTCONST -> ^(UMINUS INTCONST)
|
||||
| FLOATCONST | '-' FLOATCONST -> ^(UMINUS FLOATCONST)
|
||||
| ID | STRINGCONST;
|
||||
|
||||
// Zuweisung
|
||||
assignstat: ID ':='^ expr;
|
||||
|
||||
// Bedingungen
|
||||
cond: expr ('<'^ |'>'^ |'='^ ) expr;
|
||||
|
||||
// Bedingte Zuweisung
|
||||
condstat: 'if'^ cond 'then'! stat (options {greedy=true;}: 'else'! stat)?;
|
||||
|
||||
// Schleifen
|
||||
whilestat: 'while' '(' cond ')' stat -> ^('while' cond stat);
|
||||
forstat: 'for'^ '('! assignstat ';'! cond ';'! assignstat ')'! stat;
|
||||
|
||||
// Anweisungen
|
||||
stat: assignstat | condstat | whilestat | forstat | statlist;
|
||||
|
||||
statlist: 'begin' (stat ';')* 'end' -> ^(STATLIST stat*);
|
||||
|
||||
// Programme
|
||||
program: 'program' ID ';' decllist statlist '.' EOF -> ^('program' ID decllist statlist);
|
||||
@@ -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
|
||||
COMMENT=4
|
||||
DECL=5
|
||||
DECLLIST=6
|
||||
DIGIT=7
|
||||
FLOATCONST=8
|
||||
ID=9
|
||||
INTCONST=10
|
||||
INVALID=11
|
||||
LETTER=12
|
||||
OTHER=13
|
||||
POSDIGIT=14
|
||||
STATLIST=15
|
||||
STRINGCONST=16
|
||||
UMINUS=17
|
||||
WS=18
|
||||
ZERO=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
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,52 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Leere Baumgrammatik
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
tree grammar XTreeGrammar;
|
||||
|
||||
options {
|
||||
language = Java;
|
||||
output = AST;
|
||||
tokenVocab = X;
|
||||
ASTLabelType = CommonTree;
|
||||
}
|
||||
|
||||
@header {package de.dhbw.compiler.antlrxtreegrammar;}
|
||||
|
||||
// Deklarationen
|
||||
decl: ^(DECL ID ('int' | 'float' | 'string') 'read'? 'print'?);
|
||||
|
||||
decllist: ^(DECLLIST decl*);
|
||||
|
||||
// Ausdr<64>cke
|
||||
expr: ^(('+' | '-' | '/' | '*') expr expr)
|
||||
| ^(UMINUS (INTCONST | FLOATCONST))
|
||||
| INTCONST | FLOATCONST | STRINGCONST | ID;
|
||||
|
||||
// Zuweisung
|
||||
assignstat: ^(':=' ID expr);
|
||||
|
||||
// Bedingungen
|
||||
cond: ^(('<' |'>' |'=' ) expr expr);
|
||||
|
||||
// Bedingte Zuweisung
|
||||
condstat: ^('if' cond stat stat?);
|
||||
|
||||
// Schleifen
|
||||
whilestat: ^('while' cond stat);
|
||||
forstat: ^('for' assignstat cond assignstat stat);
|
||||
|
||||
// Anweisungen
|
||||
stat: assignstat | condstat | whilestat | forstat | statlist;
|
||||
|
||||
statlist: ^(STATLIST stat*);
|
||||
|
||||
// Programme
|
||||
program: ^('program' ID decllist statlist);
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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
|
||||
COMMENT=4
|
||||
DECL=5
|
||||
DECLLIST=6
|
||||
DIGIT=7
|
||||
FLOATCONST=8
|
||||
ID=9
|
||||
INTCONST=10
|
||||
INVALID=11
|
||||
LETTER=12
|
||||
OTHER=13
|
||||
POSDIGIT=14
|
||||
STATLIST=15
|
||||
STRINGCONST=16
|
||||
UMINUS=17
|
||||
WS=18
|
||||
ZERO=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
|
||||
@@ -0,0 +1,46 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Testfall-Utility für Parser
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
|
||||
package de.dhbw.compiler.antlrxtreegrammar.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
import org.antlr.runtime.ANTLRInputStream;
|
||||
import org.antlr.runtime.CommonTokenStream;
|
||||
import org.antlr.runtime.ParserRuleReturnScope;
|
||||
import org.antlr.runtime.tree.CommonTree;
|
||||
import org.antlr.runtime.tree.CommonTreeNodeStream;
|
||||
|
||||
import de.dhbw.compiler.antlrxtreegrammar.AssignCount;
|
||||
import de.dhbw.compiler.antlrxtreegrammar.XLexer;
|
||||
import de.dhbw.compiler.antlrxtreegrammar.XParser;
|
||||
import de.dhbw.compiler.antlrxtreegrammar.XTreeGrammar;
|
||||
|
||||
public abstract class AssignCountTest {
|
||||
|
||||
protected void testTreeGrammarTree(String in, int expected) throws Exception {
|
||||
ANTLRInputStream input = new ANTLRInputStream(new ByteArrayInputStream(in.getBytes()));
|
||||
XLexer scanner = new XLexer(input);
|
||||
CommonTokenStream tokens = new CommonTokenStream(scanner);
|
||||
XParser parser = new XParser(tokens);
|
||||
ParserRuleReturnScope result = parser.program();
|
||||
CommonTree out = (CommonTree) result.getTree();
|
||||
|
||||
AssignCount count = new AssignCount(new CommonTreeNodeStream(out));
|
||||
out = count.program().getTree();
|
||||
|
||||
assertEquals(expected, count.getCount());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Testfall-Utility für Parser
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
|
||||
package de.dhbw.compiler.antlrxtreegrammar.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
import org.antlr.runtime.ANTLRInputStream;
|
||||
import org.antlr.runtime.CommonTokenStream;
|
||||
import org.antlr.runtime.ParserRuleReturnScope;
|
||||
import org.antlr.runtime.tree.CommonTree;
|
||||
import de.dhbw.compiler.antlrxtreegrammar.XLexer;
|
||||
import de.dhbw.compiler.antlrxtreegrammar.XParser;
|
||||
|
||||
public abstract class ParseTreeTest {
|
||||
|
||||
protected void testParseTree(String in, String expected) throws Exception {
|
||||
ANTLRInputStream input = new ANTLRInputStream(new ByteArrayInputStream(in.getBytes()));
|
||||
XLexer scanner = new XLexer(input);
|
||||
CommonTokenStream tokens = new CommonTokenStream(scanner);
|
||||
XParser parser = new XParser(tokens);
|
||||
ParserRuleReturnScope result = parser.program();
|
||||
CommonTree out = (CommonTree) result.getTree();
|
||||
|
||||
if (out==null) {
|
||||
assertEquals(expected, out);
|
||||
} else {
|
||||
assertEquals(expected, out.toStringTree());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Testsuite für leere Baum-Grammatik
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
package de.dhbw.compiler.antlrxtreegrammar.test;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
import org.junit.runners.Suite.SuiteClasses;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@SuiteClasses({ TestAntlrXScanner1.class, TestAntlrXScanner2.class, TestAntlrXScanner3.class,
|
||||
TestAntlrXParser1.class, TestAntlrXParser2.class,
|
||||
TestAntlrXAssignCount1.class, TestAntlrXAssignCount2.class})
|
||||
public class TestAntlrXAssignCount {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Testf<74>lle für leere Baum-Grammmatik
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
package de.dhbw.compiler.antlrxtreegrammar.test;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runners.MethodSorters;
|
||||
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class TestAntlrXAssignCount1 extends AssignCountTest {
|
||||
|
||||
@Test
|
||||
public void program00BeginEnd() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program02BeginEnd2() throws Exception {
|
||||
String test = "program beginEnd2;\n"+
|
||||
"begin\n"+
|
||||
" begin\n"+
|
||||
" end;"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 0);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program10Assign() throws Exception {
|
||||
String test = "program assign;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0;"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program16ExprPlusMinus() throws Exception {
|
||||
String test = "program exprPlus;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0+1+-2-3+4;"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program20Cond() throws Exception {
|
||||
String test = "program cond;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" if 2<3 then x:=1;"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program21CondElse() throws Exception {
|
||||
String test = "program condElse;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" if 2>3 then x:=1 else x:=2;"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program22CondElse2() throws Exception {
|
||||
String test = "program condelse2;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" if 2=3 then if 4<5 then x:=1 else x:=2 else x:=3;"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 3);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program23CondElse3() throws Exception {
|
||||
String test = "program condelse3;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" if 2<3 then if 4>5 then x:=1 else if 6=7 then x:=2 else x:=3;"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 3);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program24CondElse4() throws Exception {
|
||||
String test = "program condelse4;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" if 2<3 then if 4>5 then x:=1 else x:=2 else if 6=7 then x:=3 else x:=4;"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 4);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program90Xmin1() throws Exception {
|
||||
String test = "program xmin1;\n"+
|
||||
" read x : int;\n"+
|
||||
" print y : int;\n"+
|
||||
"begin\n"+
|
||||
" y := 25+2*x-6*x;\n"+
|
||||
" if x<y then \n"+
|
||||
" begin\n" +
|
||||
" x:= -3/6*(y+2);\n"+
|
||||
" if y<x then y:=y+3 else y:=y+4;\n"+
|
||||
" end\n" +
|
||||
" else\n" +
|
||||
" begin\n" +
|
||||
" if y<x then y:=y+3;\n"+
|
||||
" x:= y*(y-x)/y;\n"+
|
||||
" end;\n" +
|
||||
" y := x*y;\n"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 7);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Testf<74>lle für Parser 2
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
package de.dhbw.compiler.antlrxtreegrammar.test;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runners.MethodSorters;
|
||||
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class TestAntlrXAssignCount2 extends AssignCountTest {
|
||||
|
||||
@Test
|
||||
public void program40FloatAssign() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: float;"+
|
||||
"begin\n"+
|
||||
" x :=1.23e-45;"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program41FloatExpr1() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: float;"+
|
||||
"begin\n"+
|
||||
" x :=0.0*1.1+2/-3.4e-6*(4.4-5+6.5)*7.4;"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program42FloatExpr2() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: float;"+
|
||||
"begin\n"+
|
||||
" x :=0.0+-1.2--2.3*-4.5/-6.7;"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program43StringAssign() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: string;"+
|
||||
"begin\n"+
|
||||
" x :=\"Hello\";"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program44StringExpr1() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: string;"+
|
||||
"begin\n"+
|
||||
" x := x + \"Hello\";"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program50LoopWhile1() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: string;"+
|
||||
"begin\n"+
|
||||
" while (2<x) x:= x+1;"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program51LoopWhile2() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: string;"+
|
||||
"begin\n"+
|
||||
" while (2<x) begin "+
|
||||
" x:= x+1;"+
|
||||
" x:= x+1;"+
|
||||
" end;"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program52LoopFor1() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: string;"+
|
||||
" y: string;"+
|
||||
"begin\n"+
|
||||
" for (x:=1; x<10; x:=x+1) y:= y+1;"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program53LoopFor2() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: string;"+
|
||||
" y: string;"+
|
||||
"begin\n"+
|
||||
" for (x:=1; x<10; x:=x+1) begin y:= y+1; end;"+
|
||||
"end.";
|
||||
testTreeGrammarTree(test, 3);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program91BeispielFolien() throws Exception {
|
||||
String test = "program test5;\n"+
|
||||
" read x : int;\n"+
|
||||
" print y : float;\n"+
|
||||
" z : int;\n"+
|
||||
"begin\n"+
|
||||
" while (x<4) begin\n"+
|
||||
" for (z:=0; z<4; z:=z+1) x:=x+2;\n"+
|
||||
" if x=4 then begin\n"+
|
||||
" x:=z*(x+2);\n"+
|
||||
" x:=x+10;\n"+
|
||||
" end else y:=100.e-3;\n"+
|
||||
" end;\n"+
|
||||
"end.\n";
|
||||
testTreeGrammarTree(test, 6);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Testf<74>lle für Parser 1
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
package de.dhbw.compiler.antlrxtreegrammar.test;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runners.MethodSorters;
|
||||
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class TestAntlrXParser1 extends ParseTreeTest {
|
||||
|
||||
@Test
|
||||
public void program00BeginEnd() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd DECLLIST STATLIST)";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
// @Test
|
||||
public void program01BeginEndMore() throws Exception {
|
||||
String test = "program beginEndMore;\n"+
|
||||
"begin\n"+
|
||||
"end. falsch";
|
||||
String expected = null;
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program02BeginEnd2() throws Exception {
|
||||
String test = "program beginEnd2;\n"+
|
||||
"begin\n"+
|
||||
" begin\n"+
|
||||
" end;"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd2 DECLLIST (STATLIST STATLIST))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program10Assign() throws Exception {
|
||||
String test = "program assign;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0;"+
|
||||
"end.";
|
||||
String expected = "(program assign (DECLLIST (DECL x int)) (STATLIST (:= x 0)))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program11ExprPlus() throws Exception {
|
||||
String test = "program exprPlus;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0+1;"+
|
||||
"end.";
|
||||
String expected = "(program exprPlus (DECLLIST (DECL x int)) (STATLIST (:= x (+ 0 1))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program12ExprMinus() throws Exception {
|
||||
String test = "program exprMinus;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0-1;"+
|
||||
"end.";
|
||||
String expected = "(program exprMinus (DECLLIST (DECL x int)) (STATLIST (:= x (- 0 1))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program13ExprMul() throws Exception {
|
||||
String test = "program exprMul;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0*1;"+
|
||||
"end.";
|
||||
String expected = "(program exprMul (DECLLIST (DECL x int)) (STATLIST (:= x (* 0 1))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program14ExprDiv() throws Exception {
|
||||
String test = "program exprDiv;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0/1;"+
|
||||
"end.";
|
||||
String expected = "(program exprDiv (DECLLIST (DECL x int)) (STATLIST (:= x (/ 0 1))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program15ExprUMinus() throws Exception {
|
||||
String test = "program exprUMinus;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0--1;"+
|
||||
"end.";
|
||||
String expected = "(program exprUMinus (DECLLIST (DECL x int)) (STATLIST (:= x (- 0 (UMINUS 1)))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program16ExprPlusMinus() throws Exception {
|
||||
String test = "program exprPlus;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0+1+-2-3+4;"+
|
||||
"end.";
|
||||
String expected = "(program exprPlus (DECLLIST (DECL x int)) "
|
||||
+ "(STATLIST (:= x (+ (- (+ (+ 0 1) (UMINUS 2)) 3) 4))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program17ExprMulDiv() throws Exception {
|
||||
String test = "program exprMul;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0*1/2*-3*4;"+
|
||||
"end.";
|
||||
String expected = "(program exprMul (DECLLIST (DECL x int)) "
|
||||
+ "(STATLIST (:= x (* (* (/ (* 0 1) 2) (UMINUS 3)) 4))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program18ExprAll() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0*1+2/-3*(4-5+6)*7;"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x int)) "
|
||||
+ "(STATLIST (:= x (+ (* 0 1) (* (* (/ 2 (UMINUS 3)) (+ (- 4 5) 6)) 7)))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program19ExprVar() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: int;"+
|
||||
" y: int;"+
|
||||
" z: int;"+
|
||||
"begin\n"+
|
||||
" x :=0*x+y;"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x int) (DECL y int) (DECL z int)) "
|
||||
+ "(STATLIST (:= x (+ (* 0 x) y))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program20Cond() throws Exception {
|
||||
String test = "program cond;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" if 2<3 then x:=1;"+
|
||||
"end.";
|
||||
String expected = "(program cond (DECLLIST (DECL x int)) (STATLIST (if (< 2 3) (:= x 1))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program21CondElse() throws Exception {
|
||||
String test = "program condElse;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" if 2>3 then x:=1 else x:=2;"+
|
||||
"end.";
|
||||
String expected = "(program condElse (DECLLIST (DECL x int)) (STATLIST (if (> 2 3) (:= x 1) (:= x 2))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program22CondElse2() throws Exception {
|
||||
String test = "program condelse2;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" if 2=3 then if 4<5 then x:=1 else x:=2 else x:=3;"+
|
||||
"end.";
|
||||
String expected = "(program condelse2 (DECLLIST (DECL x int)) "
|
||||
+ "(STATLIST (if (= 2 3) (if (< 4 5) (:= x 1) (:= x 2)) (:= x 3))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program23CondElse3() throws Exception {
|
||||
String test = "program condelse3;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" if 2<3 then if 4>5 then x:=1 else if 6=7 then x:=2 else x:=3;"+
|
||||
"end.";
|
||||
String expected = "(program condelse3 (DECLLIST (DECL x int)) "
|
||||
+ "(STATLIST (if (< 2 3) (if (> 4 5) (:= x 1) (if (= 6 7) (:= x 2) (:= x 3))))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program24CondElse4() throws Exception {
|
||||
String test = "program condelse4;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" if 2<3 then if 4>5 then x:=1 else x:=2 else if 6=7 then x:=3 else x:=4;"+
|
||||
"end.";
|
||||
String expected = "(program condelse4 (DECLLIST (DECL x int)) "
|
||||
+ "(STATLIST (if (< 2 3) (if (> 4 5) (:= x 1) (:= x 2)) (if (= 6 7) (:= x 3) (:= x 4)))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program90Xmin1() throws Exception {
|
||||
String test = "program xmin1;\n"+
|
||||
" read x : int;\n"+
|
||||
" print y : int;\n"+
|
||||
"begin\n"+
|
||||
" y := 25+2*x-6*x;\n"+
|
||||
" if x<y then \n"+
|
||||
" begin\n" +
|
||||
" x:= -3/6*(y+2);\n"+
|
||||
" if y<x then y:=y+3 else y:=y+4;\n"+
|
||||
" end\n" +
|
||||
" else\n" +
|
||||
" begin\n" +
|
||||
" if y<x then y:=y+3;\n"+
|
||||
" x:= y*(y-x)/y;\n"+
|
||||
" end;\n" +
|
||||
" y := x*y;\n"+
|
||||
"end.";
|
||||
String expected = "(program xmin1 (DECLLIST (DECL x int read) (DECL y int print)) "
|
||||
+ "(STATLIST (:= y (- (+ 25 (* 2 x)) (* 6 x))) (if (< x y) "
|
||||
+ "(STATLIST (:= x (* (/ (UMINUS 3) 6) (+ y 2))) (if (< y x) (:= y (+ y 3)) (:= y (+ y 4)))) "
|
||||
+ "(STATLIST (if (< y x) (:= y (+ y 3))) (:= x (/ (* y (- y x)) y)))) (:= y (* x y))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Testf<74>lle für Parser 2
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
package de.dhbw.compiler.antlrxtreegrammar.test;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runners.MethodSorters;
|
||||
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class TestAntlrXParser2 extends ParseTreeTest {
|
||||
|
||||
@Test
|
||||
public void program30Decl() throws Exception {
|
||||
String test = "program decl;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program decl (DECLLIST (DECL x int)) STATLIST)";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program31DeclRead() throws Exception {
|
||||
String test = "program declRead;\n"+
|
||||
" read x: int;"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program declRead (DECLLIST (DECL x int read)) STATLIST)";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program32DeclPrint() throws Exception {
|
||||
String test = "program declPrint;\n"+
|
||||
" print x: int;"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program declPrint (DECLLIST (DECL x int print)) STATLIST)";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program33DeclReadPrint() throws Exception {
|
||||
String test = "program declReadPrint;\n"+
|
||||
" read print x: int;"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program declReadPrint (DECLLIST (DECL x int read print)) STATLIST)";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program34DeclMult() throws Exception {
|
||||
String test = "program declMult;\n"+
|
||||
" read print a: int;"+
|
||||
" b: int;"+
|
||||
" read c: int;"+
|
||||
" print d: int;"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program declMult (DECLLIST (DECL a int read print) (DECL b int) (DECL c int read) (DECL d int print)) "
|
||||
+ "STATLIST)";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program35DeclFloatString() throws Exception {
|
||||
String test = "program decl;\n"+
|
||||
" x: float;"+
|
||||
" y: string;"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program decl (DECLLIST (DECL x float) (DECL y string)) STATLIST)";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program36DeclReadFloatString() throws Exception {
|
||||
String test = "program declRead;\n"+
|
||||
" read x: float;"+
|
||||
" read y: string;"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program declRead (DECLLIST (DECL x float read) (DECL y string read)) STATLIST)";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program37DeclPrintFloatString() throws Exception {
|
||||
String test = "program declRead;\n"+
|
||||
" print x: float;"+
|
||||
" print y: string;"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program declRead (DECLLIST (DECL x float print) (DECL y string print)) STATLIST)";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program38DeclReadPrintFloatString() throws Exception {
|
||||
String test = "program declRead;\n"+
|
||||
" read print x: float;"+
|
||||
" read print y: string;"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program declRead (DECLLIST (DECL x float read print) (DECL y string read print)) STATLIST)";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program40FloatAssign() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: float;"+
|
||||
"begin\n"+
|
||||
" x :=1.23e-45;"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x float)) (STATLIST (:= x 1.23e-45)))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program41FloatExpr1() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: float;"+
|
||||
"begin\n"+
|
||||
" x :=0.0*1.1+2/-3.4e-6*(4.4-5+6.5)*7.4;"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x float)) "
|
||||
+ "(STATLIST (:= x (+ (* 0.0 1.1) (* (* (/ 2 (UMINUS 3.4e-6)) (+ (- 4.4 5) 6.5)) 7.4)))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program42FloatExpr2() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: float;"+
|
||||
"begin\n"+
|
||||
" x :=0.0+-1.2--2.3*-4.5/-6.7;"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x float)) "
|
||||
+ "(STATLIST (:= x (- (+ 0.0 (UMINUS 1.2)) (/ (* (UMINUS 2.3) (UMINUS 4.5)) (UMINUS 6.7))))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program43StringAssign() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: string;"+
|
||||
"begin\n"+
|
||||
" x :=\"Hello\";"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x string)) (STATLIST (:= x \"Hello\")))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program44StringExpr1() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: string;"+
|
||||
"begin\n"+
|
||||
" x := x + \"Hello\";"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x string)) (STATLIST (:= x (+ x \"Hello\"))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program50LoopWhile1() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: string;"+
|
||||
"begin\n"+
|
||||
" while (2<x) x:= x+1;"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x string)) (STATLIST (while (< 2 x) (:= x (+ x 1)))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program51LoopWhile2() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: string;"+
|
||||
"begin\n"+
|
||||
" while (2<x) begin "+
|
||||
" x:= x+1;"+
|
||||
" x:= x+1;"+
|
||||
" end;"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x string)) "
|
||||
+ "(STATLIST (while (< 2 x) (STATLIST (:= x (+ x 1)) (:= x (+ x 1))))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program52LoopFor1() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: string;"+
|
||||
" y: string;"+
|
||||
"begin\n"+
|
||||
" for (x:=1; x<10; x:=x+1) y:= y+1;"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x string) (DECL y string)) "
|
||||
+ "(STATLIST (for (:= x 1) (< x 10) (:= x (+ x 1)) (:= y (+ y 1)))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program53LoopFor2() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: string;"+
|
||||
" y: string;"+
|
||||
"begin\n"+
|
||||
" for (x:=1; x<10; x:=x+1) begin y:= y+1; end;"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x string) (DECL y string)) "
|
||||
+ "(STATLIST (for (:= x 1) (< x 10) (:= x (+ x 1)) (STATLIST (:= y (+ y 1))))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program91BeispielFolien() throws Exception {
|
||||
String test = "program test5;\n"+
|
||||
" read x : int;\n"+
|
||||
" print y : float;\n"+
|
||||
" z : int;\n"+
|
||||
"begin\n"+
|
||||
" while (x<4) begin\n"+
|
||||
" for (z:=0; z<4; z:=z+1) x:=x+2;\n"+
|
||||
" if x=4 then begin\n"+
|
||||
" x:=z*(x+2);\n"+
|
||||
" x:=x+10;\n"+
|
||||
" end else y:=100.e-3;\n"+
|
||||
" end;\n"+
|
||||
"end.\n";
|
||||
String expected = "(program test5 (DECLLIST (DECL x int read) (DECL y float print) (DECL z int)) "
|
||||
+ "(STATLIST (while (< x 4) (STATLIST (for (:= z 0) (< z 4) (:= z (+ z 1)) (:= x (+ x 2))) (if (= x 4) "
|
||||
+ "(STATLIST (:= x (* z (+ x 2))) (:= x (+ x 10))) (:= y 100.e-3))))))";
|
||||
testParseTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,285 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Testf<74>lle für Scanner 1
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
package de.dhbw.compiler.antlrxtreegrammar.test;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestAntlrXScanner1 extends TokenStreamTest {
|
||||
|
||||
@Test
|
||||
public void tokenID() throws Exception {
|
||||
testTokenList(" a ", false, new TestToken(getType("ID"),"a",1,3));
|
||||
testTokenList(" A ", false, new TestToken(getType("ID"),"A",1,3));
|
||||
testTokenList(" x ", false, new TestToken(getType("ID"),"x",1,3));
|
||||
testTokenList(" X ", false, new TestToken(getType("ID"),"X",1,3));
|
||||
testTokenList(" X0 ", false, new TestToken(getType("ID"),"X0",1,3));
|
||||
testTokenList(" a1 ", false, new TestToken(getType("ID"),"a1",1,3));
|
||||
testTokenList(" abcdefghijklmnopqrstuvwxyz ", false, new TestToken(getType("ID"),"abcdefghijklmnopqrstuvwxyz",1,3));
|
||||
testTokenList(" ABCDEFGHIJKLMNOPQRSTUVWXYZ ", false, new TestToken(getType("ID"),"ABCDEFGHIJKLMNOPQRSTUVWXYZ",1,3));
|
||||
testTokenList(" NULL ", false, new TestToken(getType("ID"),"NULL",1,3));
|
||||
testTokenList(" NulL ", false, new TestToken(getType("ID"),"NulL",1,3));
|
||||
testTokenList(" null ", false, new TestToken(getType("ID"),"null",1,3));
|
||||
testTokenList(" elsethen ", false, new TestToken(getType("ID"),"elsethen",1,3));
|
||||
testTokenList(" beginner ", false, new TestToken(getType("ID"),"beginner",1,3));
|
||||
testTokenList(" 0a1 ", false, new TestToken(getType("INTCONST"),"0",1,3), new TestToken(getType("ID"),"a1",1,4));
|
||||
testTokenList(" 9a8 ", false, new TestToken(getType("INTCONST"),"9",1,3), new TestToken(getType("ID"),"a8",1,4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenIntConst1() throws Exception {
|
||||
testTokenList(" 0 ", false, new TestToken(getType("INTCONST"),"0",1,3));
|
||||
testTokenList(" 9 ", false, new TestToken(getType("INTCONST"),"9",1,3));
|
||||
testTokenList(" 1234567890 ", false, new TestToken(getType("INTCONST"),"1234567890",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenIntConst2() throws Exception {
|
||||
testTokenList(" 00 ", true, new TestToken(getType("INTCONST"),"0",1,3),
|
||||
new TestToken(getType("INTCONST"),"0",1,4));
|
||||
testTokenList(" 004500 ", true, new TestToken(getType("INTCONST"),"0",1,3),
|
||||
new TestToken(getType("INTCONST"),"0",1,4),
|
||||
new TestToken(getType("INTCONST"),"4500",1,5));
|
||||
testTokenList(" 078 ", true, new TestToken(getType("INTCONST"),"0",1,3),
|
||||
new TestToken(getType("INTCONST"),"78",1,4));
|
||||
testTokenList(" -0 ", true, new TestToken(getType("-"), "-", 1,3),
|
||||
new TestToken(getType("INTCONST"),"0",1,4));
|
||||
testTokenList(" -1 ", true, new TestToken(getType("-"), "-", 1,3),
|
||||
new TestToken(getType("INTCONST"),"1",1,4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenFloatConst1() throws Exception {
|
||||
testTokenList(" 0. ", false, new TestToken(getType("FLOATCONST"),"0.",1,3));
|
||||
testTokenList(" 1. ", false, new TestToken(getType("FLOATCONST"),"1.",1,3));
|
||||
testTokenList(" 1.2 ", false, new TestToken(getType("FLOATCONST"),"1.2",1,3));
|
||||
testTokenList(" 1.23 ", false, new TestToken(getType("FLOATCONST"),"1.23",1,3));
|
||||
testTokenList(" 3.4e5 ", false, new TestToken(getType("FLOATCONST"),"3.4e5",1,3));
|
||||
testTokenList(" 3.4e567 ", false, new TestToken(getType("FLOATCONST"),"3.4e567",1,3));
|
||||
testTokenList(" 3.4e-5 ", false, new TestToken(getType("FLOATCONST"),"3.4e-5",1,3));
|
||||
testTokenList(" 3.4e-567 ", false, new TestToken(getType("FLOATCONST"),"3.4e-567",1,3));
|
||||
testTokenList(" 6.7E8 ", false, new TestToken(getType("FLOATCONST"),"6.7E8",1,3));
|
||||
testTokenList(" 6.7E-8 ", false, new TestToken(getType("FLOATCONST"),"6.7E-8",1,3));
|
||||
testTokenList(" 6.7E-890 ", false, new TestToken(getType("FLOATCONST"),"6.7E-890",1,3));
|
||||
testTokenList(" 0.0 e ", false,
|
||||
new TestToken(getType("FLOATCONST"),"0.0",1,3),
|
||||
new TestToken(getType("ID"),"e",1,7));
|
||||
testTokenList(" 0.0e ", false,
|
||||
new TestToken(getType("FLOATCONST"),"0.0",1,3),
|
||||
new TestToken(getType("ID"),"e",1,6));
|
||||
testTokenList(" 0.0E ", false,
|
||||
new TestToken(getType("FLOATCONST"),"0.0",1,3),
|
||||
new TestToken(getType("ID"),"E",1,6));
|
||||
testTokenList(" 0.0e- ", false,
|
||||
new TestToken(getType("FLOATCONST"),"0.0",1,3),
|
||||
new TestToken(getType("ID"),"e",1,6),
|
||||
new TestToken(getType("-"),"-",1,7));
|
||||
testTokenList(" 0.0E- ", false,
|
||||
new TestToken(getType("FLOATCONST"),"0.0",1,3),
|
||||
new TestToken(getType("ID"),"E",1,6),
|
||||
new TestToken(getType("-"),"-",1,7));
|
||||
testTokenList(" 0.0-e ", false, new TestToken(getType("FLOATCONST"),"0.0",1,3),
|
||||
new TestToken(getType("-"),"-",1,6),
|
||||
new TestToken(getType("ID"),"e",1,7));
|
||||
testTokenList(" 0.03 ", false, new TestToken(getType("FLOATCONST"),"0.03",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenFloatConst2() throws Exception {
|
||||
testTokenList(" -1. ", false, new TestToken(getType("-"), "-", 1,3),
|
||||
new TestToken(getType("FLOATCONST"),"1.",1,4));
|
||||
testTokenList(" -1.2 ", false, new TestToken(getType("-"), "-", 1,3),
|
||||
new TestToken(getType("FLOATCONST"),"1.2",1,4));
|
||||
testTokenList(" 3.4e05 ", false, new TestToken(getType("FLOATCONST"),"3.4e0",1,3),
|
||||
new TestToken(getType("INTCONST"),"5",1,8));
|
||||
testTokenList(" 06.7e8 ", false, new TestToken(getType("INTCONST"),"0",1,3),
|
||||
new TestToken(getType("FLOATCONST"),"6.7e8",1,4));
|
||||
testTokenList(" 9.01e-05. ", false, new TestToken(getType("FLOATCONST"),"9.01e-0",1,3),
|
||||
new TestToken(getType("FLOATCONST"),"5.",1,10));
|
||||
testTokenList(" 6.07E089 ", false, new TestToken(getType("FLOATCONST"),"6.07E0",1,3),
|
||||
new TestToken(getType("INTCONST"),"89",1,9));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenStringConst() throws Exception {
|
||||
testTokenList(" \"a\" ", false, new TestToken(getType("STRINGCONST"),"\"a\"",1,3));
|
||||
testTokenList(" \"\" ", false, new TestToken(getType("STRINGCONST"),"\"\"",1,3));
|
||||
testTokenList(" \"abcdefghijklmnopqrstuvwxyz\" ", false,
|
||||
new TestToken(getType("STRINGCONST"),"\"abcdefghijklmnopqrstuvwxyz\"",1,3));
|
||||
testTokenList(" \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\" ", false,
|
||||
new TestToken(getType("STRINGCONST"),"\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"",1,3));
|
||||
testTokenList(" \"\\\".: abcd\"", false,
|
||||
new TestToken(getType("STRINGCONST"),"\"\\\".: abcd\"",1,3));
|
||||
/*
|
||||
testTokenList(" \"+\" ", false, new TestToken(getType("INVALID"),"\"",1,3),
|
||||
new TestToken(getType("+"),"+",1,4),
|
||||
new TestToken(getType("INVALID"),"\"",1,5));
|
||||
testTokenList(" \";\" ", false, new TestToken(getType("INVALID"),"\"",1,3),
|
||||
new TestToken(getType(";"),";",1,4),
|
||||
new TestToken(getType("INVALID"),"\"",1,5));
|
||||
testTokenList(" \"?\" ", false, new TestToken(getType("INVALID"),"\"",1,3),
|
||||
new TestToken(getType("INVALID"),"?",1,4),
|
||||
new TestToken(getType("INVALID"),"\"",1,5));
|
||||
testTokenList(" \"\\\" ", false, new TestToken(getType("INVALID"),"\"",1,3),
|
||||
new TestToken(getType("INVALID"),"\\",1,4),
|
||||
new TestToken(getType("INVALID"),"\"",1,5));
|
||||
testTokenList(" \"abcd ", false, new TestToken(getType("INVALID"),"\"",1,3),
|
||||
new TestToken(getType("INVALID"),"abcd",1,4)); */
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenLBR() throws Exception {
|
||||
testTokenList(" ( ", false, new TestToken(getType("("),"(",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenRBR() throws Exception {
|
||||
testTokenList(" ) ", false, new TestToken(getType(")"),")",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenMULT() throws Exception {
|
||||
testTokenList(" * ", false, new TestToken(getType("*"),"*",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenPLUS() throws Exception {
|
||||
testTokenList(" + ", false, new TestToken(getType("+"),"+",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenMINUS() throws Exception {
|
||||
testTokenList(" - ", false, new TestToken(getType("-"),"-",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenDOT() throws Exception {
|
||||
testTokenList(" . ", false, new TestToken(getType("."),".",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenDIV() throws Exception {
|
||||
testTokenList(" / ", false, new TestToken(getType("/"),"/",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenCOLON() throws Exception {
|
||||
testTokenList(" : ", false, new TestToken(getType(":"),":",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenASSIGN() throws Exception {
|
||||
testTokenList(" := ", false, new TestToken(getType(":="),":=",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenSEMICOLON() throws Exception {
|
||||
testTokenList(" ; ", false, new TestToken(getType(";"),";",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenLESS() throws Exception {
|
||||
testTokenList(" < ", false, new TestToken(getType("<"),"<",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenEQUALS() throws Exception {
|
||||
testTokenList(" = ", false, new TestToken(getType("="),"=",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenMORE() throws Exception {
|
||||
testTokenList(" > ", false, new TestToken(getType(">"),">",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenBEGIN() throws Exception {
|
||||
testTokenList(" begin ", false, new TestToken(getType("BEGIN"),"begin",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenELSE() throws Exception {
|
||||
testTokenList(" else ", false, new TestToken(getType("ELSE"),"else",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenEND() throws Exception {
|
||||
testTokenList(" end ", false, new TestToken(getType("END"),"end",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenFLOAT() throws Exception {
|
||||
testTokenList(" float ", false, new TestToken(getType("FLOAT"),"float",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenFOR() throws Exception {
|
||||
testTokenList(" for ", false, new TestToken(getType("FOR"),"for",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenIF() throws Exception {
|
||||
testTokenList(" if ", false, new TestToken(getType("IF"),"if",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenINT() throws Exception {
|
||||
testTokenList(" int ", false, new TestToken(getType("INT"),"int",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenPRINT() throws Exception {
|
||||
testTokenList(" print ", false, new TestToken(getType("PRINT"),"print",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenPROGRAM() throws Exception {
|
||||
testTokenList(" program ", false, new TestToken(getType("PROGRAM"),"program",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenREAD() throws Exception {
|
||||
testTokenList(" read ", false, new TestToken(getType("READ"),"read",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenSTRING() throws Exception {
|
||||
testTokenList(" string ", false, new TestToken(getType("STRING"),"string",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenTHEN() throws Exception {
|
||||
testTokenList(" then ", false, new TestToken(getType("THEN"),"then",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenWHILE() throws Exception {
|
||||
testTokenList(" while ", false, new TestToken(getType("WHILE"),"while",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenEOF() throws Exception {
|
||||
testTokenList(" ", false);
|
||||
testTokenList(" ", false, new TestToken(-1,"",1,2));
|
||||
testTokenList("", false, new TestToken(-1,"",1,1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invalidÄ() throws Exception {
|
||||
testTokenList(" Ä ", false, new TestToken(getType("INVALID"),"Ä",1,2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invalidGatter() throws Exception {
|
||||
testTokenList(" # ", false, new TestToken(getType("INVALID"),"#",1,2));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Testf<74>lle für Scanner 2
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
package de.dhbw.compiler.antlrxtreegrammar.test;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestAntlrXScanner2 extends TokenStreamTest {
|
||||
|
||||
@Test
|
||||
public void tokenIntConst() throws Exception {
|
||||
testTokenList(" 0 ", true, new TestToken(getType("INTCONST"),"0",1,3));
|
||||
testTokenList(" 1 ", true, new TestToken(getType("INTCONST"),"1",1,3));
|
||||
testTokenList(" 12 ", true, new TestToken(getType("INTCONST"),"12",1,3));
|
||||
testTokenList(" 123 ", true, new TestToken(getType("INTCONST"),"123",1,3));
|
||||
testTokenList(" 1234567890 ", true, new TestToken(getType("INTCONST"),"1234567890",1,3));
|
||||
testTokenList(" 78 ", true, new TestToken(getType("INTCONST"),"78",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenFloatConst0() throws Exception {
|
||||
testTokenList(" 0. ", true, new TestToken(getType("FLOATCONST"),"0.",1,3));
|
||||
testTokenList(" 0.0 ", true, new TestToken(getType("FLOATCONST"),"0.0",1,3));
|
||||
testTokenList(" 0.0e0 ", true, new TestToken(getType("FLOATCONST"),"0.0e0",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenFloatConst1() throws Exception {
|
||||
testTokenList(" 1. ", true, new TestToken(getType("FLOATCONST"),"1.",1,3));
|
||||
testTokenList(" 1.1 ", true, new TestToken(getType("FLOATCONST"),"1.1",1,3));
|
||||
testTokenList(" 1.1e1 ", true, new TestToken(getType("FLOATCONST"),"1.1e1",1,3));
|
||||
testTokenList(" 1e1 ", true, new TestToken(getType("FLOATCONST"),"1e1",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenFloatConst123() throws Exception {
|
||||
testTokenList(" 0.12e34 ", true, new TestToken(getType("FLOATCONST"),"0.12e34",1,3));
|
||||
testTokenList(" 0.045e23 ", true, new TestToken(getType("FLOATCONST"),"0.045e23",1,3));
|
||||
testTokenList(" 123.4560e7890 ", true, new TestToken(getType("FLOATCONST"),"123.4560e7890",1,3));
|
||||
testTokenList(" 0.12E34 ", true, new TestToken(getType("FLOATCONST"),"0.12E34",1,3));
|
||||
testTokenList(" 0.045E23 ", true, new TestToken(getType("FLOATCONST"),"0.045E23",1,3));
|
||||
testTokenList(" 123.4560E7890 ", true, new TestToken(getType("FLOATCONST"),"123.4560E7890",1,3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenStringConst() throws Exception {
|
||||
testTokenList(" \"hallo .: \" ", true, new TestToken(getType("STRINGCONST"),"\"hallo .: \"",1,3));
|
||||
testTokenList(" \" \\\" \" ", true, new TestToken(getType("STRINGCONST"),"\" \\\" \"",1,3));
|
||||
/*
|
||||
testTokenList(" \"hallo , \" ", true,
|
||||
new TestToken(getType("INVALID"),"\"hallo ",1,3),
|
||||
new TestToken(getType("INVALID"),"\" ",1,12));
|
||||
testTokenList(" \",\"", true,
|
||||
new TestToken(getType("INVALID"),"\"",1,3),
|
||||
new TestToken(getType("INVALID"),"\"",1,5)); */
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Testf<74>lle für Scanner 3
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
package de.dhbw.compiler.antlrxtreegrammar.test;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestAntlrXScanner3 extends TokenStreamTest {
|
||||
|
||||
@Test
|
||||
public void program1() throws Exception {
|
||||
testTokenList("program test1;\nbegin\nend.",false,
|
||||
new TestToken(getType("PROGRAM"),"program",1,1),
|
||||
new TestToken(getType("ID"),"test1",1,9),
|
||||
new TestToken(getType(";"),";",1,14),
|
||||
new TestToken(getType("BEGIN"),"begin",2,1),
|
||||
new TestToken(getType("END"),"end",3,1),
|
||||
new TestToken(getType("."),".",3,4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program2() throws Exception {
|
||||
testTokenList( "program test2;\n"+
|
||||
" x : int;\n"+
|
||||
" y : float;\n"+
|
||||
" z : string;\n"+
|
||||
"begin\n"+
|
||||
" x := 4+5+6.2;\n"+
|
||||
" y := 3.56+1.2e3+45.e-67+4e34+3E-1;\n"+
|
||||
" z := \"Hello \\\"World\\\"\" + \":\";\n"+
|
||||
" z := \"Peter\" + 4;\n"+
|
||||
" a := 3+4;\n"+
|
||||
"end.", false,
|
||||
// program test2;
|
||||
new TestToken(getType("PROGRAM"),"program",1,1),
|
||||
new TestToken(getType("ID"),"test2",1,9),
|
||||
new TestToken(getType(";"),";",1,14),
|
||||
// x : int;
|
||||
new TestToken(getType("ID"),"x",2,2),
|
||||
new TestToken(getType(":"),":",2,4),
|
||||
new TestToken(getType("INT"),"int",2,6),
|
||||
new TestToken(getType(";"),";",2,9),
|
||||
// y : float;
|
||||
new TestToken(getType("ID"),"y",3,2),
|
||||
new TestToken(getType(":"),":",3,4),
|
||||
new TestToken(getType("FLOAT"),"float",3,6),
|
||||
new TestToken(getType(";"),";",3,11),
|
||||
// z : string;
|
||||
new TestToken(getType("ID"),"z",4,2),
|
||||
new TestToken(getType(":"),":",4,4),
|
||||
new TestToken(getType("STRING"),"string",4,6),
|
||||
new TestToken(getType(";"),";",4,12),
|
||||
// begin
|
||||
new TestToken(getType("BEGIN"),"begin",5,1),
|
||||
// x := 4+5+6.2;
|
||||
new TestToken(getType("ID"),"x",6,2),
|
||||
new TestToken(getType(":="),":=",6,4),
|
||||
new TestToken(getType("INTCONST"),"4",6,7),
|
||||
new TestToken(getType("+"),"+",6,8),
|
||||
new TestToken(getType("INTCONST"),"5",6,9),
|
||||
new TestToken(getType("+"),"+",6,10),
|
||||
new TestToken(getType("FLOATCONST"),"6.2",6,11),
|
||||
new TestToken(getType(";"),";",6,14),
|
||||
// y := 3.56+1.2e3+45.e-67+4e34+3E-1;
|
||||
new TestToken(getType("ID"),"y",7,2),
|
||||
new TestToken(getType(":="),":=",7,4),
|
||||
new TestToken(getType("FLOATCONST"),"3.56",7,7),
|
||||
new TestToken(getType("+"),"+",7,11),
|
||||
new TestToken(getType("FLOATCONST"),"1.2e3",7,12),
|
||||
new TestToken(getType("+"),"+",7,17),
|
||||
new TestToken(getType("FLOATCONST"),"45.e-67",7,18),
|
||||
new TestToken(getType("+"),"+",7,25),
|
||||
new TestToken(getType("FLOATCONST"),"4e34",7,26),
|
||||
new TestToken(getType("+"),"+",7,30),
|
||||
new TestToken(getType("FLOATCONST"),"3E-1",7,31),
|
||||
new TestToken(getType(";"),";",7,35),
|
||||
// z := \"Hello \\\"World\\\"\" + \":\";
|
||||
new TestToken(getType("ID"),"z",8,2),
|
||||
new TestToken(getType(":="),":=",8,4),
|
||||
new TestToken(getType("STRINGCONST"),"\"Hello \\\"World\\\"\"",8,7),
|
||||
new TestToken(getType("+"),"+",8,25),
|
||||
new TestToken(getType("STRINGCONST"),"\":\"",8,27),
|
||||
new TestToken(getType(";"),";",8,30),
|
||||
// z := \"Peter\" + 4;
|
||||
new TestToken(getType("ID"),"z",9,2),
|
||||
new TestToken(getType(":="),":=",9,4),
|
||||
new TestToken(getType("STRINGCONST"),"\"Peter\"",9,7),
|
||||
new TestToken(getType("+"),"+",9,15),
|
||||
new TestToken(getType("INTCONST"),"4",9,17),
|
||||
new TestToken(getType(";"),";",9,18),
|
||||
// a := 3+4;
|
||||
new TestToken(getType("ID"),"a",10,2),
|
||||
new TestToken(getType(":="),":=",10,4),
|
||||
new TestToken(getType("INTCONST"),"3",10,7),
|
||||
new TestToken(getType("+"),"+",10,8),
|
||||
new TestToken(getType("INTCONST"),"4",10,9),
|
||||
new TestToken(getType(";"),";",10,10),
|
||||
// end.
|
||||
new TestToken(getType("END"),"end",11,1),
|
||||
new TestToken(getType("."),".",11,4));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Testsuite für leere Baum-Grammatik
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
package de.dhbw.compiler.antlrxtreegrammar.test;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
import org.junit.runners.Suite.SuiteClasses;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@SuiteClasses({ TestAntlrXScanner1.class, TestAntlrXScanner2.class, TestAntlrXScanner3.class,
|
||||
TestAntlrXParser1.class, TestAntlrXParser2.class,
|
||||
TestAntlrXTreeGrammar1.class, TestAntlrXTreeGrammar2.class})
|
||||
public class TestAntlrXTreeGrammar {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Testf<74>lle für leere Baum-Grammmatik
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
package de.dhbw.compiler.antlrxtreegrammar.test;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runners.MethodSorters;
|
||||
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class TestAntlrXTreeGrammar1 extends TreeGrammarTreeTest {
|
||||
|
||||
@Test
|
||||
public void program00BeginEnd() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd DECLLIST STATLIST)";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program02BeginEnd2() throws Exception {
|
||||
String test = "program beginEnd2;\n"+
|
||||
"begin\n"+
|
||||
" begin\n"+
|
||||
" end;"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd2 DECLLIST (STATLIST STATLIST))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program10Assign() throws Exception {
|
||||
String test = "program assign;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0;"+
|
||||
"end.";
|
||||
String expected = "(program assign (DECLLIST (DECL x int)) (STATLIST (:= x 0)))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program11ExprPlus() throws Exception {
|
||||
String test = "program exprPlus;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0+1;"+
|
||||
"end.";
|
||||
String expected = "(program exprPlus (DECLLIST (DECL x int)) (STATLIST (:= x (+ 0 1))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program12ExprMinus() throws Exception {
|
||||
String test = "program exprMinus;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0-1;"+
|
||||
"end.";
|
||||
String expected = "(program exprMinus (DECLLIST (DECL x int)) (STATLIST (:= x (- 0 1))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program13ExprMul() throws Exception {
|
||||
String test = "program exprMul;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0*1;"+
|
||||
"end.";
|
||||
String expected = "(program exprMul (DECLLIST (DECL x int)) (STATLIST (:= x (* 0 1))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program14ExprDiv() throws Exception {
|
||||
String test = "program exprDiv;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0/1;"+
|
||||
"end.";
|
||||
String expected = "(program exprDiv (DECLLIST (DECL x int)) (STATLIST (:= x (/ 0 1))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program15ExprUMinus() throws Exception {
|
||||
String test = "program exprUMinus;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0--1;"+
|
||||
"end.";
|
||||
String expected = "(program exprUMinus (DECLLIST (DECL x int)) (STATLIST (:= x (- 0 (UMINUS 1)))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program16ExprPlusMinus() throws Exception {
|
||||
String test = "program exprPlus;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0+1+-2-3+4;"+
|
||||
"end.";
|
||||
String expected = "(program exprPlus (DECLLIST (DECL x int)) "
|
||||
+ "(STATLIST (:= x (+ (- (+ (+ 0 1) (UMINUS 2)) 3) 4))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program17ExprMulDiv() throws Exception {
|
||||
String test = "program exprMul;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0*1/2*-3*4;"+
|
||||
"end.";
|
||||
String expected = "(program exprMul (DECLLIST (DECL x int)) "
|
||||
+ "(STATLIST (:= x (* (* (/ (* 0 1) 2) (UMINUS 3)) 4))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program18ExprAll() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0*1+2/-3*(4-5+6)*7;"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x int)) "
|
||||
+ "(STATLIST (:= x (+ (* 0 1) (* (* (/ 2 (UMINUS 3)) (+ (- 4 5) 6)) 7)))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program19ExprVar() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: int;"+
|
||||
" y: int;"+
|
||||
" z: int;"+
|
||||
"begin\n"+
|
||||
" x :=0*x+y;"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x int) (DECL y int) (DECL z int)) "
|
||||
+ "(STATLIST (:= x (+ (* 0 x) y))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program20Cond() throws Exception {
|
||||
String test = "program cond;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" if 2<3 then x:=1;"+
|
||||
"end.";
|
||||
String expected = "(program cond (DECLLIST (DECL x int)) (STATLIST (if (< 2 3) (:= x 1))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program21CondElse() throws Exception {
|
||||
String test = "program condElse;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" if 2>3 then x:=1 else x:=2;"+
|
||||
"end.";
|
||||
String expected = "(program condElse (DECLLIST (DECL x int)) (STATLIST (if (> 2 3) (:= x 1) (:= x 2))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program22CondElse2() throws Exception {
|
||||
String test = "program condelse2;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" if 2=3 then if 4<5 then x:=1 else x:=2 else x:=3;"+
|
||||
"end.";
|
||||
String expected = "(program condelse2 (DECLLIST (DECL x int)) "
|
||||
+ "(STATLIST (if (= 2 3) (if (< 4 5) (:= x 1) (:= x 2)) (:= x 3))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program23CondElse3() throws Exception {
|
||||
String test = "program condelse3;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" if 2<3 then if 4>5 then x:=1 else if 6=7 then x:=2 else x:=3;"+
|
||||
"end.";
|
||||
String expected = "(program condelse3 (DECLLIST (DECL x int)) "
|
||||
+ "(STATLIST (if (< 2 3) (if (> 4 5) (:= x 1) (if (= 6 7) (:= x 2) (:= x 3))))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program24CondElse4() throws Exception {
|
||||
String test = "program condelse4;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" if 2<3 then if 4>5 then x:=1 else x:=2 else if 6=7 then x:=3 else x:=4;"+
|
||||
"end.";
|
||||
String expected = "(program condelse4 (DECLLIST (DECL x int)) "
|
||||
+ "(STATLIST (if (< 2 3) (if (> 4 5) (:= x 1) (:= x 2)) (if (= 6 7) (:= x 3) (:= x 4)))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program90Xmin1() throws Exception {
|
||||
String test = "program xmin1;\n"+
|
||||
" read x : int;\n"+
|
||||
" print y : int;\n"+
|
||||
"begin\n"+
|
||||
" y := 25+2*x-6*x;\n"+
|
||||
" if x<y then \n"+
|
||||
" begin\n" +
|
||||
" x:= -3/6*(y+2);\n"+
|
||||
" if y<x then y:=y+3 else y:=y+4;\n"+
|
||||
" end\n" +
|
||||
" else\n" +
|
||||
" begin\n" +
|
||||
" if y<x then y:=y+3;\n"+
|
||||
" x:= y*(y-x)/y;\n"+
|
||||
" end;\n" +
|
||||
" y := x*y;\n"+
|
||||
"end.";
|
||||
String expected = "(program xmin1 (DECLLIST (DECL x int read) (DECL y int print)) "
|
||||
+ "(STATLIST (:= y (- (+ 25 (* 2 x)) (* 6 x))) (if (< x y) "
|
||||
+ "(STATLIST (:= x (* (/ (UMINUS 3) 6) (+ y 2))) (if (< y x) (:= y (+ y 3)) (:= y (+ y 4)))) "
|
||||
+ "(STATLIST (if (< y x) (:= y (+ y 3))) (:= x (/ (* y (- y x)) y)))) (:= y (* x y))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Testf<74>lle für Parser 2
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
package de.dhbw.compiler.antlrxtreegrammar.test;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runners.MethodSorters;
|
||||
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class TestAntlrXTreeGrammar2 extends TreeGrammarTreeTest
|
||||
{
|
||||
|
||||
@Test
|
||||
public void program30Decl() throws Exception {
|
||||
String test = "program decl;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program decl (DECLLIST (DECL x int)) STATLIST)";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program31DeclRead() throws Exception {
|
||||
String test = "program declRead;\n"+
|
||||
" read x: int;"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program declRead (DECLLIST (DECL x int read)) STATLIST)";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program32DeclPrint() throws Exception {
|
||||
String test = "program declPrint;\n"+
|
||||
" print x: int;"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program declPrint (DECLLIST (DECL x int print)) STATLIST)";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program33DeclReadPrint() throws Exception {
|
||||
String test = "program declReadPrint;\n"+
|
||||
" read print x: int;"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program declReadPrint (DECLLIST (DECL x int read print)) STATLIST)";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program34DeclMult() throws Exception {
|
||||
String test = "program declMult;\n"+
|
||||
" read print a: int;"+
|
||||
" b: int;"+
|
||||
" read c: int;"+
|
||||
" print d: int;"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program declMult (DECLLIST (DECL a int read print) (DECL b int) (DECL c int read) (DECL d int print)) "
|
||||
+ "STATLIST)";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program35DeclFloatString() throws Exception {
|
||||
String test = "program decl;\n"+
|
||||
" x: float;"+
|
||||
" y: string;"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program decl (DECLLIST (DECL x float) (DECL y string)) STATLIST)";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program36DeclReadFloatString() throws Exception {
|
||||
String test = "program declRead;\n"+
|
||||
" read x: float;"+
|
||||
" read y: string;"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program declRead (DECLLIST (DECL x float read) (DECL y string read)) STATLIST)";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program37DeclPrintFloatString() throws Exception {
|
||||
String test = "program declRead;\n"+
|
||||
" print x: float;"+
|
||||
" print y: string;"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program declRead (DECLLIST (DECL x float print) (DECL y string print)) STATLIST)";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program38DeclReadPrintFloatString() throws Exception {
|
||||
String test = "program declRead;\n"+
|
||||
" read print x: float;"+
|
||||
" read print y: string;"+
|
||||
"begin\n"+
|
||||
"end.";
|
||||
String expected = "(program declRead (DECLLIST (DECL x float read print) (DECL y string read print)) STATLIST)";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program40FloatAssign() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: float;"+
|
||||
"begin\n"+
|
||||
" x :=1.23e-45;"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x float)) (STATLIST (:= x 1.23e-45)))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program41FloatExpr1() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: float;"+
|
||||
"begin\n"+
|
||||
" x :=0.0*1.1+2/-3.4e-6*(4.4-5+6.5)*7.4;"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x float)) "
|
||||
+ "(STATLIST (:= x (+ (* 0.0 1.1) (* (* (/ 2 (UMINUS 3.4e-6)) (+ (- 4.4 5) 6.5)) 7.4)))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program42FloatExpr2() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: float;"+
|
||||
"begin\n"+
|
||||
" x :=0.0+-1.2--2.3*-4.5/-6.7;"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x float)) "
|
||||
+ "(STATLIST (:= x (- (+ 0.0 (UMINUS 1.2)) (/ (* (UMINUS 2.3) (UMINUS 4.5)) (UMINUS 6.7))))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program43StringAssign() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: string;"+
|
||||
"begin\n"+
|
||||
" x :=\"Hello\";"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x string)) (STATLIST (:= x \"Hello\")))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program44StringExpr1() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: string;"+
|
||||
"begin\n"+
|
||||
" x := x + \"Hello\";"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x string)) (STATLIST (:= x (+ x \"Hello\"))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program50LoopWhile1() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: string;"+
|
||||
"begin\n"+
|
||||
" while (2<x) x:= x+1;"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x string)) (STATLIST (while (< 2 x) (:= x (+ x 1)))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program51LoopWhile2() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: string;"+
|
||||
"begin\n"+
|
||||
" while (2<x) begin "+
|
||||
" x:= x+1;"+
|
||||
" x:= x+1;"+
|
||||
" end;"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x string)) "
|
||||
+ "(STATLIST (while (< 2 x) (STATLIST (:= x (+ x 1)) (:= x (+ x 1))))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program52LoopFor1() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: string;"+
|
||||
" y: string;"+
|
||||
"begin\n"+
|
||||
" for (x:=1; x<10; x:=x+1) y:= y+1;"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x string) (DECL y string)) "
|
||||
+ "(STATLIST (for (:= x 1) (< x 10) (:= x (+ x 1)) (:= y (+ y 1)))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program53LoopFor2() throws Exception {
|
||||
String test = "program exprAll;\n"+
|
||||
" x: string;"+
|
||||
" y: string;"+
|
||||
"begin\n"+
|
||||
" for (x:=1; x<10; x:=x+1) begin y:= y+1; end;"+
|
||||
"end.";
|
||||
String expected = "(program exprAll (DECLLIST (DECL x string) (DECL y string)) "
|
||||
+ "(STATLIST (for (:= x 1) (< x 10) (:= x (+ x 1)) (STATLIST (:= y (+ y 1))))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program91BeispielFolien() throws Exception {
|
||||
String test = "program test5;\n"+
|
||||
" read x : int;\n"+
|
||||
" print y : float;\n"+
|
||||
" z : int;\n"+
|
||||
"begin\n"+
|
||||
" while (x<4) begin\n"+
|
||||
" for (z:=0; z<4; z:=z+1) x:=x+2;\n"+
|
||||
" if x=4 then begin\n"+
|
||||
" x:=z*(x+2);\n"+
|
||||
" x:=x+10;\n"+
|
||||
" end else y:=100.e-3;\n"+
|
||||
" end;\n"+
|
||||
"end.\n";
|
||||
String expected = "(program test5 (DECLLIST (DECL x int read) (DECL y float print) (DECL z int)) "
|
||||
+ "(STATLIST (while (< x 4) (STATLIST (for (:= z 0) (< z 4) (:= z (+ z 1)) (:= x (+ x 2))) (if (= x 4) "
|
||||
+ "(STATLIST (:= x (* z (+ x 2))) (:= x (+ x 10))) (:= y 100.e-3))))))";
|
||||
testTreeGrammarTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Hilfsklasse für Scanner-tests
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
package de.dhbw.compiler.antlrxtreegrammar.test;
|
||||
|
||||
import org.antlr.runtime.CommonToken;
|
||||
|
||||
public class TestToken extends CommonToken {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public TestToken(int type, String text, int line, int column) {
|
||||
super(type, text);
|
||||
super.setLine(line);
|
||||
// Antlr beginnt mit 0, Kompatibilit<69>t zu anderen Tests
|
||||
super.setCharPositionInLine(column-1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Testfall-Utility für Scanner
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
|
||||
package de.dhbw.compiler.antlrxtreegrammar.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
import org.antlr.runtime.ANTLRInputStream;
|
||||
import org.antlr.runtime.CommonToken;
|
||||
import org.antlr.runtime.Token;
|
||||
import de.dhbw.compiler.antlrxtreegrammar.XLexer;
|
||||
import de.dhbw.compiler.antlrxtreegrammar.XParser;
|
||||
|
||||
public abstract class TokenStreamTest {
|
||||
|
||||
protected static final int IMPLICIT = -5;
|
||||
|
||||
protected int getType(String name) {
|
||||
int res = -9;
|
||||
for(res=0 ; res<XParser.tokenNames.length && !XParser.tokenNames[res].equals(name.toUpperCase()); res++) {};
|
||||
if (res>=XParser.tokenNames.length) {
|
||||
name = "'"+name.toLowerCase()+"'";
|
||||
for(res=0 ; res<XParser.tokenNames.length && !XParser.tokenNames[res].equals(name); res++);
|
||||
if (res>=XParser.tokenNames.length) {
|
||||
System.err.println("Unknown Token: "+name);
|
||||
res=-9;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
protected void testTokenList(String in, boolean convert, CommonToken... TokenList) throws Exception {
|
||||
ANTLRInputStream input = new ANTLRInputStream(new ByteArrayInputStream(in.getBytes()));
|
||||
XLexer scanner = new XLexer(input);
|
||||
Token myToken;
|
||||
|
||||
for (Token expected : TokenList) {
|
||||
myToken = scanner.nextToken();
|
||||
if (expected.getType()!=IMPLICIT) {
|
||||
assertEquals("Expect Token "+expected.toString()+". Error in type.",
|
||||
expected.getType(), myToken.getType());
|
||||
}
|
||||
if (myToken.getType()!=XLexer.EOF) {
|
||||
assertEquals("Expect Token "+expected.toString()+". Error in text.",
|
||||
expected.getText(), myToken.getText());
|
||||
assertEquals("Expect Token "+expected.toString()+". Error in line.",
|
||||
expected.getLine(), myToken.getLine());
|
||||
assertEquals("Expect Token "+expected.toString()+". Error in column.",
|
||||
expected.getCharPositionInLine(), myToken.getCharPositionInLine());
|
||||
}
|
||||
if (convert) {
|
||||
// Type-Conversion-Test not implemented yet
|
||||
/*
|
||||
switch (myToken.getType()) {
|
||||
case Token.INTCONST:
|
||||
int intValue = Integer.parseInt(expected.getText());
|
||||
assertEquals("Expected Token value "+intValue,
|
||||
intValue,((IntConstToken)myToken).getValue());
|
||||
break;
|
||||
case Token.FLOATCONST:
|
||||
double doubleValue = Double.parseDouble(expected.getText().replace("^", "e"));
|
||||
assertEquals("Expected Token value "+doubleValue,
|
||||
doubleValue,((FloatConstToken)myToken).getValue(), doubleValue*0.0000001);
|
||||
break;
|
||||
case Token.STRINGCONST:
|
||||
String stringValue = expected.getText().substring(1, expected.getText().length()-1).replace("\\\"", "\"");
|
||||
assertEquals("Expected Token value "+stringValue,
|
||||
stringValue,((StringConstToken)myToken).getValue());
|
||||
break;
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
myToken = scanner.nextToken();
|
||||
assertEquals("Expected End of File (EOF), read " + myToken.toString() + ".", Token.EOF, myToken.getType());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Testfall-Utility für Parser
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
|
||||
package de.dhbw.compiler.antlrxtreegrammar.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
import org.antlr.runtime.ANTLRInputStream;
|
||||
import org.antlr.runtime.CommonTokenStream;
|
||||
import org.antlr.runtime.ParserRuleReturnScope;
|
||||
import org.antlr.runtime.tree.CommonTree;
|
||||
import org.antlr.runtime.tree.CommonTreeNodeStream;
|
||||
|
||||
import de.dhbw.compiler.antlrxtreegrammar.XLexer;
|
||||
import de.dhbw.compiler.antlrxtreegrammar.XParser;
|
||||
import de.dhbw.compiler.antlrxtreegrammar.XTreeGrammar;
|
||||
|
||||
public abstract class TreeGrammarTreeTest {
|
||||
|
||||
protected void testTreeGrammarTree(String in, String expected) throws Exception {
|
||||
ANTLRInputStream input = new ANTLRInputStream(new ByteArrayInputStream(in.getBytes()));
|
||||
XLexer scanner = new XLexer(input);
|
||||
CommonTokenStream tokens = new CommonTokenStream(scanner);
|
||||
XParser parser = new XParser(tokens);
|
||||
ParserRuleReturnScope result = parser.program();
|
||||
CommonTree out = (CommonTree) result.getTree();
|
||||
|
||||
XTreeGrammar treeGrammar = new XTreeGrammar(new CommonTreeNodeStream(out));
|
||||
out = treeGrammar.program().getTree();
|
||||
|
||||
if (out==null) {
|
||||
assertEquals(expected, out);
|
||||
} else {
|
||||
assertEquals(expected, out.toStringTree());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user