[12 - Tree Grammar] Add "CC-Praxis-Antlr Baumgrammatiken-Leer"

This commit is contained in:
2020-06-10 08:36:49 +02:00
parent 8a640d5174
commit 4a304d4955
30 changed files with 7029 additions and 44 deletions

View File

@@ -0,0 +1,87 @@
/* **********************************************
* 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();
// Z<>hle Zuweisungen
// TODO
}
}

View File

@@ -0,0 +1,22 @@
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; }
}
program: 'todo';

View File

@@ -0,0 +1,159 @@
// $ANTLR 3.5.2 C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Baumgrammatiken-Leer\\src\\de\\dhbw\\compiler\\antlrxtreegrammar\\AssignCount.g 2019-05-23 11:52:00
package de.dhbw.compiler.antlrxtreegrammar;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import java.util.Stack;
import java.util.List;
import java.util.ArrayList;
@SuppressWarnings("all")
public class AssignCount extends TreeParser {
public static final String[] tokenNames = new String[] {
"<invalid>", "<EOR>", "<DOWN>", "<UP>", "COMMENT", "DECL", "DECLLIST",
"DIGIT", "FLOATCONST", "ID", "INTCONST", "INVALID", "LETTER", "OTHER",
"POSDIGIT", "STATLIST", "STRINGCONST", "UMINUS", "WS", "ZERO", "'('",
"')'", "'*'", "'+'", "'-'", "'.'", "'/'", "':'", "':='", "';'", "'<'",
"'='", "'>'", "'begin'", "'else'", "'end'", "'float'", "'for'", "'if'",
"'int'", "'print'", "'program'", "'read'", "'string'", "'then'", "'while'",
"'todo'"
};
public static final int EOF=-1;
public static final int T__20=20;
public static final int T__21=21;
public static final int T__22=22;
public static final int T__23=23;
public static final int T__24=24;
public static final int T__25=25;
public static final int T__26=26;
public static final int T__27=27;
public static final int T__28=28;
public static final int T__29=29;
public static final int T__30=30;
public static final int T__31=31;
public static final int T__32=32;
public static final int T__33=33;
public static final int T__34=34;
public static final int T__35=35;
public static final int T__36=36;
public static final int T__37=37;
public static final int T__38=38;
public static final int T__39=39;
public static final int T__40=40;
public static final int T__41=41;
public static final int T__42=42;
public static final int T__43=43;
public static final int T__44=44;
public static final int T__45=45;
public static final int COMMENT=4;
public static final int DECL=5;
public static final int DECLLIST=6;
public static final int DIGIT=7;
public static final int FLOATCONST=8;
public static final int ID=9;
public static final int INTCONST=10;
public static final int INVALID=11;
public static final int LETTER=12;
public static final int OTHER=13;
public static final int POSDIGIT=14;
public static final int STATLIST=15;
public static final int STRINGCONST=16;
public static final int UMINUS=17;
public static final int WS=18;
public static final int ZERO=19;
public static final int T__46=46;
// delegates
public TreeParser[] getDelegates() {
return new TreeParser[] {};
}
// delegators
public AssignCount(TreeNodeStream input) {
this(input, new RecognizerSharedState());
}
public AssignCount(TreeNodeStream input, RecognizerSharedState state) {
super(input, state);
}
protected TreeAdaptor adaptor = new CommonTreeAdaptor();
public void setTreeAdaptor(TreeAdaptor adaptor) {
this.adaptor = adaptor;
}
public TreeAdaptor getTreeAdaptor() {
return adaptor;
}
@Override public String[] getTokenNames() { return AssignCount.tokenNames; }
@Override public String getGrammarFileName() { return "C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Baumgrammatiken-Leer\\src\\de\\dhbw\\compiler\\antlrxtreegrammar\\AssignCount.g"; }
private int count =0;
public int getCount() { return count; }
public static class program_return extends TreeRuleReturnScope {
CommonTree tree;
@Override
public CommonTree getTree() { return tree; }
};
// $ANTLR start "program"
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Baumgrammatiken-Leer\\src\\de\\dhbw\\compiler\\antlrxtreegrammar\\AssignCount.g:21:1: program : 'todo' ;
public final AssignCount.program_return program() throws RecognitionException {
AssignCount.program_return retval = new AssignCount.program_return();
retval.start = input.LT(1);
CommonTree root_0 = null;
CommonTree _first_0 = null;
CommonTree _last = null;
CommonTree string_literal1=null;
CommonTree string_literal1_tree=null;
try {
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Baumgrammatiken-Leer\\src\\de\\dhbw\\compiler\\antlrxtreegrammar\\AssignCount.g:21:8: ( 'todo' )
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Baumgrammatiken-Leer\\src\\de\\dhbw\\compiler\\antlrxtreegrammar\\AssignCount.g:21:15: 'todo'
{
root_0 = (CommonTree)adaptor.nil();
_last = (CommonTree)input.LT(1);
string_literal1=(CommonTree)match(input,46,FOLLOW_46_in_program70);
string_literal1_tree = (CommonTree)adaptor.dupNode(string_literal1);
adaptor.addChild(root_0, string_literal1_tree);
}
retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0);
}
catch (RecognitionException re) {
reportError(re);
recover(input,re);
}
finally {
// do for sure before leaving
}
return retval;
}
// $ANTLR end "program"
// Delegated rules
public static final BitSet FOLLOW_46_in_program70 = new BitSet(new long[]{0x0000000000000002L});
}

View File

@@ -0,0 +1,70 @@
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
T__46=46
'('=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
'todo'=46

View File

@@ -0,0 +1,89 @@
/* **********************************************
* Duale Hochschule Baden-Württemberg Karlsruhe
* Prof. Dr. Jörn Eisenbiegler
*
* Vorlesung Übersetzerbau
* Praxis ANTLR-Parser Baumgrammatiken
* - 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);

View File

@@ -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

View File

@@ -0,0 +1,26 @@
/* **********************************************
* 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
*
* **********************************************
*/
tree grammar XTreeGrammar;
options {
language = Java;
output = AST;
tokenVocab = X;
ASTLabelType = CommonTree;
}
@header {package de.dhbw.compiler.antlrxtreegrammar;}
program: 'todo';

