[11 - Antlr Parser] Add global library and add implementation for "CC-Praxis-Antlr Parser fuer X-Leer"/AntlrXParseMain

This commit is contained in:
2020-05-27 12:07:14 +02:00
parent 7d7b787c80
commit 9a3e3cfe97
14 changed files with 362 additions and 238 deletions

View File

@@ -0,0 +1,7 @@
T__9=9
DECL=4
DECLLIST=5
INVALID=6
STATLIST=7
UMINUS=8
'TODO'=9

View File

@@ -69,13 +69,24 @@ public class AntlrXParserMain {
"end.\n"; "end.\n";
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
ANTLRInputStream input = new ANTLRInputStream(new ByteArrayInputStream(BEISPIELFOLIEN.getBytes()));
//TODO //TODO
// - Scanner aufrufen // - Scanner aufrufen
// - Parser aufrufen // - Parser aufrufen
// - Text- und Grapviz-Form ausgeben/speichern // - Text- und Grapviz-Form ausgeben/speichern
ANTLRInputStream input = new ANTLRInputStream(new ByteArrayInputStream(BEISPIELFOLIEN.getBytes()));
XLexer lexer = new XLexer(input);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
XParser parser = new XParser(tokenStream);
XParser.program_return result = parser.program();
CommonTree tree = (CommonTree) result.getTree();
graphizDotTree(tree, "XAST.dot");
}
public static void graphizDotTree(Tree tree, String name) throws FileNotFoundException {
StringTemplate template = new DOTTreeGenerator().toDOT(tree);
PrintWriter out = new PrintWriter(name);
out.println(template.toString());
out.close();
} }
} }

View File

