[11 - Antlr Parser] Add "CC-Praxis-Antlr Parser fuer X-Leer"
This commit is contained in:
BIN
CC-Praxis-Antlr Parser fuer X-Leer/lib/antlr-3.5.2-complete.jar
Normal file
BIN
CC-Praxis-Antlr Parser fuer X-Leer/lib/antlr-3.5.2-complete.jar
Normal file
Binary file not shown.
@@ -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,81 @@
|
|||||||
|
/* **********************************************
|
||||||
|
* 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.antlrxparser;
|
||||||
|
|
||||||
|
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.DOTTreeGenerator;
|
||||||
|
import org.antlr.runtime.tree.Tree;
|
||||||
|
import org.antlr.stringtemplate.StringTemplate;
|
||||||
|
|
||||||
|
public class AntlrXParserMain {
|
||||||
|
|
||||||
|
public static void saveToGrapvizDOT(Tree tree, String name) throws FileNotFoundException {
|
||||||
|
StringTemplate dot = (new DOTTreeGenerator()).toDOT(tree);
|
||||||
|
PrintWriter out = new PrintWriter(name);
|
||||||
|
out.println(dot.toString());
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final String TEST =
|
||||||
|
"program test2;\n"+
|
||||||
|
" x : int;\n"+
|
||||||
|
" y : float;\n"+
|
||||||
|
" z : string;\n"+
|
||||||
|
"begin\n"+
|
||||||
|
" x := 4+5+6.2;\n"+
|
||||||
|
" y := 3.56+1.2e3+45.e-67+4e34+3E-1;\n"+
|
||||||
|
" z := \"Hello \\\"World\\\"\" + \":\";\n"+
|
||||||
|
" z := \"Peter\" + 4;\n"+
|
||||||
|
" if 2<3 then abc := 5 else abc := 7;\n"+
|
||||||
|
" while (x>y) y := y+1;\n"+
|
||||||
|
" begin\n"+
|
||||||
|
" for (x:=1; x<6; x:=x+1) y:=y+2;\n"+
|
||||||
|
" end;\n"+
|
||||||
|
"end.";
|
||||||
|
|
||||||
|
private static final String TEST2 =
|
||||||
|
"program test2;\n"+
|
||||||
|
"begin\n"+
|
||||||
|
" x := 4-5*6*7+8/9;\n"+
|
||||||
|
"end.";
|
||||||
|
|
||||||
|
private static final String BEISPIELFOLIEN =
|
||||||
|
"program test5;\n"+
|
||||||
|
" read x : int;\n"+
|
||||||
|
" print y : float;\n"+
|
||||||
|
" z : int;\n"+
|
||||||
|
"begin\n"+
|
||||||
|
" while (x<4) begin\n"+
|
||||||
|
" for (z:=0; z<4; z:=z+1) x:=x+2;\n"+
|
||||||
|
" if x=4 then begin\n"+
|
||||||
|
" x:=z*(x+2);\n"+
|
||||||
|
" x:=x+10;\n"+
|
||||||
|
" end else y:=100.e-3;\n"+
|
||||||
|
" end;\n"+
|
||||||
|
"end.\n";
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
|
ANTLRInputStream input = new ANTLRInputStream(new ByteArrayInputStream(BEISPIELFOLIEN.getBytes()));
|
||||||
|
|
||||||
|
//TODO
|
||||||
|
// - Scanner aufrufen
|
||||||
|
// - Parser aufrufen
|
||||||
|
// - Text- und Grapviz-Form ausgeben/speichern
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
/* **********************************************
|
||||||
|
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||||
|
* Prof. Dr. Jörn Eisenbiegler
|
||||||
|
*
|
||||||
|
* Vorlesung Übersetzerbau
|
||||||
|
* Praxis ANTLR-Parser für X
|
||||||
|
* - Grammatik für Scanner und Parser
|
||||||
|
*
|
||||||
|
* **********************************************
|
||||||
|
*/
|
||||||
|
|
||||||
|
grammar X;
|
||||||
|
|
||||||
|
options {
|
||||||
|
language = Java;
|
||||||
|
output = AST;
|
||||||
|
}
|
||||||
|
|
||||||
|
// AST-Tokens
|
||||||
|
tokens {
|
||||||
|
DECL;
|
||||||
|
STATLIST;
|
||||||
|
DECLLIST;
|
||||||
|
UMINUS;
|
||||||
|
}
|
||||||
|
|
||||||
|
@parser::header {package de.dhbw.compiler.antlrxparser;}
|
||||||
|
@lexer::header {package de.dhbw.compiler.antlrxparser;}
|
||||||
|
|
||||||
|
|
||||||
|
INVALID: .;
|
||||||
|
|
||||||
|
program: 'TODO';
|
||||||
@@ -0,0 +1,122 @@
|
|||||||
|
// $ANTLR 3.5.2 C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Parser fuer X-Leer\\src\\de\\dhbw\\compiler\\antlrxparser\\X.g 2019-05-16 16:03:08
|
||||||
|
package de.dhbw.compiler.antlrxparser;
|
||||||
|
|
||||||
|
import org.antlr.runtime.*;
|
||||||
|
import java.util.Stack;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
@SuppressWarnings("all")
|
||||||
|
public class XLexer extends Lexer {
|
||||||
|
public static final int EOF=-1;
|
||||||
|
public static final int T__9=9;
|
||||||
|
public static final int DECL=4;
|
||||||
|
public static final int DECLLIST=5;
|
||||||
|
public static final int INVALID=6;
|
||||||
|
public static final int STATLIST=7;
|
||||||
|
public static final int UMINUS=8;
|
||||||
|
|
||||||
|
// delegates
|
||||||
|
// delegators
|
||||||
|
public Lexer[] getDelegates() {
|
||||||
|
return new Lexer[] {};
|
||||||
|
}
|
||||||
|
|
||||||
|
public XLexer() {}
|
||||||
|
public XLexer(CharStream input) {
|
||||||
|
this(input, new RecognizerSharedState());
|
||||||
|
}
|
||||||
|
public XLexer(CharStream input, RecognizerSharedState state) {
|
||||||
|
super(input,state);
|
||||||
|
}
|
||||||
|
@Override public String getGrammarFileName() { return "C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Parser fuer X-Leer\\src\\de\\dhbw\\compiler\\antlrxparser\\X.g"; }
|
||||||
|
|
||||||
|
// $ANTLR start "T__9"
|
||||||
|
public final void mT__9() throws RecognitionException {
|
||||||
|
try {
|
||||||
|
int _type = T__9;
|
||||||
|
int _channel = DEFAULT_TOKEN_CHANNEL;
|
||||||
|
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Parser fuer X-Leer\\src\\de\\dhbw\\compiler\\antlrxparser\\X.g:9:6: ( 'TODO' )
|
||||||
|
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Parser fuer X-Leer\\src\\de\\dhbw\\compiler\\antlrxparser\\X.g:9:8: 'TODO'
|
||||||
|
{
|
||||||
|
match("TODO");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
state.type = _type;
|
||||||
|
state.channel = _channel;
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
// do for sure before leaving
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// $ANTLR end "T__9"
|
||||||
|
|
||||||
|
// $ANTLR start "INVALID"
|
||||||
|
public final void mINVALID() throws RecognitionException {
|
||||||
|
try {
|
||||||
|
int _type = INVALID;
|
||||||
|
int _channel = DEFAULT_TOKEN_CHANNEL;
|
||||||
|
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Parser fuer X-Leer\\src\\de\\dhbw\\compiler\\antlrxparser\\X.g:31:8: ( . )
|
||||||
|
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Parser fuer X-Leer\\src\\de\\dhbw\\compiler\\antlrxparser\\X.g:31:11: .
|
||||||
|
{
|
||||||
|
matchAny();
|
||||||
|
}
|
||||||
|
|
||||||
|
state.type = _type;
|
||||||
|
state.channel = _channel;
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
// do for sure before leaving
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// $ANTLR end "INVALID"
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mTokens() throws RecognitionException {
|
||||||
|
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Parser fuer X-Leer\\src\\de\\dhbw\\compiler\\antlrxparser\\X.g:1:8: ( T__9 | INVALID )
|
||||||
|
int alt1=2;
|
||||||
|
int LA1_0 = input.LA(1);
|
||||||
|
if ( (LA1_0=='T') ) {
|
||||||
|
int LA1_1 = input.LA(2);
|
||||||
|
if ( (LA1_1=='O') ) {
|
||||||
|
alt1=1;
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
alt1=2;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else if ( ((LA1_0 >= '\u0000' && LA1_0 <= 'S')||(LA1_0 >= 'U' && LA1_0 <= '\uFFFF')) ) {
|
||||||
|
alt1=2;
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
NoViableAltException nvae =
|
||||||
|
new NoViableAltException("", 1, 0, input);
|
||||||
|
throw nvae;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (alt1) {
|
||||||
|
case 1 :
|
||||||
|
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Parser fuer X-Leer\\src\\de\\dhbw\\compiler\\antlrxparser\\X.g:1:10: T__9
|
||||||
|
{
|
||||||
|
mT__9();
|
||||||
|
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 2 :
|
||||||
|
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Parser fuer X-Leer\\src\\de\\dhbw\\compiler\\antlrxparser\\X.g:1:15: INVALID
|
||||||
|
{
|
||||||
|
mINVALID();
|
||||||
|
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
// $ANTLR 3.5.2 C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Parser fuer X-Leer\\src\\de\\dhbw\\compiler\\antlrxparser\\X.g 2019-05-16 16:03:08
|
||||||
|
package de.dhbw.compiler.antlrxparser;
|
||||||
|
|
||||||
|
import org.antlr.runtime.*;
|
||||||
|
import java.util.Stack;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import org.antlr.runtime.tree.*;
|
||||||
|
|
||||||
|
|
||||||
|
@SuppressWarnings("all")
|
||||||
|
public class XParser extends Parser {
|
||||||
|
public static final String[] tokenNames = new String[] {
|
||||||
|
"<invalid>", "<EOR>", "<DOWN>", "<UP>", "DECL", "DECLLIST", "INVALID",
|
||||||
|
"STATLIST", "UMINUS", "'TODO'"
|
||||||
|
};
|
||||||
|
public static final int EOF=-1;
|
||||||
|
public static final int T__9=9;
|
||||||
|
public static final int DECL=4;
|
||||||
|
public static final int DECLLIST=5;
|
||||||
|
public static final int INVALID=6;
|
||||||
|
public static final int STATLIST=7;
|
||||||
|
public static final int UMINUS=8;
|
||||||
|
|
||||||
|
// delegates
|
||||||
|
public Parser[] getDelegates() {
|
||||||
|
return new Parser[] {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// delegators
|
||||||
|
|
||||||
|
|
||||||
|
public XParser(TokenStream input) {
|
||||||
|
this(input, new RecognizerSharedState());
|
||||||
|
}
|
||||||
|
public XParser(TokenStream 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 XParser.tokenNames; }
|
||||||
|
@Override public String getGrammarFileName() { return "C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Parser fuer X-Leer\\src\\de\\dhbw\\compiler\\antlrxparser\\X.g"; }
|
||||||
|
|
||||||
|
|
||||||
|
public static class program_return extends ParserRuleReturnScope {
|
||||||
|
Object tree;
|
||||||
|
@Override
|
||||||
|
public Object getTree() { return tree; }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// $ANTLR start "program"
|
||||||
|
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Parser fuer X-Leer\\src\\de\\dhbw\\compiler\\antlrxparser\\X.g:33:1: program : 'TODO' ;
|
||||||
|
public final XParser.program_return program() throws RecognitionException {
|
||||||
|
XParser.program_return retval = new XParser.program_return();
|
||||||
|
retval.start = input.LT(1);
|
||||||
|
|
||||||
|
Object root_0 = null;
|
||||||
|
|
||||||
|
Token string_literal1=null;
|
||||||
|
|
||||||
|
Object string_literal1_tree=null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Parser fuer X-Leer\\src\\de\\dhbw\\compiler\\antlrxparser\\X.g:33:8: ( 'TODO' )
|
||||||
|
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Parser fuer X-Leer\\src\\de\\dhbw\\compiler\\antlrxparser\\X.g:33:10: 'TODO'
|
||||||
|
{
|
||||||
|
root_0 = (Object)adaptor.nil();
|
||||||
|
|
||||||
|
|
||||||
|
string_literal1=(Token)match(input,9,FOLLOW_9_in_program95);
|
||||||
|
string_literal1_tree = (Object)adaptor.create(string_literal1);
|
||||||
|
adaptor.addChild(root_0, string_literal1_tree);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
retval.stop = input.LT(-1);
|
||||||
|
|
||||||
|
retval.tree = (Object)adaptor.rulePostProcessing(root_0);
|
||||||
|
adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop);
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (RecognitionException re) {
|
||||||
|
reportError(re);
|
||||||
|
recover(input,re);
|
||||||
|
retval.tree = (Object)adaptor.errorNode(input, retval.start, input.LT(-1), re);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
// do for sure before leaving
|
||||||
|
}
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
// $ANTLR end "program"
|
||||||
|
|
||||||
|
// Delegated rules
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static final BitSet FOLLOW_9_in_program95 = new BitSet(new long[]{0x0000000000000002L});
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
/* **********************************************
|
||||||
|
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||||
|
* Prof. Dr. Jörn Eisenbiegler
|
||||||
|
*
|
||||||
|
* Vorlesung Übersetzerbau
|
||||||
|
* Praxis ANTLR-Parser für X
|
||||||
|
* - Testfall-Utility für Parser
|
||||||
|
*
|
||||||
|
* **********************************************
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
package de.dhbw.compiler.antlrxparser.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.antlrxparser.XLexer;
|
||||||
|
import de.dhbw.compiler.antlrxparser.XParser;
|
||||||
|
|
||||||
|
public abstract class ParseTreeTest {
|
||||||
|
|
||||||
|
protected void testParseTree(String in, String expected) throws Exception {
|
||||||
|
ANTLRInputStream input = new ANTLRInputStream(new ByteArrayInputStream(in.getBytes()));
|
||||||
|
XLexer scanner = new XLexer(input);
|
||||||
|
CommonTokenStream tokens = new CommonTokenStream(scanner);
|
||||||
|
XParser parser = new XParser(tokens);
|
||||||
|
ParserRuleReturnScope result = parser.program();
|
||||||
|
CommonTree out = (CommonTree) result.getTree();
|
||||||
|
|
||||||
|
if (out==null) {
|
||||||
|
assertEquals(expected, out);
|
||||||
|
} else {
|
||||||
|
assertEquals(expected, out.toStringTree());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,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.antlrxparser.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.antlrxparser.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.antlrxparser.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.antlrxparser.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.antlrxparser.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.antlrxparser.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,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.antlrxparser.test;
|
||||||
|
|
||||||
|
import org.antlr.runtime.CommonToken;
|
||||||
|
|
||||||
|
public class TestToken extends CommonToken {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public TestToken(int type, String text, int line, int column) {
|
||||||
|
super(type, text);
|
||||||
|
super.setLine(line);
|
||||||
|
// Antlr beginnt mit 0, Kompatibilit<69>t zu anderen Tests
|
||||||
|
super.setCharPositionInLine(column-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
/* **********************************************
|
||||||
|
* Duale Hochschule Baden-Württemberg Karlsruhe
|
||||||
|
* Prof. Dr. Jörn Eisenbiegler
|
||||||
|
*
|
||||||
|
* Vorlesung Übersetzerbau
|
||||||
|
* Praxis ANTLR-Parser für X
|
||||||
|
* - Testfall-Utility für Scanner
|
||||||
|
*
|
||||||
|
* **********************************************
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
package de.dhbw.compiler.antlrxparser.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.antlrxparser.XLexer;
|
||||||
|
import de.dhbw.compiler.antlrxparser.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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user