[13 - String Templates] Add "CC-Praxis-Antlr X Uebersetzer-Leer"
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
<component name="libraryTable">
|
||||
<library name="antlr-3.5.2-complete">
|
||||
<CLASSES>
|
||||
<root url="jar://$PROJECT_DIR$/lib/antlr-3.5.2-complete.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</component>
|
||||
10
CC-Praxis-Antlr Baumgrammatiken-Leer/.idea/libraries/lib.xml
generated
Normal file
10
CC-Praxis-Antlr Baumgrammatiken-Leer/.idea/libraries/lib.xml
generated
Normal file
@@ -0,0 +1,10 @@
|
||||
<component name="libraryTable">
|
||||
<library name="lib">
|
||||
<CLASSES>
|
||||
<root url="file://$PROJECT_DIR$/../lib" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
<jarDirectory url="file://$PROJECT_DIR$/../lib" recursive="false" />
|
||||
</library>
|
||||
</component>
|
||||
@@ -4,6 +4,7 @@
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/../CC-Praxis-Antlr Baumgrammatiken/CC-Praxis-Antlr Baumgrammatiken.iml" filepath="$PROJECT_DIR$/../CC-Praxis-Antlr Baumgrammatiken/CC-Praxis-Antlr Baumgrammatiken.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer.iml" filepath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/../CC-Praxis-Antlr X Uebersetzer-Leer/CC-Praxis-Antlr X Uebersetzer-Leer.iml" filepath="$PROJECT_DIR$/../CC-Praxis-Antlr X Uebersetzer-Leer/CC-Praxis-Antlr X Uebersetzer-Leer.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
@@ -7,6 +7,6 @@
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="antlr-3.5.2-complete" level="project" />
|
||||
<orderEntry type="library" name="lib" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
@@ -7,5 +7,6 @@
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="lib" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="lib" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
68
CC-Praxis-Antlr X Uebersetzer-Leer/X.tokens
Normal file
68
CC-Praxis-Antlr X Uebersetzer-Leer/X.tokens
Normal 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
|
||||
@@ -0,0 +1,93 @@
|
||||
/* **********************************************
|
||||
* 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.antlrxcompiler;
|
||||
|
||||
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 AntlrXCompiler {
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
public static void saveToJavaFile(StringTemplate template, String name) throws FileNotFoundException {
|
||||
PrintWriter out = new PrintWriter(name);
|
||||
out.println(template.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"+
|
||||
" y := 0*x;\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 {
|
||||
|
||||
XTreeAdaptor xTreeAdaptor = new XTreeAdaptor();
|
||||
|
||||
ANTLRInputStream input = new ANTLRInputStream(new ByteArrayInputStream(TEST2.getBytes()));
|
||||
XLexer lexer = new XLexer(input);
|
||||
XParser parser = new XParser(new CommonTokenStream(lexer));
|
||||
parser.setTreeAdaptor(xTreeAdaptor);
|
||||
CommonTree tree = parser.program().getTree();
|
||||
|
||||
//TODO Weitere Stufen Aufrufen
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package de.dhbw.compiler.antlrxcompiler;
|
||||
|
||||
public class Symbol {
|
||||
|
||||
public String name = "";
|
||||
public XType type = XType.NoType;
|
||||
public boolean read = false;
|
||||
public boolean print = false;
|
||||
public boolean isConst = false;
|
||||
|
||||
public Symbol(String name, XType type) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Symbol(String name, XType type, boolean read, boolean print) {
|
||||
this(name, type);
|
||||
this.read = read;
|
||||
this.print = print;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
String res = "["+name+","+type;
|
||||
if (read) { res+=",read"; }
|
||||
if (print) { res+=",print"; }
|
||||
if (isConst) { res+=",const"; }
|
||||
res += "]";
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package de.dhbw.compiler.antlrxcompiler;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class SymbolTable extends HashMap<String, Symbol>{
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/* Singleton */
|
||||
private static SymbolTable instance = new SymbolTable();
|
||||
|
||||
private SymbolTable() {}
|
||||
|
||||
public static SymbolTable getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer res = new StringBuffer();
|
||||
for (Symbol s: this.values()) { res.append(s.toString()+"\n"); }
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 = XTree;
|
||||
}
|
||||
|
||||
// AST-Tokens
|
||||
tokens {
|
||||
DECL;
|
||||
STATLIST;
|
||||
DECLLIST;
|
||||
UMINUS;
|
||||
}
|
||||
|
||||
@parser::header {package de.dhbw.compiler.antlrxcompiler;}
|
||||
@lexer::header {package de.dhbw.compiler.antlrxcompiler;}
|
||||
|
||||
// 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
@@ -0,0 +1,63 @@
|
||||
tree grammar XOptimizer;
|
||||
|
||||
options {
|
||||
language = Java;
|
||||
output = AST;
|
||||
tokenVocab = XTypeCheck;
|
||||
ASTLabelType = XTree;
|
||||
filter=true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@header {
|
||||
package de.dhbw.compiler.antlrxcompiler;
|
||||
}
|
||||
|
||||
@members {
|
||||
private String addString(String first, String last) {
|
||||
return first.substring(0, first.length()-1)+last.substring(1);
|
||||
}
|
||||
|
||||
private String opInt(String first, String last, char op) {
|
||||
int firstValue = 0;
|
||||
int lastValue = 0;
|
||||
try {
|
||||
firstValue = Integer.parseInt(first);
|
||||
lastValue = Integer.parseInt(last);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
switch (op) {
|
||||
case '+': return Integer.toString(firstValue+lastValue);
|
||||
case '-': return Integer.toString(firstValue-lastValue);
|
||||
case '*': return Integer.toString(firstValue*lastValue);
|
||||
case '/': return Integer.toString(firstValue/lastValue);
|
||||
default: return "";
|
||||
}
|
||||
}
|
||||
|
||||
private String opFloat(String first, String last, char op) {
|
||||
double firstValue = 0.0;
|
||||
double lastValue = 0.0;
|
||||
try {
|
||||
firstValue = Double.parseDouble(first);
|
||||
lastValue = Double.parseDouble(last);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
switch (op) {
|
||||
case '+': return Double.toString(firstValue+lastValue);
|
||||
case '-': return Double.toString(firstValue-lastValue);
|
||||
case '*': return Double.toString(firstValue*lastValue);
|
||||
case '/': return Double.toString(firstValue/lastValue);
|
||||
default: return "";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
bottomup: ;
|
||||
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
// $ANTLR 3.5.2 C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr X Uebersetzer-Leer\\src\\de\\dhbw\\compiler\\antlrxcompiler\\XOptimizer.g 2019-05-23 12:03:27
|
||||
|
||||
package de.dhbw.compiler.antlrxcompiler;
|
||||
|
||||
|
||||
import org.antlr.runtime.*;
|
||||
import org.antlr.runtime.tree.*;
|
||||
import java.util.Stack;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public class XOptimizer extends TreeRewriter {
|
||||
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;
|
||||
public static final int PLUS=23;
|
||||
|
||||
// delegates
|
||||
public TreeRewriter[] getDelegates() {
|
||||
return new TreeRewriter[] {};
|
||||
}
|
||||
|
||||
// delegators
|
||||
|
||||
|
||||
public XOptimizer(TreeNodeStream input) {
|
||||
this(input, new RecognizerSharedState());
|
||||
}
|
||||
public XOptimizer(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 XOptimizer.tokenNames; }
|
||||
@Override public String getGrammarFileName() { return "C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr X Uebersetzer-Leer\\src\\de\\dhbw\\compiler\\antlrxcompiler\\XOptimizer.g"; }
|
||||
|
||||
|
||||
private String addString(String first, String last) {
|
||||
return first.substring(0, first.length()-1)+last.substring(1);
|
||||
}
|
||||
|
||||
private String opInt(String first, String last, char op) {
|
||||
int firstValue = 0;
|
||||
int lastValue = 0;
|
||||
try {
|
||||
firstValue = Integer.parseInt(first);
|
||||
lastValue = Integer.parseInt(last);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
switch (op) {
|
||||
case '+': return Integer.toString(firstValue+lastValue);
|
||||
case '-': return Integer.toString(firstValue-lastValue);
|
||||
case '*': return Integer.toString(firstValue*lastValue);
|
||||
case '/': return Integer.toString(firstValue/lastValue);
|
||||
default: return "";
|
||||
}
|
||||
}
|
||||
|
||||
private String opFloat(String first, String last, char op) {
|
||||
double firstValue = 0.0;
|
||||
double lastValue = 0.0;
|
||||
try {
|
||||
firstValue = Double.parseDouble(first);
|
||||
lastValue = Double.parseDouble(last);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
switch (op) {
|
||||
case '+': return Double.toString(firstValue+lastValue);
|
||||
case '-': return Double.toString(firstValue-lastValue);
|
||||
case '*': return Double.toString(firstValue*lastValue);
|
||||
case '/': return Double.toString(firstValue/lastValue);
|
||||
default: return "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static class bottomup_return extends TreeRuleReturnScope {
|
||||
XTree tree;
|
||||
@Override
|
||||
public XTree getTree() { return tree; }
|
||||
};
|
||||
|
||||
|
||||
// $ANTLR start "bottomup"
|
||||
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr X Uebersetzer-Leer\\src\\de\\dhbw\\compiler\\antlrxcompiler\\XOptimizer.g:61:1: bottomup :;
|
||||
@Override
|
||||
public final XOptimizer.bottomup_return bottomup() throws RecognitionException {
|
||||
XOptimizer.bottomup_return retval = new XOptimizer.bottomup_return();
|
||||
retval.start = input.LT(1);
|
||||
|
||||
XTree root_0 = null;
|
||||
|
||||
XTree _first_0 = null;
|
||||
XTree _last = null;
|
||||
|
||||
|
||||
try {
|
||||
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr X Uebersetzer-Leer\\src\\de\\dhbw\\compiler\\antlrxcompiler\\XOptimizer.g:61:9: ()
|
||||
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr X Uebersetzer-Leer\\src\\de\\dhbw\\compiler\\antlrxcompiler\\XOptimizer.g:61:11:
|
||||
{
|
||||
if ( state.backtracking==1 ) {
|
||||
retval.tree = _first_0;
|
||||
if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
|
||||
retval.tree = (XTree)adaptor.getParent(retval.tree);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
finally {
|
||||
// do for sure before leaving
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
// $ANTLR end "bottomup"
|
||||
|
||||
// Delegated rules
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
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
|
||||
PLUS=23
|
||||
'('=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
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,32 @@
|
||||
package de.dhbw.compiler.antlrxcompiler;
|
||||
|
||||
import org.antlr.runtime.Token;
|
||||
import org.antlr.runtime.tree.CommonTree;
|
||||
|
||||
|
||||
public class XTree extends CommonTree {
|
||||
|
||||
public XType exprType = XType.NoType;
|
||||
|
||||
public XTree() {
|
||||
super();
|
||||
};
|
||||
|
||||
public XTree(Token t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
public XTree(CommonTree t ) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String s = super.toString();
|
||||
if (exprType!=XType.NoType) {
|
||||
s+="<"+exprType+">";
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package de.dhbw.compiler.antlrxcompiler;
|
||||
|
||||
import org.antlr.runtime.CommonToken;
|
||||
import org.antlr.runtime.Token;
|
||||
import org.antlr.runtime.tree.CommonTreeAdaptor;
|
||||
|
||||
|
||||
public class XTreeAdaptor extends CommonTreeAdaptor {
|
||||
|
||||
@Override
|
||||
public Object create(Token token) {
|
||||
return new XTree(token);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object dupNode(Object t) {
|
||||
if ( t==null ) {
|
||||
return null;
|
||||
}
|
||||
return create(((XTree)t).token);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object create(int tokenType, String text) {
|
||||
return create(tokenType, null, text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object create(int tokenType, Token fromToken) {
|
||||
return create(tokenType, fromToken, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object create(int tokenType, Token fromToken, String text) {
|
||||
if (fromToken==null) {
|
||||
fromToken = new CommonToken(tokenType, text);
|
||||
} else {
|
||||
fromToken.setType(tokenType);
|
||||
fromToken.setText(text);
|
||||
}
|
||||
return new XTree(fromToken);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package de.dhbw.compiler.antlrxcompiler;
|
||||
|
||||
public enum XType {
|
||||
NoType, InvalidType, IntType, FloatType, StringType;
|
||||
|
||||
public String toString() {
|
||||
switch (this) {
|
||||
case InvalidType: return "invalid";
|
||||
case IntType: return "int";
|
||||
case FloatType: return "float";
|
||||
case StringType: return "string";
|
||||
default: return "";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
tree grammar XTypeCheck;
|
||||
|
||||
options {
|
||||
language = Java;
|
||||
output = AST;
|
||||
tokenVocab = X;
|
||||
ASTLabelType = XTree;
|
||||
}
|
||||
|
||||
tokens{
|
||||
PLUS='+';
|
||||
}
|
||||
|
||||
@header {
|
||||
package de.dhbw.compiler.antlrxcompiler;
|
||||
|
||||
import java.util.HashMap;
|
||||
}
|
||||
|
||||
@members {
|
||||
|
||||
private SymbolTable symbols = SymbolTable.getInstance();
|
||||
|
||||
}
|
||||
|
||||
|
||||
program: 'todo';
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
// $ANTLR 3.5.2 /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XTypeCheck.g 2020-06-10 10:16:11
|
||||
|
||||
package de.dhbw.compiler.antlrxcompiler;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
|
||||
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 XTypeCheck 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;
|
||||
public static final int PLUS=23;
|
||||
|
||||
// delegates
|
||||
public TreeParser[] getDelegates() {
|
||||
return new TreeParser[] {};
|
||||
}
|
||||
|
||||
// delegators
|
||||
|
||||
|
||||
public XTypeCheck(TreeNodeStream input) {
|
||||
this(input, new RecognizerSharedState());
|
||||
}
|
||||
public XTypeCheck(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 XTypeCheck.tokenNames; }
|
||||
@Override public String getGrammarFileName() { return "/Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XTypeCheck.g"; }
|
||||
|
||||
|
||||
|
||||
private SymbolTable symbols = SymbolTable.getInstance();
|
||||
|
||||
|
||||
|
||||
public static class program_return extends TreeRuleReturnScope {
|
||||
XTree tree;
|
||||
@Override
|
||||
public XTree getTree() { return tree; }
|
||||
};
|
||||
|
||||
|
||||
// $ANTLR start "program"
|
||||
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XTypeCheck.g:27:1: program : 'todo' ;
|
||||
public final XTypeCheck.program_return program() throws RecognitionException {
|
||||
XTypeCheck.program_return retval = new XTypeCheck.program_return();
|
||||
retval.start = input.LT(1);
|
||||
|
||||
XTree root_0 = null;
|
||||
|
||||
XTree _first_0 = null;
|
||||
XTree _last = null;
|
||||
|
||||
|
||||
XTree string_literal1=null;
|
||||
|
||||
XTree string_literal1_tree=null;
|
||||
|
||||
try {
|
||||
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XTypeCheck.g:27:8: ( 'todo' )
|
||||
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XTypeCheck.g:27:15: 'todo'
|
||||
{
|
||||
root_0 = (XTree)adaptor.nil();
|
||||
|
||||
|
||||
_last = (XTree)input.LT(1);
|
||||
string_literal1=(XTree)match(input,46,FOLLOW_46_in_program82);
|
||||
string_literal1_tree = (XTree)adaptor.dupNode(string_literal1);
|
||||
|
||||
|
||||
adaptor.addChild(root_0, string_literal1_tree);
|
||||
|
||||
}
|
||||
|
||||
retval.tree = (XTree)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_program82 = new BitSet(new long[]{0x0000000000000002L});
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
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
|
||||
PLUS=23
|
||||
'('=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
|
||||
@@ -0,0 +1,21 @@
|
||||
tree grammar XtoJava;
|
||||
|
||||
options {
|
||||
language = Java;
|
||||
output = template;
|
||||
tokenVocab = XOptimizer;
|
||||
ASTLabelType = XTree;
|
||||
}
|
||||
|
||||
@header {
|
||||
package de.dhbw.compiler.antlrxcompiler;
|
||||
}
|
||||
|
||||
@members {
|
||||
|
||||
private SymbolTable symbols = SymbolTable.getInstance();
|
||||
|
||||
}
|
||||
|
||||
program: 'todo' -> template() "Hello World!";
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
// $ANTLR 3.5.2 C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr X Uebersetzer-Leer\\src\\de\\dhbw\\compiler\\antlrxcompiler\\XtoJava.g 2019-05-23 12:03:27
|
||||
|
||||
package de.dhbw.compiler.antlrxcompiler;
|
||||
|
||||
|
||||
import org.antlr.runtime.*;
|
||||
import org.antlr.runtime.tree.*;
|
||||
import java.util.Stack;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.antlr.stringtemplate.*;
|
||||
import org.antlr.stringtemplate.language.*;
|
||||
import java.util.HashMap;
|
||||
@SuppressWarnings("all")
|
||||
public class XtoJava 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;
|
||||
public static final int PLUS=23;
|
||||
|
||||
// delegates
|
||||
public TreeParser[] getDelegates() {
|
||||
return new TreeParser[] {};
|
||||
}
|
||||
|
||||
// delegators
|
||||
|
||||
|
||||
public XtoJava(TreeNodeStream input) {
|
||||
this(input, new RecognizerSharedState());
|
||||
}
|
||||
public XtoJava(TreeNodeStream input, RecognizerSharedState state) {
|
||||
super(input, state);
|
||||
}
|
||||
|
||||
protected StringTemplateGroup templateLib =
|
||||
new StringTemplateGroup("XtoJavaTemplates", AngleBracketTemplateLexer.class);
|
||||
|
||||
public void setTemplateLib(StringTemplateGroup templateLib) {
|
||||
this.templateLib = templateLib;
|
||||
}
|
||||
public StringTemplateGroup getTemplateLib() {
|
||||
return templateLib;
|
||||
}
|
||||
/** allows convenient multi-value initialization:
|
||||
* "new STAttrMap().put(...).put(...)"
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public static class STAttrMap extends HashMap<String, Object> {
|
||||
public STAttrMap put(String attrName, Object value) {
|
||||
super.put(attrName, value);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@Override public String[] getTokenNames() { return XtoJava.tokenNames; }
|
||||
@Override public String getGrammarFileName() { return "C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr X Uebersetzer-Leer\\src\\de\\dhbw\\compiler\\antlrxcompiler\\XtoJava.g"; }
|
||||
|
||||
|
||||
|
||||
private SymbolTable symbols = SymbolTable.getInstance();
|
||||
|
||||
|
||||
|
||||
public static class program_return extends TreeRuleReturnScope {
|
||||
public StringTemplate st;
|
||||
public Object getTemplate() { return st; }
|
||||
public String toString() { return st==null?null:st.toString(); }
|
||||
};
|
||||
|
||||
|
||||
// $ANTLR start "program"
|
||||
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr X Uebersetzer-Leer\\src\\de\\dhbw\\compiler\\antlrxcompiler\\XtoJava.g:20:1: program : 'todo' -> template( \"Hello World!\";
|
||||
public final XtoJava.program_return program() throws RecognitionException {
|
||||
XtoJava.program_return retval = new XtoJava.program_return();
|
||||
retval.start = input.LT(1);
|
||||
|
||||
try {
|
||||
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr X Uebersetzer-Leer\\src\\de\\dhbw\\compiler\\antlrxcompiler\\XtoJava.g:20:8: ( 'todo' -> template( \"Hello World!\")
|
||||
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr X Uebersetzer-Leer\\src\\de\\dhbw\\compiler\\antlrxcompiler\\XtoJava.g:20:15: 'todo'
|
||||
{
|
||||
match(input,46,FOLLOW_46_in_program69);
|
||||
// TEMPLATE REWRITE
|
||||
// 20:22: -> template( \"Hello World!\"
|
||||
{
|
||||
retval.st = new StringTemplate(templateLib, "Hello World!");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
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_program69 = new BitSet(new long[]{0x0000000000000002L});
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
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
|
||||
PLUS=23
|
||||
'('=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
|
||||
@@ -0,0 +1,51 @@
|
||||
/* **********************************************
|
||||
* 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.antlrxcompiler.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.tree.CommonTreeNodeStream;
|
||||
|
||||
import de.dhbw.compiler.antlrxcompiler.XLexer;
|
||||
import de.dhbw.compiler.antlrxcompiler.XOptimizer;
|
||||
import de.dhbw.compiler.antlrxcompiler.XParser;
|
||||
import de.dhbw.compiler.antlrxcompiler.XTree;
|
||||
import de.dhbw.compiler.antlrxcompiler.XTreeAdaptor;
|
||||
import de.dhbw.compiler.antlrxcompiler.XTypeCheck;
|
||||
|
||||
public abstract class OptimizerTest {
|
||||
|
||||
protected void testTypeCheckTree(String in, String expected) throws Exception {
|
||||
XTreeAdaptor xTreeAdaptor = new XTreeAdaptor();
|
||||
ANTLRInputStream input = new ANTLRInputStream(new ByteArrayInputStream(in.getBytes()));
|
||||
XLexer scanner = new XLexer(input);
|
||||
CommonTokenStream tokens = new CommonTokenStream(scanner);
|
||||
XParser parser = new XParser(tokens);
|
||||
parser.setTreeAdaptor(xTreeAdaptor);
|
||||
XTree out = parser.program().getTree();
|
||||
XOptimizer optimizer = new XOptimizer(new CommonTreeNodeStream(xTreeAdaptor, out));
|
||||
optimizer.setTreeAdaptor(xTreeAdaptor);
|
||||
out = (XTree)optimizer.downup(out,true);
|
||||
|
||||
if (out==null) {
|
||||
assertEquals(expected, out);
|
||||
} else {
|
||||
assertEquals(expected, out.toStringTree());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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.antlrxcompiler.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.antlrxcompiler.XLexer;
|
||||
import de.dhbw.compiler.antlrxcompiler.XParser;
|
||||
import de.dhbw.compiler.antlrxcompiler.XTreeAdaptor;
|
||||
|
||||
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);
|
||||
parser.setTreeAdaptor(new XTreeAdaptor());
|
||||
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 Typ-Pr<50>fung
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
package de.dhbw.compiler.antlrxcompiler.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,
|
||||
TestAntlrXOptimizer1.class})
|
||||
public class TestAntlrXOptimizer {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,401 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Testf<74>lle für Optimierer
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
package de.dhbw.compiler.antlrxcompiler.test;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runners.MethodSorters;
|
||||
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class TestAntlrXOptimizer1 extends OptimizerTest {
|
||||
|
||||
@Test
|
||||
public void optimize00() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : int;\n"+
|
||||
"begin\n"+
|
||||
" x:=0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x int)) (STATLIST (:= x 0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize01() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : int;\n"+
|
||||
"begin\n"+
|
||||
" x:=1+2;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x int)) (STATLIST (:= x 3)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void optimize02() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : int;\n"+
|
||||
"begin\n"+
|
||||
" x:=1-2;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x int)) (STATLIST (:= x -1)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void optimize03() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : int;\n"+
|
||||
"begin\n"+
|
||||
" x:=1*2;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x int)) (STATLIST (:= x 2)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize04() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : int;\n"+
|
||||
"begin\n"+
|
||||
" x:=1/2;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x int)) (STATLIST (:= x 0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize05() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : int;\n"+
|
||||
"begin\n"+
|
||||
" x:=1+-2;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x int)) (STATLIST (:= x -1)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize06() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : int;\n"+
|
||||
"begin\n"+
|
||||
" x:=1+12*(4-5)/6+7-(8+-9);\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x int)) (STATLIST (:= x 7)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize10() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
"begin\n"+
|
||||
" x:=0.0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:= x 0.0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize11() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
"begin\n"+
|
||||
" x:=1.0+2.0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:= x 3.0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void optimize12() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
"begin\n"+
|
||||
" x:=1.0-2.0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:= x -1.0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void optimize13() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
"begin\n"+
|
||||
" x:=1.0*2.0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:= x 2.0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize14() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
"begin\n"+
|
||||
" x:=1.0/2.0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:= x 0.5)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize15() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
"begin\n"+
|
||||
" x:=1.0+-2.0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:= x -1.0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize16() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
"begin\n"+
|
||||
" x:=1.0+12.0*(4.0-5.0)/6.0+7.0-(8.0+-9.0);\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:= x 7.0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize30() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
"begin\n"+
|
||||
" x:=1+2.0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:= x 3.0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize31() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
"begin\n"+
|
||||
" x:=1.0+2;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:= x 3.0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize32() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
"begin\n"+
|
||||
" x:=1-2.0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:= x -1.0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize33() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
"begin\n"+
|
||||
" x:=1.0-2;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:= x -1.0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize34() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
"begin\n"+
|
||||
" x:=1*2.0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:= x 2.0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize35() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
"begin\n"+
|
||||
" x:=1.0*2;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:= x 2.0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize36() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
"begin\n"+
|
||||
" x:=1/2.0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:= x 0.5)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize37() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
"begin\n"+
|
||||
" x:=1.0/2;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:= x 0.5)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize38() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
"begin\n"+
|
||||
" x:=1.0+12*(4.0-5)/6.0+7-(8.0+-9);\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:= x 7.0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize40() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
" y : int;\n"+
|
||||
"begin\n"+
|
||||
" x:=1+y;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float) (DECL y int)) (STATLIST (:= x (+ 1 y))))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize41() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
" y : int;\n"+
|
||||
"begin\n"+
|
||||
" x:=1+3+y+5*6;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float) (DECL y int)) (STATLIST (:= x (+ (+ 4 y) 30))))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize50() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
" y : int;\n"+
|
||||
"begin\n"+
|
||||
" x:=0*y;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float) (DECL y int)) (STATLIST (:= x 0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize51() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
" y : int;\n"+
|
||||
"begin\n"+
|
||||
" x:=y*0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float) (DECL y int)) (STATLIST (:= x 0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize52() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
" y : int;\n"+
|
||||
"begin\n"+
|
||||
" x:=(1+y)*0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float) (DECL y int)) (STATLIST (:= x 0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize53() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
" y : int;\n"+
|
||||
"begin\n"+
|
||||
" x:=0*(1-y);\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float) (DECL y int)) (STATLIST (:= x 0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize54() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
" y : int;\n"+
|
||||
"begin\n"+
|
||||
" x:=(1*y)*0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float) (DECL y int)) (STATLIST (:= x 0)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optimize55() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x : float;\n"+
|
||||
" y : int;\n"+
|
||||
"begin\n"+
|
||||
" x:=0*(1/y);\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float) (DECL y int)) (STATLIST (:= x 0)))";
|
||||
testTypeCheckTree(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 0) (if (< y x) (:= y (+ y 3)) (:= y (+ y 4)))) "
|
||||
+ "(STATLIST (if (< y x) (:= y (+ y 3))) (:= x (/ (* y (- y x)) y)))) (:= y (* x y))))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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.antlrxcompiler.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 {
|
||||
|
||||
}
|
||||
@@ -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.antlrxcompiler.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.antlrxcompiler.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.antlrxcompiler.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.antlrxcompiler.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.antlrxcompiler.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 Typ-Pr<50>fung
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
package de.dhbw.compiler.antlrxcompiler.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,
|
||||
TestAntlrXTypeCheck1.class})
|
||||
public class TestAntlrXTypeCheck {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,488 @@
|
||||
/* **********************************************
|
||||
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||
* Prof. Dr. Jörn Eisenbiegler
|
||||
*
|
||||
* Vorlesung Übersetzerbau
|
||||
* Praxis ANTLR-Parser für X
|
||||
* - Testf<74>lle für Typ-Pr<50>fung
|
||||
*
|
||||
* **********************************************
|
||||
*/
|
||||
|
||||
package de.dhbw.compiler.antlrxcompiler.test;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runners.MethodSorters;
|
||||
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class TestAntlrXTypeCheck1 extends TypeCheckTest {
|
||||
|
||||
@Test
|
||||
public void typecheck00Undefined() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
"begin\n"+
|
||||
" x:=0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd DECLLIST (STATLIST (:=<invalid> x<invalid> 0<int>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void typecheck11AssignIntValid1() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x:=0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x int)) (STATLIST (:=<int> x<int> 0<int>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck12AssignIntValid2() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x: int;"+
|
||||
" y: int;"+
|
||||
"begin\n"+
|
||||
" x:=y;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x int) (DECL y int)) (STATLIST (:=<int> x<int> y<int>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck13AssignIntInvalid1() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x:=0.0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x int)) (STATLIST (:=<invalid> x<int> 0.0<float>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck14AssignIntInvalid2() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x:=\"Hello\";\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x int)) (STATLIST (:=<invalid> x<int> \"Hello\"<string>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void typecheck15AssignIntInvalid3() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x:=y;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x int)) (STATLIST (:=<invalid> x<int> y<invalid>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck16AssignIntInvalid4() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x: int;"+
|
||||
" y: float;"+
|
||||
"begin\n"+
|
||||
" x:=y;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x int) (DECL y float)) (STATLIST (:=<invalid> x<int> y<float>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck20AssignFloatValid1() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x: float;"+
|
||||
"begin\n"+
|
||||
" x:=0.0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:=<float> x<float> 0.0<float>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck21AssignFloatValid2() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x: float;"+
|
||||
" y: float;"+
|
||||
"begin\n"+
|
||||
" x:=y;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float) (DECL y float)) (STATLIST (:=<float> x<float> y<float>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck22AssignFloatValid3() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x: float;"+
|
||||
"begin\n"+
|
||||
" x:=0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:=<float> x<float> 0<int>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck22AssignFloatInvalid2() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x: float;"+
|
||||
"begin\n"+
|
||||
" x:=\"Hello\";\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:=<invalid> x<float> \"Hello\"<string>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void typecheck23AssignFloatInvalid3() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x: float;"+
|
||||
"begin\n"+
|
||||
" x:=y;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float)) (STATLIST (:=<invalid> x<float> y<invalid>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck24AssignFloatInvalid4() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x: float;"+
|
||||
" y: string;"+
|
||||
"begin\n"+
|
||||
" x:=y;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x float) (DECL y string)) (STATLIST (:=<invalid> x<float> y<string>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck30AssignStringValid1() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x: string;"+
|
||||
"begin\n"+
|
||||
" x:=\"Hello\";\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x string)) (STATLIST (:=<string> x<string> \"Hello\"<string>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck31AssignStringValid2() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x: string;"+
|
||||
" y: string;"+
|
||||
"begin\n"+
|
||||
" x:=y;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x string) (DECL y string)) (STATLIST (:=<string> x<string> y<string>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck32AssignStringInvalid1() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x: string;"+
|
||||
"begin\n"+
|
||||
" x:=0.0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x string)) (STATLIST (:=<invalid> x<string> 0.0<float>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck33AssignStringInvalid2() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x: string;"+
|
||||
"begin\n"+
|
||||
" x:=0;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x string)) (STATLIST (:=<invalid> x<string> 0<int>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void typecheck34AssignStringInvalid3() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x: string;"+
|
||||
"begin\n"+
|
||||
" x:=y;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x string)) (STATLIST (:=<invalid> x<string> y<invalid>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck35AssignStringInvalid4() throws Exception {
|
||||
String test = "program beginEnd;\n"+
|
||||
" x: string;"+
|
||||
" y: float;"+
|
||||
"begin\n"+
|
||||
" x:=y;\n"+
|
||||
"end.";
|
||||
String expected = "(program beginEnd (DECLLIST (DECL x string) (DECL y float)) (STATLIST (:=<invalid> x<string> y<float>)))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck40ExprPlus1() throws Exception {
|
||||
String test = "program ExprPlus;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0+1;"+
|
||||
"end.";
|
||||
String expected = "(program ExprPlus (DECLLIST (DECL x int)) (STATLIST (:=<int> x<int> (+<int> 0<int> 1<int>))))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck41ExprPlus2() throws Exception {
|
||||
String test = "program ExprPlus;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=0+1.0;"+
|
||||
"end.";
|
||||
String expected = "(program ExprPlus (DECLLIST (DECL x int)) (STATLIST (:=<invalid> x<int> (+<float> 0<int> 1.0<float>))))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck42ExprPlus3() throws Exception {
|
||||
String test = "program ExprPlus;\n"+
|
||||
" x: int;"+
|
||||
"begin\n"+
|
||||
" x :=\"Hello\"+1;"+
|
||||
"end.";
|
||||
String expected = "(program ExprPlus (DECLLIST (DECL x int)) (STATLIST (:=<invalid> x<int> (+<invalid> \"Hello\"<string> 1<int>))))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck43ExprPlus5() throws Exception {
|
||||
String test = "program ExprPlus;\n"+
|
||||
" x: float;"+
|
||||
"begin\n"+
|
||||
" x :=0+1;"+
|
||||
"end.";
|
||||
String expected = "(program ExprPlus (DECLLIST (DECL x float)) (STATLIST (:=<float> x<float> (+<int> 0<int> 1<int>))))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck44ExprPlus6() throws Exception {
|
||||
String test = "program ExprPlus;\n"+
|
||||
" x: float;"+
|
||||
"begin\n"+
|
||||
" x :=0+1.0;"+
|
||||
"end.";
|
||||
String expected = "(program ExprPlus (DECLLIST (DECL x float)) (STATLIST (:=<float> x<float> (+<float> 0<int> 1.0<float>))))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck45ExprPlus7() throws Exception {
|
||||
String test = "program ExprPlus;\n"+
|
||||
" x: float;"+
|
||||
"begin\n"+
|
||||
" x :=0.0+1.0;"+
|
||||
"end.";
|
||||
String expected = "(program ExprPlus (DECLLIST (DECL x float)) (STATLIST (:=<float> x<float> (+<float> 0.0<float> 1.0<float>))))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typecheck46ExprPlus8() throws Exception {
|
||||
String test = "program ExprPlus;\n"+
|
||||
" x: string;"+
|
||||
"begin\n"+
|
||||
" x :=\"Hello\"+\"World\";"+
|
||||
"end.";
|
||||
String expected = "(program ExprPlus (DECLLIST (DECL x string)) (STATLIST (:=<string> x<string> (+<string> \"Hello\"<string> \"World\"<string>))))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void program13ExprMul() throws Exception {
|
||||
String test = "program exprMul;\n"+
|
||||
" x: float;"+
|
||||
"begin\n"+
|
||||
" x :=0*1;"+
|
||||
"end.";
|
||||
String expected = "(program exprMul (DECLLIST (DECL x float)) (STATLIST (:=<float> x<float> (*<int> 0<int> 1<int>))))";
|
||||
testTypeCheckTree(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 (:=<int> x<int> (/<int> 0<int> 1<int>))))";
|
||||
testTypeCheckTree(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 (:=<int> x<int> (-<int> 0<int> (UMINUS<int> 1<int>)))))";
|
||||
testTypeCheckTree(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 (:=<int> x<int> (+<int> (-<int> (+<int> (+<int> 0<int> 1<int>) (UMINUS<int> 2<int>)) 3<int>) 4<int>))))";
|
||||
testTypeCheckTree(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 (:=<int> x<int> (*<int> (*<int> (/<int> (*<int> 0<int> 1<int>) 2<int>) (UMINUS<int> 3<int>)) 4<int>))))";
|
||||
testTypeCheckTree(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 (:=<int> x<int> (+<int> (*<int> 0<int> 1<int>) (*<int> (*<int> (/<int> 2<int> (UMINUS<int> 3<int>)) (+<int> (-<int> 4<int> 5<int>) 6<int>)) 7<int>)))))";
|
||||
testTypeCheckTree(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 (:=<int> x<int> (+<int> (*<int> 0<int> x<int>) y<int>))))";
|
||||
testTypeCheckTree(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 (<<int> 2<int> 3<int>) (:=<int> x<int> 1<int>))))";
|
||||
testTypeCheckTree(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 (><int> 2<int> 3<int>) (:=<int> x<int> 1<int>) (:=<int> x<int> 2<int>))))";
|
||||
testTypeCheckTree(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 (=<int> 2<int> 3<int>) (if (<<int> 4<int> 5<int>) (:=<int> x<int> 1<int>) (:=<int> x<int> 2<int>)) (:=<int> x<int> 3<int>))))";
|
||||
testTypeCheckTree(test, expected);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void program23CondElse3() throws Exception {
|
||||
String test = "program condelse3;\n"+
|
||||
" x: float;"+
|
||||
"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 float)) "
|
||||
+ "(STATLIST (if (<<int> 2<int> 3<int>) (if (><int> 4<int> 5<int>) (:=<float> x<float> 1<int>) (if (=<int> 6<int> 7<int>) (:=<float> x<float> 2<int>) (:=<float> x<float> 3<int>))))))";
|
||||
testTypeCheckTree(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 (<<int> 2<int> 3<int>) (if (><int> 4<int> 5<int>) (:=<int> x<int> 1<int>) (:=<int> x<int> 2<int>)) (if (=<int> 6<int> 7<int>) (:=<int> x<int> 3<int>) (:=<int> x<int> 4<int>)))))";
|
||||
testTypeCheckTree(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 (:=<int> y<int> (-<int> (+<int> 25<int> (*<int> 2<int> x<int>)) (*<int> 6<int> x<int>))) (if (<<int> x<int> y<int>) "
|
||||
+ "(STATLIST (:=<int> x<int> (*<int> (/<int> (UMINUS<int> 3<int>) 6<int>) (+<int> y<int> 2<int>))) (if (<<int> y<int> x<int>) (:=<int> y<int> (+<int> y<int> 3<int>)) (:=<int> y<int> (+<int> y<int> 4<int>)))) "
|
||||
+ "(STATLIST (if (<<int> y<int> x<int>) (:=<int> y<int> (+<int> y<int> 3<int>))) (:=<int> x<int> (/<int> (*<int> y<int> (-<int> y<int> x<int>)) y<int>)))) (:=<int> y<int> (*<int> x<int> y<int>))))";
|
||||
testTypeCheckTree(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.antlrxcompiler.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,91 @@
|
||||
/* **********************************************
|
||||
* 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.antlrxcompiler.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.antlrxcompiler.XLexer;
|
||||
import de.dhbw.compiler.antlrxcompiler.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,50 @@
|
||||
/* **********************************************
|
||||
* 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.antlrxcompiler.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.tree.CommonTreeNodeStream;
|
||||
|
||||
import de.dhbw.compiler.antlrxcompiler.XLexer;
|
||||
import de.dhbw.compiler.antlrxcompiler.XParser;
|
||||
import de.dhbw.compiler.antlrxcompiler.XTree;
|
||||
import de.dhbw.compiler.antlrxcompiler.XTreeAdaptor;
|
||||
import de.dhbw.compiler.antlrxcompiler.XTypeCheck;
|
||||
|
||||
public abstract class TypeCheckTest {
|
||||
|
||||
protected void testTypeCheckTree(String in, String expected) throws Exception {
|
||||
XTreeAdaptor xTreeAdaptor = new XTreeAdaptor();
|
||||
ANTLRInputStream input = new ANTLRInputStream(new ByteArrayInputStream(in.getBytes()));
|
||||
XLexer scanner = new XLexer(input);
|
||||
CommonTokenStream tokens = new CommonTokenStream(scanner);
|
||||
XParser parser = new XParser(tokens);
|
||||
parser.setTreeAdaptor(xTreeAdaptor);
|
||||
XTree out = parser.program().getTree();
|
||||
XTypeCheck typecheck = new XTypeCheck(new CommonTreeNodeStream(xTreeAdaptor, out));
|
||||
typecheck.setTreeAdaptor(xTreeAdaptor);
|
||||
out = typecheck.program().getTree();
|
||||
|
||||
if (out==null) {
|
||||
assertEquals(expected, out);
|
||||
} else {
|
||||
assertEquals(expected, out.toStringTree());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user