@@ -27,7 +27,3 @@ tokens {
@parser::header {package de.dhbw.compiler.antlrxparser;} @parser::header {package de.dhbw.compiler.antlrxparser;}
@lexer::header {package de.dhbw.compiler.antlrxparser;} @lexer::header {package de.dhbw.compiler.antlrxparser;}
INVALID: .;
program: 'TODO';

View File

@@ -1,122 +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 // $ANTLR 3.5.2 /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g 2020-05-27 11:44:58
package de.dhbw.compiler.antlrxparser; package de.dhbw.compiler.antlrxparser;
import org.antlr.runtime.*; import org.antlr.runtime.*;
import java.util.Stack; import java.util.Stack;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
@SuppressWarnings("all") @SuppressWarnings("all")
public class XLexer extends Lexer { public class XLexer extends Lexer {
public static final int EOF=-1; public static final int EOF=-1;
public static final int T__9=9; public static final int T__9=9;
public static final int DECL=4; public static final int DECL=4;
public static final int DECLLIST=5; public static final int DECLLIST=5;
public static final int INVALID=6; public static final int INVALID=6;
public static final int STATLIST=7; public static final int STATLIST=7;
public static final int UMINUS=8; public static final int UMINUS=8;
// delegates // delegates
// delegators // delegators
public Lexer[] getDelegates() { public Lexer[] getDelegates() {
return new Lexer[] {}; return new Lexer[] {};
} }
public XLexer() {} public XLexer() {}
public XLexer(CharStream input) { public XLexer(CharStream input) {
this(input, new RecognizerSharedState()); this(input, new RecognizerSharedState());
} }
public XLexer(CharStream input, RecognizerSharedState state) { public XLexer(CharStream input, RecognizerSharedState state) {
super(input,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"; } @Override public String getGrammarFileName() { return "/Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g"; }
// $ANTLR start "T__9" // $ANTLR start "T__9"
public final void mT__9() throws RecognitionException { public final void mT__9() throws RecognitionException {
try { try {
int _type = T__9; int _type = T__9;
int _channel = DEFAULT_TOKEN_CHANNEL; 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' ) // /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/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' // /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:9:8: 'TODO'
{ {
match("TODO"); match("TODO");
} }
state.type = _type; state.type = _type;
state.channel = _channel; state.channel = _channel;
} }
finally { finally {
// do for sure before leaving // do for sure before leaving
} }
} }
// $ANTLR end "T__9" // $ANTLR end "T__9"
// $ANTLR start "INVALID" // $ANTLR start "INVALID"
public final void mINVALID() throws RecognitionException { public final void mINVALID() throws RecognitionException {
try { try {
int _type = INVALID; int _type = INVALID;
int _channel = DEFAULT_TOKEN_CHANNEL; 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: ( . ) // /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/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: . // /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:31:11: .
{ {
matchAny(); matchAny();
} }
state.type = _type; state.type = _type;
state.channel = _channel; state.channel = _channel;
} }
finally { finally {
// do for sure before leaving // do for sure before leaving
} }
} }
// $ANTLR end "INVALID" // $ANTLR end "INVALID"
@Override @Override
public void mTokens() throws RecognitionException { 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 ) // /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:1:8: ( T__9 | INVALID )
int alt1=2; int alt1=2;
int LA1_0 = input.LA(1); int LA1_0 = input.LA(1);
if ( (LA1_0=='T') ) { if ( (LA1_0=='T') ) {
int LA1_1 = input.LA(2); int LA1_1 = input.LA(2);
if ( (LA1_1=='O') ) { if ( (LA1_1=='O') ) {
alt1=1; alt1=1;
} }
else { else {
alt1=2; alt1=2;
} }
} }
else if ( ((LA1_0 >= '\u0000' && LA1_0 <= 'S')||(LA1_0 >= 'U' && LA1_0 <= '\uFFFF')) ) { else if ( ((LA1_0 >= '\u0000' && LA1_0 <= 'S')||(LA1_0 >= 'U' && LA1_0 <= '\uFFFF')) ) {
alt1=2; alt1=2;
} }
else { else {
NoViableAltException nvae = NoViableAltException nvae =
new NoViableAltException("", 1, 0, input); new NoViableAltException("", 1, 0, input);
throw nvae; throw nvae;
} }
switch (alt1) { switch (alt1) {
case 1 : 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 // /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:1:10: T__9
{ {
mT__9(); mT__9();
} }
break; break;
case 2 : 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 // /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:1:15: INVALID
{ {
mINVALID(); mINVALID();
} }
break; break;
} }
} }
} }

View File

@@ -1,108 +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 // $ANTLR 3.5.2 /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g 2020-05-27 11:44:58
package de.dhbw.compiler.antlrxparser; package de.dhbw.compiler.antlrxparser;
import org.antlr.runtime.*; import org.antlr.runtime.*;
import java.util.Stack; import java.util.Stack;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import org.antlr.runtime.tree.*; import org.antlr.runtime.tree.*;
@SuppressWarnings("all") @SuppressWarnings("all")
public class XParser extends Parser { public class XParser extends Parser {
public static final String[] tokenNames = new String[] { public static final String[] tokenNames = new String[] {
"<invalid>", "<EOR>", "<DOWN>", "<UP>", "DECL", "DECLLIST", "INVALID", "<invalid>", "<EOR>", "<DOWN>", "<UP>", "DECL", "DECLLIST", "INVALID",
"STATLIST", "UMINUS", "'TODO'" "STATLIST", "UMINUS", "'TODO'"
}; };
public static final int EOF=-1; public static final int EOF=-1;
public static final int T__9=9; public static final int T__9=9;
public static final int DECL=4; public static final int DECL=4;
public static final int DECLLIST=5; public static final int DECLLIST=5;
public static final int INVALID=6; public static final int INVALID=6;
public static final int STATLIST=7; public static final int STATLIST=7;
public static final int UMINUS=8; public static final int UMINUS=8;
// delegates // delegates
public Parser[] getDelegates() { public Parser[] getDelegates() {
return new Parser[] {}; return new Parser[] {};
} }
// delegators // delegators
public XParser(TokenStream input) { public XParser(TokenStream input) {
this(input, new RecognizerSharedState()); this(input, new RecognizerSharedState());
} }
public XParser(TokenStream input, RecognizerSharedState state) { public XParser(TokenStream input, RecognizerSharedState state) {
super(input, state); super(input, state);
} }
protected TreeAdaptor adaptor = new CommonTreeAdaptor(); protected TreeAdaptor adaptor = new CommonTreeAdaptor();
public void setTreeAdaptor(TreeAdaptor adaptor) { public void setTreeAdaptor(TreeAdaptor adaptor) {
this.adaptor = adaptor; this.adaptor = adaptor;
} }
public TreeAdaptor getTreeAdaptor() { public TreeAdaptor getTreeAdaptor() {
return adaptor; return adaptor;
} }
@Override public String[] getTokenNames() { return XParser.tokenNames; } @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"; } @Override public String getGrammarFileName() { return "/Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g"; }
public static class program_return extends ParserRuleReturnScope { public static class program_return extends ParserRuleReturnScope {
Object tree; Object tree;
@Override @Override
public Object getTree() { return tree; } public Object getTree() { return tree; }
}; };
// $ANTLR start "program" // $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' ; // /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/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 { public final XParser.program_return program() throws RecognitionException {
XParser.program_return retval = new XParser.program_return(); XParser.program_return retval = new XParser.program_return();
retval.start = input.LT(1); retval.start = input.LT(1);
Object root_0 = null; Object root_0 = null;
Token string_literal1=null; Token string_literal1=null;
Object string_literal1_tree=null; Object string_literal1_tree=null;
try { try {
// C:\\Users\\eisenbiegler\\Dropbox\\workspace_cc\\CC-Praxis-Antlr Parser fuer X-Leer\\src\\de\\dhbw\\compiler\\antlrxparser\\X.g:33:8: ( 'TODO' ) // /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/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' // /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:33:10: 'TODO'
{ {
root_0 = (Object)adaptor.nil(); root_0 = (Object)adaptor.nil();
string_literal1=(Token)match(input,9,FOLLOW_9_in_program95); string_literal1=(Token)match(input,9,FOLLOW_9_in_program95);
string_literal1_tree = (Object)adaptor.create(string_literal1); string_literal1_tree = (Object)adaptor.create(string_literal1);
adaptor.addChild(root_0, string_literal1_tree); adaptor.addChild(root_0, string_literal1_tree);
} }
retval.stop = input.LT(-1); retval.stop = input.LT(-1);
retval.tree = (Object)adaptor.rulePostProcessing(root_0); retval.tree = (Object)adaptor.rulePostProcessing(root_0);
adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop); adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop);
} }
catch (RecognitionException re) { catch (RecognitionException re) {
reportError(re); reportError(re);
recover(input,re); recover(input,re);
retval.tree = (Object)adaptor.errorNode(input, retval.start, input.LT(-1), re); retval.tree = (Object)adaptor.errorNode(input, retval.start, input.LT(-1), re);
} }
finally { finally {
// do for sure before leaving // do for sure before leaving
} }
return retval; return retval;
} }
// $ANTLR end "program" // $ANTLR end "program"
// Delegated rules // Delegated rules
public static final BitSet FOLLOW_9_in_program95 = new BitSet(new long[]{0x0000000000000002L}); public static final BitSet FOLLOW_9_in_program95 = new BitSet(new long[]{0x0000000000000002L});
} }

