[11 - Antlr Parser] Fix grammar file and TestAntlrXScanner1#tokenID

This commit is contained in:
2020-05-27 12:41:27 +02:00
parent 9a3e3cfe97
commit 8fbf3095f0
19 changed files with 430 additions and 79 deletions

View File

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

View File

@@ -20,6 +20,7 @@ import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.DOTTreeGenerator;
import org.antlr.runtime.tree.Tree;
import org.antlr.stringtemplate.StringTemplate;
import org.antlr.tool.GrammarAST;
public class AntlrXParserMain {

View File

@@ -14,16 +14,29 @@ grammar X;
options {
language = Java;
output = AST;
ASTLabelType = CommonTree;
backtrack = true;
}
// AST-Tokens
tokens {
DECL;
STATLIST;
DECLLIST;
UMINUS;
ID;
INTCONST;
WS;
}
@parser::header {package de.dhbw.compiler.antlrxparser;}
@lexer::header {package de.dhbw.compiler.antlrxparser;}
// Anmerkung: Es müssen Lexer- UND Parser-Regeln stehen, da sonst das Generieren nicht funktionieren
// Lexer stuff
ID: ('a'..'z' | 'A'..'Z')
(options {
greedy = true;
}: 'a'..'z' | 'A'..'Z' | '0'..'9')*;
INTCONST: ('0'..'9')+;
WS: ('\t' | ' ' | '\r' | '\n' | '\f')+ { skip(); };
// Parser stuff
program: 'TODO';

View File

@@ -1,4 +1,4 @@
// $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
// $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 12:40:05
package de.dhbw.compiler.antlrxparser;
import org.antlr.runtime.*;
@@ -9,12 +9,10 @@ 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;
public static final int T__7=7;
public static final int ID=4;
public static final int INTCONST=5;
public static final int WS=6;
// delegates
// delegators
@@ -31,10 +29,10 @@ public class XLexer extends Lexer {
}
@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 {
// $ANTLR start "T__7"
public final void mT__7() throws RecognitionException {
try {
int _type = T__9;
int _type = T__7;
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'
@@ -50,17 +48,129 @@ public class XLexer extends Lexer {
// do for sure before leaving
}
}
// $ANTLR end "T__9"
// $ANTLR end "T__7"
// $ANTLR start "INVALID"
public final void mINVALID() throws RecognitionException {
// $ANTLR start "ID"
public final void mID() throws RecognitionException {
try {
int _type = INVALID;
int _type = ID;
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: .
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:33:3: ( ( 'a' .. 'z' | 'A' .. 'Z' ) ( options {greedy=true; } : 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' )* )
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:33:13: ( 'a' .. 'z' | 'A' .. 'Z' ) ( options {greedy=true; } : 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' )*
{
matchAny();
if ( (input.LA(1) >= 'A' && input.LA(1) <= 'Z')||(input.LA(1) >= 'a' && input.LA(1) <= 'z') ) {
input.consume();
}
else {
MismatchedSetException mse = new MismatchedSetException(null,input);
recover(mse);
throw mse;
}
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:34:13: ( options {greedy=true; } : 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' )*
loop1:
while (true) {
int alt1=4;
switch ( input.LA(1) ) {
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
{
alt1=1;
}
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
{
alt1=2;
}
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{
alt1=3;
}
break;
}
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:36:16: 'a' .. 'z'
{
matchRange('a','z');
}
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:36:27: 'A' .. 'Z'
{
matchRange('A','Z');
}
break;
case 3 :
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:36:38: '0' .. '9'
{
matchRange('0','9');
}
break;
default :
break loop1;
}
}
}
state.type = _type;
@@ -70,46 +180,264 @@ public class XLexer extends Lexer {
// do for sure before leaving
}
}
// $ANTLR end "INVALID"
// $ANTLR end "ID"
// $ANTLR start "INTCONST"
public final void mINTCONST() throws RecognitionException {
try {
int _type = INTCONST;
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:37:9: ( ( '0' .. '9' )+ )
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:37:13: ( '0' .. '9' )+
{
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:37:13: ( '0' .. '9' )+
int cnt2=0;
loop2:
while (true) {
int alt2=2;
int LA2_0 = input.LA(1);
if ( ((LA2_0 >= '0' && LA2_0 <= '9')) ) {
alt2=1;
}
switch (alt2) {
case 1 :
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:
{
if ( (input.LA(1) >= '0' && input.LA(1) <= '9') ) {
input.consume();
}
else {
MismatchedSetException mse = new MismatchedSetException(null,input);
recover(mse);
throw mse;
}
}
break;
default :
if ( cnt2 >= 1 ) break loop2;
EarlyExitException eee = new EarlyExitException(2, input);
throw eee;
}
cnt2++;
}
}
state.type = _type;
state.channel = _channel;
}
finally {
// do for sure before leaving
}
}
// $ANTLR end "INTCONST"
// $ANTLR start "WS"
public final void mWS() throws RecognitionException {
try {
int _type = WS;
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:39:3: ( ( '\\t' | ' ' | '\\r' | '\\n' | '\\f' )+ )
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:39:13: ( '\\t' | ' ' | '\\r' | '\\n' | '\\f' )+
{
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:39:13: ( '\\t' | ' ' | '\\r' | '\\n' | '\\f' )+
int cnt3=0;
loop3:
while (true) {
int alt3=2;
int LA3_0 = input.LA(1);
if ( ((LA3_0 >= '\t' && LA3_0 <= '\n')||(LA3_0 >= '\f' && LA3_0 <= '\r')||LA3_0==' ') ) {
alt3=1;
}
switch (alt3) {
case 1 :
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:
{
if ( (input.LA(1) >= '\t' && input.LA(1) <= '\n')||(input.LA(1) >= '\f' && input.LA(1) <= '\r')||input.LA(1)==' ' ) {
input.consume();
}
else {
MismatchedSetException mse = new MismatchedSetException(null,input);
recover(mse);
throw mse;
}
}
break;
default :
if ( cnt3 >= 1 ) break loop3;
EarlyExitException eee = new EarlyExitException(3, input);
throw eee;
}
cnt3++;
}
skip();
}
state.type = _type;
state.channel = _channel;
}
finally {
// do for sure before leaving
}
}
// $ANTLR end "WS"
@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;
// /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__7 | ID | INTCONST | WS )
int alt4=4;
switch ( input.LA(1) ) {
case 'T':
{
int LA4_1 = input.LA(2);
if ( (LA4_1=='O') ) {
int LA4_5 = input.LA(3);
if ( (LA4_5=='D') ) {
int LA4_6 = input.LA(4);
if ( (LA4_6=='O') ) {
int LA4_7 = input.LA(5);
if ( ((LA4_7 >= '0' && LA4_7 <= '9')||(LA4_7 >= 'A' && LA4_7 <= 'Z')||(LA4_7 >= 'a' && LA4_7 <= 'z')) ) {
alt4=2;
}
else {
alt4=1;
}
}
else {
alt4=2;
}
}
else {
alt4=2;
}
}
else {
alt1=2;
alt4=2;
}
}
else if ( ((LA1_0 >= '\u0000' && LA1_0 <= 'S')||(LA1_0 >= 'U' && LA1_0 <= '\uFFFF')) ) {
alt1=2;
}
else {
}
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
{
alt4=2;
}
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{
alt4=3;
}
break;
case '\t':
case '\n':
case '\f':
case '\r':
case ' ':
{
alt4=4;
}
break;
default:
NoViableAltException nvae =
new NoViableAltException("", 1, 0, input);
new NoViableAltException("", 4, 0, input);
throw nvae;
}
switch (alt1) {
switch (alt4) {
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
// /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__7
{
mT__9();
mT__7();
}
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
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:1:15: ID
{
mINVALID();
mID();
}
break;
case 3 :
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:1:18: INTCONST
{
mINTCONST();
}
break;
case 4 :
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:1:27: WS
{
mWS();
}
break;

View File

@@ -1,10 +1,12 @@
// $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
// $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 12:40:05
package de.dhbw.compiler.antlrxparser;
import org.antlr.runtime.*;
import java.util.Stack;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import org.antlr.runtime.tree.*;
@@ -12,16 +14,13 @@ 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'"
"<invalid>", "<EOR>", "<DOWN>", "<UP>", "ID", "INTCONST", "WS", "'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;
public static final int T__7=7;
public static final int ID=4;
public static final int INTCONST=5;
public static final int WS=6;
// delegates
public Parser[] getDelegates() {
@@ -51,47 +50,50 @@ public class XParser extends Parser {
public static class program_return extends ParserRuleReturnScope {
Object tree;
CommonTree tree;
@Override
public Object getTree() { return tree; }
public CommonTree 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' ;
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:42: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;
CommonTree root_0 = null;
Token string_literal1=null;
Object string_literal1_tree=null;
CommonTree 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'
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:42:8: ( 'TODO' )
// /Users/kreis/git/gitea.humenius.me/dhbw-compilerbau/CC-Praxis-Antlr Parser fuer X-Leer/src/de/dhbw/compiler/antlrxparser/X.g:42:13: 'TODO'
{
root_0 = (Object)adaptor.nil();
root_0 = (CommonTree)adaptor.nil();
string_literal1=(Token)match(input,9,FOLLOW_9_in_program95);
string_literal1_tree = (Object)adaptor.create(string_literal1);
string_literal1=(Token)match(input,7,FOLLOW_7_in_program256); if (state.failed) return retval;
if ( state.backtracking==0 ) {
string_literal1_tree = (CommonTree)adaptor.create(string_literal1);
adaptor.addChild(root_0, string_literal1_tree);
}
}
retval.stop = input.LT(-1);
retval.tree = (Object)adaptor.rulePostProcessing(root_0);
if ( state.backtracking==0 ) {
retval.tree = (CommonTree)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);
retval.tree = (CommonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re);
}
finally {
// do for sure before leaving
@@ -104,5 +106,5 @@ public class XParser extends Parser {
public static final BitSet FOLLOW_9_in_program95 = new BitSet(new long[]{0x0000000000000002L});
public static final BitSet FOLLOW_7_in_program256 = new BitSet(new long[]{0x0000000000000002L});
}

View File

@@ -14,20 +14,29 @@ grammar X;
options {
language = Java;
output = AST;
ASTLabelType = CommonTree;
backtrack = true;
}
// AST-Tokens
tokens {
DECL;
STATLIST;
DECLLIST;
UMINUS;
ID;
INTCONST;
WS;
}
@parser::header {package de.dhbw.compiler.antlrxparser;}
@lexer::header {package de.dhbw.compiler.antlrxparser;}
// Anmerkung: Es müssen Lexer- UND Parser-Regeln stehen, da sonst das Generieren nicht funktionieren
// Lexer stuff
ID: ('a'..'z' | 'A'..'Z')
(options {
greedy = true;
}: 'a'..'z' | 'A'..'Z' | '0'..'9')*;
INTCONST: ('0'..'9')+;
INVALID: .;
WS: ('\t' | ' ' | '\r' | '\n' | '\f')+ { skip(); };
program: 'TODO';
// Parser stuff
program: 'TODO';