[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";
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
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;}
@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
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;
}
}
}
// $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;
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 "/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"
public final void mT__9() throws RecognitionException {
try {
int _type = T__9;
int _channel = DEFAULT_TOKEN_CHANNEL;
// /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' )
// /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");
}
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;
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/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:11: .
{
matchAny();
}
state.type = _type;
state.channel = _channel;
}
finally {
// do for sure before leaving
}
}
// $ANTLR end "INVALID"
@Override
public void mTokens() throws RecognitionException {
// /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 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 :
// /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();
}
break;
case 2 :
// /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();
}
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
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});
}
// $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;
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 "/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 {
Object tree;
@Override
public Object getTree() { return tree; }
};
// $ANTLR start "program"
// /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 {
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 {
// /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' )
// /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();
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});
}

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>
</orderEntry>
<orderEntry type="library" name="lib" level="project" />
<orderEntry type="library" name="junit:junit:4.12" level="application" />
</component>
</module>

View File

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