View File

@@ -0,0 +1,152 @@
// $ANTLR 3.5.2 C:\\Users\\eisenbiegler\\Dropbox\\workspace_<65>b\\<5C>B-Praxis-Antlr Baumgrammatiken-Leer\\src\\de\\dhbw\\compiler\\antlrxtreegrammar\\XTreeGrammar.g 2019-02-19 10:21:04
package de.dhbw.compiler.antlrxtreegrammar;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import java.util.Stack;
import java.util.List;
import java.util.ArrayList;
@SuppressWarnings("all")
public class XTreeGrammar extends TreeParser {
public static final String[] tokenNames = new String[] {
"<invalid>", "<EOR>", "<DOWN>", "<UP>", "COMMENT", "DECL", "DECLLIST",
"DIGIT", "FLOATCONST", "ID", "INTCONST", "INVALID", "LETTER", "OTHER",
"POSDIGIT", "STATLIST", "STRINGCONST", "UMINUS", "WS", "ZERO", "'('",
"')'", "'*'", "'+'", "'-'", "'.'", "'/'", "':'", "':='", "';'", "'<'",
"'='", "'>'", "'begin'", "'else'", "'end'", "'float'", "'for'", "'if'",
"'int'", "'print'", "'program'", "'read'", "'string'", "'then'", "'while'",
"'todo'"
};
public static final int EOF=-1;
public static final int T__20=20;
public static final int T__21=21;
public static final int T__22=22;
public static final int T__23=23;
public static final int T__24=24;
public static final int T__25=25;
public static final int T__26=26;
public static final int T__27=27;
public static final int T__28=28;
public static final int T__29=29;
public static final int T__30=30;
public static final int T__31=31;
public static final int T__32=32;
public static final int T__33=33;
public static final int T__34=34;
public static final int T__35=35;
public static final int T__36=36;
public static final int T__37=37;
public static final int T__38=38;
public static final int T__39=39;
public static final int T__40=40;
public static final int T__41=41;
public static final int T__42=42;
public static final int T__43=43;
public static final int T__44=44;
public static final int T__45=45;
public static final int COMMENT=4;
public static final int DECL=5;
public static final int DECLLIST=6;
public static final int DIGIT=7;
public static final int FLOATCONST=8;
public static final int ID=9;
public static final int INTCONST=10;
public static final int INVALID=11;
public static final int LETTER=12;
public static final int OTHER=13;
public static final int POSDIGIT=14;
public static final int STATLIST=15;
public static final int STRINGCONST=16;
public static final int UMINUS=17;
public static final int WS=18;
public static final int ZERO=19;
public static final int T__46=46;
// delegates
public TreeParser[] getDelegates() {
return new TreeParser[] {};
}
// delegators
public XTreeGrammar(TreeNodeStream input) {
this(input, new RecognizerSharedState());
}
public XTreeGrammar(TreeNodeStream input, RecognizerSharedState state) {
super(input, state);
}
protected TreeAdaptor adaptor = new CommonTreeAdaptor();
public void setTreeAdaptor(TreeAdaptor adaptor) {
this.adaptor = adaptor;
}
public TreeAdaptor getTreeAdaptor() {
return adaptor;
}
@Override public String[] getTokenNames() { return XTreeGrammar.tokenNames; }
@Override public String getGrammarFileName() { return "C:\\Users\\eisenbiegler\\Dropbox\\workspace_<EFBFBD>b\\<EFBFBD>B-Praxis-Antlr Baumgrammatiken-Leer\\src\\de\\dhbw\\compiler\\antlrxtreegrammar\\XTreeGrammar.g"; }
public static class program_return extends TreeRuleReturnScope {
CommonTree tree;
@Override
public CommonTree getTree() { return tree; }
};
// $ANTLR start "program"
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_<65>b\\<5C>B-Praxis-Antlr Baumgrammatiken-Leer\\src\\de\\dhbw\\compiler\\antlrxtreegrammar\\XTreeGrammar.g:24:1: program : 'todo' ;
public final XTreeGrammar.program_return program() throws RecognitionException {
XTreeGrammar.program_return retval = new XTreeGrammar.program_return();
retval.start = input.LT(1);
CommonTree root_0 = null;
CommonTree _first_0 = null;
CommonTree _last = null;
CommonTree string_literal1=null;
CommonTree string_literal1_tree=null;
try {
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_<65>b\\<5C>B-Praxis-Antlr Baumgrammatiken-Leer\\src\\de\\dhbw\\compiler\\antlrxtreegrammar\\XTreeGrammar.g:24:8: ( 'todo' )
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_<65>b\\<5C>B-Praxis-Antlr Baumgrammatiken-Leer\\src\\de\\dhbw\\compiler\\antlrxtreegrammar\\XTreeGrammar.g:24:15: 'todo'
{
root_0 = (CommonTree)adaptor.nil();
_last = (CommonTree)input.LT(1);
string_literal1=(CommonTree)match(input,46,FOLLOW_46_in_program67);
string_literal1_tree = (CommonTree)adaptor.dupNode(string_literal1);
adaptor.addChild(root_0, string_literal1_tree);
}
retval.tree = (CommonTree)adaptor.rulePostProcessing(root_0);
}
catch (RecognitionException re) {
reportError(re);
recover(input,re);
}
finally {
// do for sure before leaving
}
return retval;
}
// $ANTLR end "program"
// Delegated rules
public static final BitSet FOLLOW_46_in_program67 = new BitSet(new long[]{0x0000000000000002L});
}

View File

@@ -0,0 +1,70 @@
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
T__46=46
'('=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
'todo'=46

View File

@@ -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());
}
}

View File

@@ -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());
}
}
}

View File

@@ -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 {
}

View File

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

View File

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

View File

@@ -0,0 +1,21 @@
/* **********************************************
* Duale Hochschule Baden-Württemberg Karlsruhe
* Prof. Dr. Jörn Eisenbiegler
*
* Vorlesung Übersetzerbau
* Praxis ANTLR-Parser für X
* - Testsuite für Antlr-Parser
*
* **********************************************
*/
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 })
public class TestAntlrXParser {
}

View File

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

View File

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

View File

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

View File

@@ -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)); */
}
}

View File

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

View File

@@ -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 {
}

View File

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

View File

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

View File

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

View File

@@ -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());
}
}

View File

@@ -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());
}
}
}