diff --git a/CC-Praxis-Antlr Parser fuer X-Leer/lib/antlr-3.5.2-complete.jar b/CC-Praxis-Antlr Parser fuer X-Leer/lib/antlr-3.5.2-complete.jar new file mode 100644 index 0000000..260de76 Binary files /dev/null and b/CC-Praxis-Antlr Parser fuer X-Leer/lib/antlr-3.5.2-complete.jar differ diff --git a/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/AntlrX.tokens b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/AntlrX.tokens new file mode 100644 index 0000000..5e3d994 --- /dev/null +++ b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/AntlrX.tokens @@ -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 diff --git a/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/AntlrXParserMain.java b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/AntlrXParserMain.java new file mode 100644 index 0000000..2e46042 --- /dev/null +++ b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/AntlrXParserMain.java @@ -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 + + } +} diff --git a/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g new file mode 100644 index 0000000..107751e --- /dev/null +++ b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g @@ -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'; \ No newline at end of file diff --git a/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/XLexer.java b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/XLexer.java new file mode 100644 index 0000000..89cd809 --- /dev/null +++ b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/XLexer.java @@ -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; + + } + } + + + +} diff --git a/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/XParser.java b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/XParser.java new file mode 100644 index 0000000..019f92b --- /dev/null +++ b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/XParser.java @@ -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[] { + "", "", "", "", "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}); +} diff --git a/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/ParseTreeTest.java b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/ParseTreeTest.java new file mode 100644 index 0000000..fe72619 --- /dev/null +++ b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/ParseTreeTest.java @@ -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()); + } + } + +} diff --git a/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/TestAntlrXParser.java b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/TestAntlrXParser.java new file mode 100644 index 0000000..44b68bb --- /dev/null +++ b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/TestAntlrXParser.java @@ -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 { + +} diff --git a/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/TestAntlrXParser1.java b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/TestAntlrXParser1.java new file mode 100644 index 0000000..805e1de --- /dev/null +++ b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/TestAntlrXParser1.java @@ -0,0 +1,256 @@ +/* ********************************************** + * Duale Hochschule Baden-Württemberg Karlsruhe + * Prof. Dr. Jörn Eisenbiegler + * + * Vorlesung Übersetzerbau + * Praxis ANTLR-Parser für X + * - Testf�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 ", 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)); + } + +} diff --git a/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/TestAntlrXScanner2.java b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/TestAntlrXScanner2.java new file mode 100644 index 0000000..2f98de9 --- /dev/null +++ b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/TestAntlrXScanner2.java @@ -0,0 +1,66 @@ +/* ********************************************** + * Duale Hochschule Baden-Württemberg Karlsruhe + * Prof. Dr. Jörn Eisenbiegler + * + * Vorlesung Übersetzerbau + * Praxis ANTLR-Parser für X + * - Testf�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)); */ + } + +} diff --git a/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/TestAntlrXScanner3.java b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/TestAntlrXScanner3.java new file mode 100644 index 0000000..4194251 --- /dev/null +++ b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/TestAntlrXScanner3.java @@ -0,0 +1,110 @@ +/* ********************************************** + * Duale Hochschule Baden-Württemberg Karlsruhe + * Prof. Dr. Jörn Eisenbiegler + * + * Vorlesung Übersetzerbau + * Praxis ANTLR-Parser für X + * - Testf�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)); + } +} diff --git a/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/TestToken.java b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/TestToken.java new file mode 100644 index 0000000..18bdce8 --- /dev/null +++ b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/TestToken.java @@ -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�t zu anderen Tests + super.setCharPositionInLine(column-1); + } + +} diff --git a/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/TokenStreamTest.java b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/TokenStreamTest.java new file mode 100644 index 0000000..ea25f01 --- /dev/null +++ b/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/test/TokenStreamTest.java @@ -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) { + name = "'"+name.toLowerCase()+"'"; + for(res=0 ; 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()); + } + +}