7
X.tokens Normal file
View File

@@ -0,0 +1,7 @@
T__9=9
DECL=4
DECLLIST=5
INVALID=6
STATLIST=7
UMINUS=8
'TODO'=9

Binary file not shown.

BIN
lib/hamcrest-core-1.3.jar Normal file

Binary file not shown.

BIN
lib/junit-4.12-javadoc.jar Normal file

Binary file not shown.

BIN
lib/junit-4.12.jar Normal file

Binary file not shown.

View File

@@ -0,0 +1,68 @@
T__20=20
T__21=21
T__22=22
T__23=23
T__24=24
T__25=25
T__26=26
T__27=27
T__28=28
T__29=29
T__30=30
T__31=31
T__32=32
T__33=33
T__34=34
T__35=35
T__36=36
T__37=37
T__38=38
T__39=39
T__40=40
T__41=41
T__42=42
T__43=43
T__44=44
T__45=45
COMMENT=4
DECL=5
DECLLIST=6
DIGIT=7
FLOATCONST=8
ID=9
INTCONST=10
INVALID=11
LETTER=12
OTHER=13
POSDIGIT=14
STATLIST=15
STRINGCONST=16
UMINUS=17
WS=18
ZERO=19
'('=20
')'=21
'*'=22
'+'=23
'-'=24
'.'=25
'/'=26
':'=27
':='=28
';'=29
'<'=30
'='=31
'>'=32
'begin'=33
'else'=34
'end'=35
'float'=36
'for'=37
'if'=38
'int'=39
'print'=40
'program'=41
'read'=42
'string'=43
'then'=44
'while'=45

View File

@@ -0,0 +1,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';

View File

@@ -33,5 +33,6 @@
</library> </library>
</orderEntry> </orderEntry>
<orderEntry type="library" name="lib" level="project" /> <orderEntry type="library" name="lib" level="project" />
<orderEntry type="library" name="junit:junit:4.12" level="application" />
</component> </component>
</module> </module>

View File

@@ -18,5 +18,6 @@
<SOURCES /> <SOURCES />
</library> </library>
</orderEntry> </orderEntry>
<orderEntry type="library" name="junit:junit:4.12" level="application" />
</component> </component>
</module> </module>