[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,4 +1,4 @@
// $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;
import org.antlr.runtime.*;
@@ -29,15 +29,15 @@ public class XLexer extends Lexer {
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"; }
@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;
// 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'
// /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");
@@ -57,8 +57,8 @@ public class XLexer extends Lexer {
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: .
// /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();
}
@@ -74,7 +74,7 @@ public class XLexer extends Lexer {
@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 )
// /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') ) {
@@ -100,14 +100,14 @@ public class XLexer extends Lexer {
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
// /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 :
// 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();

View File

@@ -1,4 +1,4 @@
// $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;
import org.antlr.runtime.*;
@@ -47,7 +47,7 @@ public class XParser extends Parser {
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"; }
@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 {
@@ -58,7 +58,7 @@ public class XParser extends Parser {
// $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 {
XParser.program_return retval = new XParser.program_return();
retval.start = input.LT(1);
@@ -70,8 +70,8 @@ public class XParser extends Parser {
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'
// /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();

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>