mirror of
https://github.com/mudabbir-ahmad/MYOL-TB2-prog3-Py.git
synced 2026-10-07 19:40:21 +00:00
Add ANTLR grammar for greetings and farewells with corresponding parser and listener implementations
This commit is contained in:
1 parent
2027302cf1
commit
6205020013
30 files changed
+1109
-37
No files matched your search
@@ -1,5 +1,10 @@
|
||||
grammar Hello;
|
||||
|
||||
greeting : greet+;
|
||||
leaving : bye+;
|
||||
greet : 'hello' | 'hi' ;
|
||||
|
||||
start : 'hello' ID ;
|
||||
ID : [a-zA-Z]+ ;
|
||||
WS : [ \t\r\n]+ -> skip ;
|
||||
bye : ('bye'| 'goodbye') ID ;
|
||||
+11
-1
@@ -1,18 +1,28 @@
|
||||
token literal names:
|
||||
null
|
||||
'hello'
|
||||
'hi'
|
||||
'bye'
|
||||
'goodbye'
|
||||
null
|
||||
null
|
||||
|
||||
token symbolic names:
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
ID
|
||||
WS
|
||||
|
||||
rule names:
|
||||
greeting
|
||||
leaving
|
||||
greet
|
||||
start
|
||||
bye
|
||||
|
||||
|
||||
atn:
|
||||
[4, 1, 3, 6, 2, 0, 7, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 4, 0, 2, 1, 0, 0, 0, 2, 3, 5, 1, 0, 0, 3, 4, 5, 2, 0, 0, 4, 1, 1, 0, 0, 0, 0]
|
||||
[4, 1, 6, 29, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 1, 0, 4, 0, 12, 8, 0, 11, 0, 12, 0, 13, 1, 1, 4, 1, 17, 8, 1, 11, 1, 12, 1, 18, 1, 2, 1, 2, 1, 3, 1, 3, 1, 3, 1, 4, 1, 4, 1, 4, 1, 4, 0, 0, 5, 0, 2, 4, 6, 8, 0, 2, 1, 0, 1, 2, 1, 0, 3, 4, 25, 0, 11, 1, 0, 0, 0, 2, 16, 1, 0, 0, 0, 4, 20, 1, 0, 0, 0, 6, 22, 1, 0, 0, 0, 8, 25, 1, 0, 0, 0, 10, 12, 3, 4, 2, 0, 11, 10, 1, 0, 0, 0, 12, 13, 1, 0, 0, 0, 13, 11, 1, 0, 0, 0, 13, 14, 1, 0, 0, 0, 14, 1, 1, 0, 0, 0, 15, 17, 3, 8, 4, 0, 16, 15, 1, 0, 0, 0, 17, 18, 1, 0, 0, 0, 18, 16, 1, 0, 0, 0, 18, 19, 1, 0, 0, 0, 19, 3, 1, 0, 0, 0, 20, 21, 7, 0, 0, 0, 21, 5, 1, 0, 0, 0, 22, 23, 5, 1, 0, 0, 23, 24, 5, 5, 0, 0, 24, 7, 1, 0, 0, 0, 25, 26, 7, 1, 0, 0, 26, 27, 5, 5, 0, 0, 27, 9, 1, 0, 0, 0, 2, 13, 18]
|
||||
+8
-2
@@ -1,4 +1,10 @@
|
||||
T__0=1
|
||||
ID=2
|
||||
WS=3
|
||||
T__1=2
|
||||
T__2=3
|
||||
T__3=4
|
||||
ID=5
|
||||
WS=6
|
||||
'hello'=1
|
||||
'hi'=2
|
||||
'bye'=3
|
||||
'goodbye'=4
|
||||
Binary file not shown.
@@ -0,0 +1,75 @@
|
||||
// Generated from Hello.g4 by ANTLR 4.13.2
|
||||
|
||||
import org.antlr.v4.runtime.ParserRuleContext;
|
||||
import org.antlr.v4.runtime.tree.ErrorNode;
|
||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||
|
||||
/**
|
||||
* This class provides an empty implementation of {@link HelloListener},
|
||||
* which can be extended to create a listener which only needs to handle a subset
|
||||
* of the available methods.
|
||||
*/
|
||||
@SuppressWarnings("CheckReturnValue")
|
||||
public class HelloBaseListener implements HelloListener {
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void enterGreeting(HelloParser.GreetingContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void exitGreeting(HelloParser.GreetingContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void enterGreet(HelloParser.GreetContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void exitGreet(HelloParser.GreetContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void enterStart(HelloParser.StartContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void exitStart(HelloParser.StartContext ctx) { }
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void enterEveryRule(ParserRuleContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void exitEveryRule(ParserRuleContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void visitTerminal(TerminalNode node) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void visitErrorNode(ErrorNode node) { }
|
||||
}
|
||||
Binary file not shown.
+10
-1
@@ -1,17 +1,26 @@
|
||||
token literal names:
|
||||
null
|
||||
'hello'
|
||||
'hi'
|
||||
'bye'
|
||||
'goodbye'
|
||||
null
|
||||
null
|
||||
|
||||
token symbolic names:
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
null
|
||||
ID
|
||||
WS
|
||||
|
||||
rule names:
|
||||
T__0
|
||||
T__1
|
||||
T__2
|
||||
T__3
|
||||
ID
|
||||
WS
|
||||
|
||||
@@ -23,4 +32,4 @@ mode names:
|
||||
DEFAULT_MODE
|
||||
|
||||
atn:
|
||||
[4, 0, 3, 25, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 4, 1, 15, 8, 1, 11, 1, 12, 1, 16, 1, 2, 4, 2, 20, 8, 2, 11, 2, 12, 2, 21, 1, 2, 1, 2, 0, 0, 3, 1, 1, 3, 2, 5, 3, 1, 0, 2, 2, 0, 65, 90, 97, 122, 3, 0, 9, 10, 13, 13, 32, 32, 26, 0, 1, 1, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 5, 1, 0, 0, 0, 1, 7, 1, 0, 0, 0, 3, 14, 1, 0, 0, 0, 5, 19, 1, 0, 0, 0, 7, 8, 5, 104, 0, 0, 8, 9, 5, 101, 0, 0, 9, 10, 5, 108, 0, 0, 10, 11, 5, 108, 0, 0, 11, 12, 5, 111, 0, 0, 12, 2, 1, 0, 0, 0, 13, 15, 7, 0, 0, 0, 14, 13, 1, 0, 0, 0, 15, 16, 1, 0, 0, 0, 16, 14, 1, 0, 0, 0, 16, 17, 1, 0, 0, 0, 17, 4, 1, 0, 0, 0, 18, 20, 7, 1, 0, 0, 19, 18, 1, 0, 0, 0, 20, 21, 1, 0, 0, 0, 21, 19, 1, 0, 0, 0, 21, 22, 1, 0, 0, 0, 22, 23, 1, 0, 0, 0, 23, 24, 6, 2, 0, 0, 24, 6, 1, 0, 0, 0, 3, 0, 16, 21, 1, 6, 0, 0]
|
||||
[4, 0, 6, 46, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 4, 4, 4, 36, 8, 4, 11, 4, 12, 4, 37, 1, 5, 4, 5, 41, 8, 5, 11, 5, 12, 5, 42, 1, 5, 1, 5, 0, 0, 6, 1, 1, 3, 2, 5, 3, 7, 4, 9, 5, 11, 6, 1, 0, 2, 2, 0, 65, 90, 97, 122, 3, 0, 9, 10, 13, 13, 32, 32, 47, 0, 1, 1, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 9, 1, 0, 0, 0, 0, 11, 1, 0, 0, 0, 1, 13, 1, 0, 0, 0, 3, 19, 1, 0, 0, 0, 5, 22, 1, 0, 0, 0, 7, 26, 1, 0, 0, 0, 9, 35, 1, 0, 0, 0, 11, 40, 1, 0, 0, 0, 13, 14, 5, 104, 0, 0, 14, 15, 5, 101, 0, 0, 15, 16, 5, 108, 0, 0, 16, 17, 5, 108, 0, 0, 17, 18, 5, 111, 0, 0, 18, 2, 1, 0, 0, 0, 19, 20, 5, 104, 0, 0, 20, 21, 5, 105, 0, 0, 21, 4, 1, 0, 0, 0, 22, 23, 5, 98, 0, 0, 23, 24, 5, 121, 0, 0, 24, 25, 5, 101, 0, 0, 25, 6, 1, 0, 0, 0, 26, 27, 5, 103, 0, 0, 27, 28, 5, 111, 0, 0, 28, 29, 5, 111, 0, 0, 29, 30, 5, 100, 0, 0, 30, 31, 5, 98, 0, 0, 31, 32, 5, 121, 0, 0, 32, 33, 5, 101, 0, 0, 33, 8, 1, 0, 0, 0, 34, 36, 7, 0, 0, 0, 35, 34, 1, 0, 0, 0, 36, 37, 1, 0, 0, 0, 37, 35, 1, 0, 0, 0, 37, 38, 1, 0, 0, 0, 38, 10, 1, 0, 0, 0, 39, 41, 7, 1, 0, 0, 40, 39, 1, 0, 0, 0, 41, 42, 1, 0, 0, 0, 42, 40, 1, 0, 0, 0, 42, 43, 1, 0, 0, 0, 43, 44, 1, 0, 0, 0, 44, 45, 6, 5, 0, 0, 45, 12, 1, 0, 0, 0, 3, 0, 37, 42, 1, 6, 0, 0]
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
// Generated from Hello.g4 by ANTLR 4.13.2
|
||||
import org.antlr.v4.runtime.Lexer;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
import org.antlr.v4.runtime.TokenStream;
|
||||
import org.antlr.v4.runtime.*;
|
||||
import org.antlr.v4.runtime.atn.*;
|
||||
import org.antlr.v4.runtime.dfa.DFA;
|
||||
import org.antlr.v4.runtime.misc.*;
|
||||
|
||||
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast", "CheckReturnValue", "this-escape"})
|
||||
public class HelloLexer extends Lexer {
|
||||
static { RuntimeMetaData.checkVersion("4.13.2", RuntimeMetaData.VERSION); }
|
||||
|
||||
protected static final DFA[] _decisionToDFA;
|
||||
protected static final PredictionContextCache _sharedContextCache =
|
||||
new PredictionContextCache();
|
||||
public static final int
|
||||
T__0=1, T__1=2, ID=3, WS=4;
|
||||
public static String[] channelNames = {
|
||||
"DEFAULT_TOKEN_CHANNEL", "HIDDEN"
|
||||
};
|
||||
|
||||
public static String[] modeNames = {
|
||||
"DEFAULT_MODE"
|
||||
};
|
||||
|
||||
private static String[] makeRuleNames() {
|
||||
return new String[] {
|
||||
"T__0", "T__1", "ID", "WS"
|
||||
};
|
||||
}
|
||||
public static final String[] ruleNames = makeRuleNames();
|
||||
|
||||
private static String[] makeLiteralNames() {
|
||||
return new String[] {
|
||||
null, "'hello'", "'hi'"
|
||||
};
|
||||
}
|
||||
private static final String[] _LITERAL_NAMES = makeLiteralNames();
|
||||
private static String[] makeSymbolicNames() {
|
||||
return new String[] {
|
||||
null, null, null, "ID", "WS"
|
||||
};
|
||||
}
|
||||
private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames();
|
||||
public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link #VOCABULARY} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public static final String[] tokenNames;
|
||||
static {
|
||||
tokenNames = new String[_SYMBOLIC_NAMES.length];
|
||||
for (int i = 0; i < tokenNames.length; i++) {
|
||||
tokenNames[i] = VOCABULARY.getLiteralName(i);
|
||||
if (tokenNames[i] == null) {
|
||||
tokenNames[i] = VOCABULARY.getSymbolicName(i);
|
||||
}
|
||||
|
||||
if (tokenNames[i] == null) {
|
||||
tokenNames[i] = "<INVALID>";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public String[] getTokenNames() {
|
||||
return tokenNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public Vocabulary getVocabulary() {
|
||||
return VOCABULARY;
|
||||
}
|
||||
|
||||
|
||||
public HelloLexer(CharStream input) {
|
||||
super(input);
|
||||
_interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getGrammarFileName() { return "Hello.g4"; }
|
||||
|
||||
@Override
|
||||
public String[] getRuleNames() { return ruleNames; }
|
||||
|
||||
@Override
|
||||
public String getSerializedATN() { return _serializedATN; }
|
||||
|
||||
@Override
|
||||
public String[] getChannelNames() { return channelNames; }
|
||||
|
||||
@Override
|
||||
public String[] getModeNames() { return modeNames; }
|
||||
|
||||
@Override
|
||||
public ATN getATN() { return _ATN; }
|
||||
|
||||
public static final String _serializedATN =
|
||||
"\u0004\u0000\u0004\u001e\u0006\uffff\uffff\u0002\u0000\u0007\u0000\u0002"+
|
||||
"\u0001\u0007\u0001\u0002\u0002\u0007\u0002\u0002\u0003\u0007\u0003\u0001"+
|
||||
"\u0000\u0001\u0000\u0001\u0000\u0001\u0000\u0001\u0000\u0001\u0000\u0001"+
|
||||
"\u0001\u0001\u0001\u0001\u0001\u0001\u0002\u0004\u0002\u0014\b\u0002\u000b"+
|
||||
"\u0002\f\u0002\u0015\u0001\u0003\u0004\u0003\u0019\b\u0003\u000b\u0003"+
|
||||
"\f\u0003\u001a\u0001\u0003\u0001\u0003\u0000\u0000\u0004\u0001\u0001\u0003"+
|
||||
"\u0002\u0005\u0003\u0007\u0004\u0001\u0000\u0002\u0002\u0000AZaz\u0003"+
|
||||
"\u0000\t\n\r\r \u001f\u0000\u0001\u0001\u0000\u0000\u0000\u0000\u0003"+
|
||||
"\u0001\u0000\u0000\u0000\u0000\u0005\u0001\u0000\u0000\u0000\u0000\u0007"+
|
||||
"\u0001\u0000\u0000\u0000\u0001\t\u0001\u0000\u0000\u0000\u0003\u000f\u0001"+
|
||||
"\u0000\u0000\u0000\u0005\u0013\u0001\u0000\u0000\u0000\u0007\u0018\u0001"+
|
||||
"\u0000\u0000\u0000\t\n\u0005h\u0000\u0000\n\u000b\u0005e\u0000\u0000\u000b"+
|
||||
"\f\u0005l\u0000\u0000\f\r\u0005l\u0000\u0000\r\u000e\u0005o\u0000\u0000"+
|
||||
"\u000e\u0002\u0001\u0000\u0000\u0000\u000f\u0010\u0005h\u0000\u0000\u0010"+
|
||||
"\u0011\u0005i\u0000\u0000\u0011\u0004\u0001\u0000\u0000\u0000\u0012\u0014"+
|
||||
"\u0007\u0000\u0000\u0000\u0013\u0012\u0001\u0000\u0000\u0000\u0014\u0015"+
|
||||
"\u0001\u0000\u0000\u0000\u0015\u0013\u0001\u0000\u0000\u0000\u0015\u0016"+
|
||||
"\u0001\u0000\u0000\u0000\u0016\u0006\u0001\u0000\u0000\u0000\u0017\u0019"+
|
||||
"\u0007\u0001\u0000\u0000\u0018\u0017\u0001\u0000\u0000\u0000\u0019\u001a"+
|
||||
"\u0001\u0000\u0000\u0000\u001a\u0018\u0001\u0000\u0000\u0000\u001a\u001b"+
|
||||
"\u0001\u0000\u0000\u0000\u001b\u001c\u0001\u0000\u0000\u0000\u001c\u001d"+
|
||||
"\u0006\u0003\u0000\u0000\u001d\b\u0001\u0000\u0000\u0000\u0003\u0000\u0015"+
|
||||
"\u001a\u0001\u0006\u0000\u0000";
|
||||
public static final ATN _ATN =
|
||||
new ATNDeserializer().deserialize(_serializedATN.toCharArray());
|
||||
static {
|
||||
_decisionToDFA = new DFA[_ATN.getNumberOfDecisions()];
|
||||
for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) {
|
||||
_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
|
||||
}
|
||||
}
|
||||
}
|
||||
+23
-13
@@ -10,15 +10,22 @@ else:
|
||||
|
||||
def serializedATN():
|
||||
return [
|
||||
4,0,3,25,6,-1,2,0,7,0,2,1,7,1,2,2,7,2,1,0,1,0,1,0,1,0,1,0,1,0,1,
|
||||
1,4,1,15,8,1,11,1,12,1,16,1,2,4,2,20,8,2,11,2,12,2,21,1,2,1,2,0,
|
||||
0,3,1,1,3,2,5,3,1,0,2,2,0,65,90,97,122,3,0,9,10,13,13,32,32,26,0,
|
||||
1,1,0,0,0,0,3,1,0,0,0,0,5,1,0,0,0,1,7,1,0,0,0,3,14,1,0,0,0,5,19,
|
||||
1,0,0,0,7,8,5,104,0,0,8,9,5,101,0,0,9,10,5,108,0,0,10,11,5,108,0,
|
||||
0,11,12,5,111,0,0,12,2,1,0,0,0,13,15,7,0,0,0,14,13,1,0,0,0,15,16,
|
||||
1,0,0,0,16,14,1,0,0,0,16,17,1,0,0,0,17,4,1,0,0,0,18,20,7,1,0,0,19,
|
||||
18,1,0,0,0,20,21,1,0,0,0,21,19,1,0,0,0,21,22,1,0,0,0,22,23,1,0,0,
|
||||
0,23,24,6,2,0,0,24,6,1,0,0,0,3,0,16,21,1,6,0,0
|
||||
4,0,6,46,6,-1,2,0,7,0,2,1,7,1,2,2,7,2,2,3,7,3,2,4,7,4,2,5,7,5,1,
|
||||
0,1,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,2,1,2,1,2,1,2,1,3,1,3,1,3,1,
|
||||
3,1,3,1,3,1,3,1,3,1,4,4,4,36,8,4,11,4,12,4,37,1,5,4,5,41,8,5,11,
|
||||
5,12,5,42,1,5,1,5,0,0,6,1,1,3,2,5,3,7,4,9,5,11,6,1,0,2,2,0,65,90,
|
||||
97,122,3,0,9,10,13,13,32,32,47,0,1,1,0,0,0,0,3,1,0,0,0,0,5,1,0,0,
|
||||
0,0,7,1,0,0,0,0,9,1,0,0,0,0,11,1,0,0,0,1,13,1,0,0,0,3,19,1,0,0,0,
|
||||
5,22,1,0,0,0,7,26,1,0,0,0,9,35,1,0,0,0,11,40,1,0,0,0,13,14,5,104,
|
||||
0,0,14,15,5,101,0,0,15,16,5,108,0,0,16,17,5,108,0,0,17,18,5,111,
|
||||
0,0,18,2,1,0,0,0,19,20,5,104,0,0,20,21,5,105,0,0,21,4,1,0,0,0,22,
|
||||
23,5,98,0,0,23,24,5,121,0,0,24,25,5,101,0,0,25,6,1,0,0,0,26,27,5,
|
||||
103,0,0,27,28,5,111,0,0,28,29,5,111,0,0,29,30,5,100,0,0,30,31,5,
|
||||
98,0,0,31,32,5,121,0,0,32,33,5,101,0,0,33,8,1,0,0,0,34,36,7,0,0,
|
||||
0,35,34,1,0,0,0,36,37,1,0,0,0,37,35,1,0,0,0,37,38,1,0,0,0,38,10,
|
||||
1,0,0,0,39,41,7,1,0,0,40,39,1,0,0,0,41,42,1,0,0,0,42,40,1,0,0,0,
|
||||
42,43,1,0,0,0,43,44,1,0,0,0,44,45,6,5,0,0,45,12,1,0,0,0,3,0,37,42,
|
||||
1,6,0,0
|
||||
]
|
||||
|
||||
class HelloLexer(Lexer):
|
||||
@@ -28,20 +35,23 @@ class HelloLexer(Lexer):
|
||||
decisionsToDFA = [ DFA(ds, i) for i, ds in enumerate(atn.decisionToState) ]
|
||||
|
||||
T__0 = 1
|
||||
ID = 2
|
||||
WS = 3
|
||||
T__1 = 2
|
||||
T__2 = 3
|
||||
T__3 = 4
|
||||
ID = 5
|
||||
WS = 6
|
||||
|
||||
channelNames = [ u"DEFAULT_TOKEN_CHANNEL", u"HIDDEN" ]
|
||||
|
||||
modeNames = [ "DEFAULT_MODE" ]
|
||||
|
||||
literalNames = [ "<INVALID>",
|
||||
"'hello'" ]
|
||||
"'hello'", "'hi'", "'bye'", "'goodbye'" ]
|
||||
|
||||
symbolicNames = [ "<INVALID>",
|
||||
"ID", "WS" ]
|
||||
|
||||
ruleNames = [ "T__0", "ID", "WS" ]
|
||||
ruleNames = [ "T__0", "T__1", "T__2", "T__3", "ID", "WS" ]
|
||||
|
||||
grammarFileName = "Hello.g4"
|
||||
|
||||
|
||||
+8
-2
@@ -1,4 +1,10 @@
|
||||
T__0=1
|
||||
ID=2
|
||||
WS=3
|
||||
T__1=2
|
||||
T__2=3
|
||||
T__3=4
|
||||
ID=5
|
||||
WS=6
|
||||
'hello'=1
|
||||
'hi'=2
|
||||
'bye'=3
|
||||
'goodbye'=4
|
||||
Binary file not shown.
@@ -0,0 +1,39 @@
|
||||
// Generated from Hello.g4 by ANTLR 4.13.2
|
||||
import org.antlr.v4.runtime.tree.ParseTreeListener;
|
||||
|
||||
/**
|
||||
* This interface defines a complete listener for a parse tree produced by
|
||||
* {@link HelloParser}.
|
||||
*/
|
||||
public interface HelloListener extends ParseTreeListener {
|
||||
/**
|
||||
* Enter a parse tree produced by {@link HelloParser#greeting}.
|
||||
* @param ctx the parse tree
|
||||
*/
|
||||
void enterGreeting(HelloParser.GreetingContext ctx);
|
||||
/**
|
||||
* Exit a parse tree produced by {@link HelloParser#greeting}.
|
||||
* @param ctx the parse tree
|
||||
*/
|
||||
void exitGreeting(HelloParser.GreetingContext ctx);
|
||||
/**
|
||||
* Enter a parse tree produced by {@link HelloParser#greet}.
|
||||
* @param ctx the parse tree
|
||||
*/
|
||||
void enterGreet(HelloParser.GreetContext ctx);
|
||||
/**
|
||||
* Exit a parse tree produced by {@link HelloParser#greet}.
|
||||
* @param ctx the parse tree
|
||||
*/
|
||||
void exitGreet(HelloParser.GreetContext ctx);
|
||||
/**
|
||||
* Enter a parse tree produced by {@link HelloParser#start}.
|
||||
* @param ctx the parse tree
|
||||
*/
|
||||
void enterStart(HelloParser.StartContext ctx);
|
||||
/**
|
||||
* Exit a parse tree produced by {@link HelloParser#start}.
|
||||
* @param ctx the parse tree
|
||||
*/
|
||||
void exitStart(HelloParser.StartContext ctx);
|
||||
}
|
||||
@@ -8,6 +8,33 @@ else:
|
||||
# This class defines a complete listener for a parse tree produced by HelloParser.
|
||||
class HelloListener(ParseTreeListener):
|
||||
|
||||
# Enter a parse tree produced by HelloParser#greeting.
|
||||
def enterGreeting(self, ctx:HelloParser.GreetingContext):
|
||||
pass
|
||||
|
||||
# Exit a parse tree produced by HelloParser#greeting.
|
||||
def exitGreeting(self, ctx:HelloParser.GreetingContext):
|
||||
pass
|
||||
|
||||
|
||||
# Enter a parse tree produced by HelloParser#leaving.
|
||||
def enterLeaving(self, ctx:HelloParser.LeavingContext):
|
||||
pass
|
||||
|
||||
# Exit a parse tree produced by HelloParser#leaving.
|
||||
def exitLeaving(self, ctx:HelloParser.LeavingContext):
|
||||
pass
|
||||
|
||||
|
||||
# Enter a parse tree produced by HelloParser#greet.
|
||||
def enterGreet(self, ctx:HelloParser.GreetContext):
|
||||
pass
|
||||
|
||||
# Exit a parse tree produced by HelloParser#greet.
|
||||
def exitGreet(self, ctx:HelloParser.GreetContext):
|
||||
pass
|
||||
|
||||
|
||||
# Enter a parse tree produced by HelloParser#start.
|
||||
def enterStart(self, ctx:HelloParser.StartContext):
|
||||
pass
|
||||
@@ -17,5 +44,14 @@ class HelloListener(ParseTreeListener):
|
||||
pass
|
||||
|
||||
|
||||
# Enter a parse tree produced by HelloParser#bye.
|
||||
def enterBye(self, ctx:HelloParser.ByeContext):
|
||||
pass
|
||||
|
||||
# Exit a parse tree produced by HelloParser#bye.
|
||||
def exitBye(self, ctx:HelloParser.ByeContext):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
del HelloParser
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,253 @@
|
||||
// Generated from Hello.g4 by ANTLR 4.13.2
|
||||
import org.antlr.v4.runtime.atn.*;
|
||||
import org.antlr.v4.runtime.dfa.DFA;
|
||||
import org.antlr.v4.runtime.*;
|
||||
import org.antlr.v4.runtime.misc.*;
|
||||
import org.antlr.v4.runtime.tree.*;
|
||||
import java.util.List;
|
||||
import java.util.Iterator;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast", "CheckReturnValue", "this-escape"})
|
||||
public class HelloParser extends Parser {
|
||||
static { RuntimeMetaData.checkVersion("4.13.2", RuntimeMetaData.VERSION); }
|
||||
|
||||
protected static final DFA[] _decisionToDFA;
|
||||
protected static final PredictionContextCache _sharedContextCache =
|
||||
new PredictionContextCache();
|
||||
public static final int
|
||||
T__0=1, T__1=2, ID=3, WS=4;
|
||||
public static final int
|
||||
RULE_greeting = 0, RULE_greet = 1, RULE_start = 2;
|
||||
private static String[] makeRuleNames() {
|
||||
return new String[] {
|
||||
"greeting", "greet", "start"
|
||||
};
|
||||
}
|
||||
public static final String[] ruleNames = makeRuleNames();
|
||||
|
||||
private static String[] makeLiteralNames() {
|
||||
return new String[] {
|
||||
null, "'hello'", "'hi'"
|
||||
};
|
||||
}
|
||||
private static final String[] _LITERAL_NAMES = makeLiteralNames();
|
||||
private static String[] makeSymbolicNames() {
|
||||
return new String[] {
|
||||
null, null, null, "ID", "WS"
|
||||
};
|
||||
}
|
||||
private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames();
|
||||
public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link #VOCABULARY} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public static final String[] tokenNames;
|
||||
static {
|
||||
tokenNames = new String[_SYMBOLIC_NAMES.length];
|
||||
for (int i = 0; i < tokenNames.length; i++) {
|
||||
tokenNames[i] = VOCABULARY.getLiteralName(i);
|
||||
if (tokenNames[i] == null) {
|
||||
tokenNames[i] = VOCABULARY.getSymbolicName(i);
|
||||
}
|
||||
|
||||
if (tokenNames[i] == null) {
|
||||
tokenNames[i] = "<INVALID>";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public String[] getTokenNames() {
|
||||
return tokenNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public Vocabulary getVocabulary() {
|
||||
return VOCABULARY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getGrammarFileName() { return "Hello.g4"; }
|
||||
|
||||
@Override
|
||||
public String[] getRuleNames() { return ruleNames; }
|
||||
|
||||
@Override
|
||||
public String getSerializedATN() { return _serializedATN; }
|
||||
|
||||
@Override
|
||||
public ATN getATN() { return _ATN; }
|
||||
|
||||
public HelloParser(TokenStream input) {
|
||||
super(input);
|
||||
_interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache);
|
||||
}
|
||||
|
||||
@SuppressWarnings("CheckReturnValue")
|
||||
public static class GreetingContext extends ParserRuleContext {
|
||||
public List<GreetContext> greet() {
|
||||
return getRuleContexts(GreetContext.class);
|
||||
}
|
||||
public GreetContext greet(int i) {
|
||||
return getRuleContext(GreetContext.class,i);
|
||||
}
|
||||
public GreetingContext(ParserRuleContext parent, int invokingState) {
|
||||
super(parent, invokingState);
|
||||
}
|
||||
@Override public int getRuleIndex() { return RULE_greeting; }
|
||||
@Override
|
||||
public void enterRule(ParseTreeListener listener) {
|
||||
if ( listener instanceof HelloListener ) ((HelloListener)listener).enterGreeting(this);
|
||||
}
|
||||
@Override
|
||||
public void exitRule(ParseTreeListener listener) {
|
||||
if ( listener instanceof HelloListener ) ((HelloListener)listener).exitGreeting(this);
|
||||
}
|
||||
}
|
||||
|
||||
public final GreetingContext greeting() throws RecognitionException {
|
||||
GreetingContext _localctx = new GreetingContext(_ctx, getState());
|
||||
enterRule(_localctx, 0, RULE_greeting);
|
||||
int _la;
|
||||
try {
|
||||
enterOuterAlt(_localctx, 1);
|
||||
{
|
||||
setState(7);
|
||||
_errHandler.sync(this);
|
||||
_la = _input.LA(1);
|
||||
do {
|
||||
{
|
||||
{
|
||||
setState(6);
|
||||
greet();
|
||||
}
|
||||
}
|
||||
setState(9);
|
||||
_errHandler.sync(this);
|
||||
_la = _input.LA(1);
|
||||
} while ( _la==T__0 || _la==T__1 );
|
||||
}
|
||||
}
|
||||
catch (RecognitionException re) {
|
||||
_localctx.exception = re;
|
||||
_errHandler.reportError(this, re);
|
||||
_errHandler.recover(this, re);
|
||||
}
|
||||
finally {
|
||||
exitRule();
|
||||
}
|
||||
return _localctx;
|
||||
}
|
||||
|
||||
@SuppressWarnings("CheckReturnValue")
|
||||
public static class GreetContext extends ParserRuleContext {
|
||||
public GreetContext(ParserRuleContext parent, int invokingState) {
|
||||
super(parent, invokingState);
|
||||
}
|
||||
@Override public int getRuleIndex() { return RULE_greet; }
|
||||
@Override
|
||||
public void enterRule(ParseTreeListener listener) {
|
||||
if ( listener instanceof HelloListener ) ((HelloListener)listener).enterGreet(this);
|
||||
}
|
||||
@Override
|
||||
public void exitRule(ParseTreeListener listener) {
|
||||
if ( listener instanceof HelloListener ) ((HelloListener)listener).exitGreet(this);
|
||||
}
|
||||
}
|
||||
|
||||
public final GreetContext greet() throws RecognitionException {
|
||||
GreetContext _localctx = new GreetContext(_ctx, getState());
|
||||
enterRule(_localctx, 2, RULE_greet);
|
||||
int _la;
|
||||
try {
|
||||
enterOuterAlt(_localctx, 1);
|
||||
{
|
||||
setState(11);
|
||||
_la = _input.LA(1);
|
||||
if ( !(_la==T__0 || _la==T__1) ) {
|
||||
_errHandler.recoverInline(this);
|
||||
}
|
||||
else {
|
||||
if ( _input.LA(1)==Token.EOF ) matchedEOF = true;
|
||||
_errHandler.reportMatch(this);
|
||||
consume();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (RecognitionException re) {
|
||||
_localctx.exception = re;
|
||||
_errHandler.reportError(this, re);
|
||||
_errHandler.recover(this, re);
|
||||
}
|
||||
finally {
|
||||
exitRule();
|
||||
}
|
||||
return _localctx;
|
||||
}
|
||||
|
||||
@SuppressWarnings("CheckReturnValue")
|
||||
public static class StartContext extends ParserRuleContext {
|
||||
public TerminalNode ID() { return getToken(HelloParser.ID, 0); }
|
||||
public StartContext(ParserRuleContext parent, int invokingState) {
|
||||
super(parent, invokingState);
|
||||
}
|
||||
@Override public int getRuleIndex() { return RULE_start; }
|
||||
@Override
|
||||
public void enterRule(ParseTreeListener listener) {
|
||||
if ( listener instanceof HelloListener ) ((HelloListener)listener).enterStart(this);
|
||||
}
|
||||
@Override
|
||||
public void exitRule(ParseTreeListener listener) {
|
||||
if ( listener instanceof HelloListener ) ((HelloListener)listener).exitStart(this);
|
||||
}
|
||||
}
|
||||
|
||||
public final StartContext start() throws RecognitionException {
|
||||
StartContext _localctx = new StartContext(_ctx, getState());
|
||||
enterRule(_localctx, 4, RULE_start);
|
||||
try {
|
||||
enterOuterAlt(_localctx, 1);
|
||||
{
|
||||
setState(13);
|
||||
match(T__0);
|
||||
setState(14);
|
||||
match(ID);
|
||||
}
|
||||
}
|
||||
catch (RecognitionException re) {
|
||||
_localctx.exception = re;
|
||||
_errHandler.reportError(this, re);
|
||||
_errHandler.recover(this, re);
|
||||
}
|
||||
finally {
|
||||
exitRule();
|
||||
}
|
||||
return _localctx;
|
||||
}
|
||||
|
||||
public static final String _serializedATN =
|
||||
"\u0004\u0001\u0004\u0011\u0002\u0000\u0007\u0000\u0002\u0001\u0007\u0001"+
|
||||
"\u0002\u0002\u0007\u0002\u0001\u0000\u0004\u0000\b\b\u0000\u000b\u0000"+
|
||||
"\f\u0000\t\u0001\u0001\u0001\u0001\u0001\u0002\u0001\u0002\u0001\u0002"+
|
||||
"\u0001\u0002\u0000\u0000\u0003\u0000\u0002\u0004\u0000\u0001\u0001\u0000"+
|
||||
"\u0001\u0002\u000e\u0000\u0007\u0001\u0000\u0000\u0000\u0002\u000b\u0001"+
|
||||
"\u0000\u0000\u0000\u0004\r\u0001\u0000\u0000\u0000\u0006\b\u0003\u0002"+
|
||||
"\u0001\u0000\u0007\u0006\u0001\u0000\u0000\u0000\b\t\u0001\u0000\u0000"+
|
||||
"\u0000\t\u0007\u0001\u0000\u0000\u0000\t\n\u0001\u0000\u0000\u0000\n\u0001"+
|
||||
"\u0001\u0000\u0000\u0000\u000b\f\u0007\u0000\u0000\u0000\f\u0003\u0001"+
|
||||
"\u0000\u0000\u0000\r\u000e\u0005\u0001\u0000\u0000\u000e\u000f\u0005\u0003"+
|
||||
"\u0000\u0000\u000f\u0005\u0001\u0000\u0000\u0000\u0001\t";
|
||||
public static final ATN _ATN =
|
||||
new ATNDeserializer().deserialize(_serializedATN.toCharArray());
|
||||
static {
|
||||
_decisionToDFA = new DFA[_ATN.getNumberOfDecisions()];
|
||||
for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) {
|
||||
_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
|
||||
}
|
||||
}
|
||||
}
|
||||
+232
-11
@@ -10,8 +10,15 @@ else:
|
||||
|
||||
def serializedATN():
|
||||
return [
|
||||
4,1,3,6,2,0,7,0,1,0,1,0,1,0,1,0,0,0,1,0,0,0,4,0,2,1,0,0,0,2,3,5,
|
||||
1,0,0,3,4,5,2,0,0,4,1,1,0,0,0,0
|
||||
4,1,6,29,2,0,7,0,2,1,7,1,2,2,7,2,2,3,7,3,2,4,7,4,1,0,4,0,12,8,0,
|
||||
11,0,12,0,13,1,1,4,1,17,8,1,11,1,12,1,18,1,2,1,2,1,3,1,3,1,3,1,4,
|
||||
1,4,1,4,1,4,0,0,5,0,2,4,6,8,0,2,1,0,1,2,1,0,3,4,25,0,11,1,0,0,0,
|
||||
2,16,1,0,0,0,4,20,1,0,0,0,6,22,1,0,0,0,8,25,1,0,0,0,10,12,3,4,2,
|
||||
0,11,10,1,0,0,0,12,13,1,0,0,0,13,11,1,0,0,0,13,14,1,0,0,0,14,1,1,
|
||||
0,0,0,15,17,3,8,4,0,16,15,1,0,0,0,17,18,1,0,0,0,18,16,1,0,0,0,18,
|
||||
19,1,0,0,0,19,3,1,0,0,0,20,21,7,0,0,0,21,5,1,0,0,0,22,23,5,1,0,0,
|
||||
23,24,5,5,0,0,24,7,1,0,0,0,25,26,7,1,0,0,26,27,5,5,0,0,27,9,1,0,
|
||||
0,0,2,13,18
|
||||
]
|
||||
|
||||
class HelloParser ( Parser ):
|
||||
@@ -24,18 +31,26 @@ class HelloParser ( Parser ):
|
||||
|
||||
sharedContextCache = PredictionContextCache()
|
||||
|
||||
literalNames = [ "<INVALID>", "'hello'" ]
|
||||
literalNames = [ "<INVALID>", "'hello'", "'hi'", "'bye'", "'goodbye'" ]
|
||||
|
||||
symbolicNames = [ "<INVALID>", "<INVALID>", "ID", "WS" ]
|
||||
symbolicNames = [ "<INVALID>", "<INVALID>", "<INVALID>", "<INVALID>",
|
||||
"<INVALID>", "ID", "WS" ]
|
||||
|
||||
RULE_start = 0
|
||||
RULE_greeting = 0
|
||||
RULE_leaving = 1
|
||||
RULE_greet = 2
|
||||
RULE_start = 3
|
||||
RULE_bye = 4
|
||||
|
||||
ruleNames = [ "start" ]
|
||||
ruleNames = [ "greeting", "leaving", "greet", "start", "bye" ]
|
||||
|
||||
EOF = Token.EOF
|
||||
T__0=1
|
||||
ID=2
|
||||
WS=3
|
||||
T__1=2
|
||||
T__2=3
|
||||
T__3=4
|
||||
ID=5
|
||||
WS=6
|
||||
|
||||
def __init__(self, input:TokenStream, output:TextIO = sys.stdout):
|
||||
super().__init__(input, output)
|
||||
@@ -46,6 +61,163 @@ class HelloParser ( Parser ):
|
||||
|
||||
|
||||
|
||||
class GreetingContext(ParserRuleContext):
|
||||
__slots__ = 'parser'
|
||||
|
||||
def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1):
|
||||
super().__init__(parent, invokingState)
|
||||
self.parser = parser
|
||||
|
||||
def greet(self, i:int=None):
|
||||
if i is None:
|
||||
return self.getTypedRuleContexts(HelloParser.GreetContext)
|
||||
else:
|
||||
return self.getTypedRuleContext(HelloParser.GreetContext,i)
|
||||
|
||||
|
||||
def getRuleIndex(self):
|
||||
return HelloParser.RULE_greeting
|
||||
|
||||
def enterRule(self, listener:ParseTreeListener):
|
||||
if hasattr( listener, "enterGreeting" ):
|
||||
listener.enterGreeting(self)
|
||||
|
||||
def exitRule(self, listener:ParseTreeListener):
|
||||
if hasattr( listener, "exitGreeting" ):
|
||||
listener.exitGreeting(self)
|
||||
|
||||
|
||||
|
||||
|
||||
def greeting(self):
|
||||
|
||||
localctx = HelloParser.GreetingContext(self, self._ctx, self.state)
|
||||
self.enterRule(localctx, 0, self.RULE_greeting)
|
||||
self._la = 0 # Token type
|
||||
try:
|
||||
self.enterOuterAlt(localctx, 1)
|
||||
self.state = 11
|
||||
self._errHandler.sync(self)
|
||||
_la = self._input.LA(1)
|
||||
while True:
|
||||
self.state = 10
|
||||
self.greet()
|
||||
self.state = 13
|
||||
self._errHandler.sync(self)
|
||||
_la = self._input.LA(1)
|
||||
if not (_la==1 or _la==2):
|
||||
break
|
||||
|
||||
except RecognitionException as re:
|
||||
localctx.exception = re
|
||||
self._errHandler.reportError(self, re)
|
||||
self._errHandler.recover(self, re)
|
||||
finally:
|
||||
self.exitRule()
|
||||
return localctx
|
||||
|
||||
|
||||
class LeavingContext(ParserRuleContext):
|
||||
__slots__ = 'parser'
|
||||
|
||||
def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1):
|
||||
super().__init__(parent, invokingState)
|
||||
self.parser = parser
|
||||
|
||||
def bye(self, i:int=None):
|
||||
if i is None:
|
||||
return self.getTypedRuleContexts(HelloParser.ByeContext)
|
||||
else:
|
||||
return self.getTypedRuleContext(HelloParser.ByeContext,i)
|
||||
|
||||
|
||||
def getRuleIndex(self):
|
||||
return HelloParser.RULE_leaving
|
||||
|
||||
def enterRule(self, listener:ParseTreeListener):
|
||||
if hasattr( listener, "enterLeaving" ):
|
||||
listener.enterLeaving(self)
|
||||
|
||||
def exitRule(self, listener:ParseTreeListener):
|
||||
if hasattr( listener, "exitLeaving" ):
|
||||
listener.exitLeaving(self)
|
||||
|
||||
|
||||
|
||||
|
||||
def leaving(self):
|
||||
|
||||
localctx = HelloParser.LeavingContext(self, self._ctx, self.state)
|
||||
self.enterRule(localctx, 2, self.RULE_leaving)
|
||||
self._la = 0 # Token type
|
||||
try:
|
||||
self.enterOuterAlt(localctx, 1)
|
||||
self.state = 16
|
||||
self._errHandler.sync(self)
|
||||
_la = self._input.LA(1)
|
||||
while True:
|
||||
self.state = 15
|
||||
self.bye()
|
||||
self.state = 18
|
||||
self._errHandler.sync(self)
|
||||
_la = self._input.LA(1)
|
||||
if not (_la==3 or _la==4):
|
||||
break
|
||||
|
||||
except RecognitionException as re:
|
||||
localctx.exception = re
|
||||
self._errHandler.reportError(self, re)
|
||||
self._errHandler.recover(self, re)
|
||||
finally:
|
||||
self.exitRule()
|
||||
return localctx
|
||||
|
||||
|
||||
class GreetContext(ParserRuleContext):
|
||||
__slots__ = 'parser'
|
||||
|
||||
def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1):
|
||||
super().__init__(parent, invokingState)
|
||||
self.parser = parser
|
||||
|
||||
|
||||
def getRuleIndex(self):
|
||||
return HelloParser.RULE_greet
|
||||
|
||||
def enterRule(self, listener:ParseTreeListener):
|
||||
if hasattr( listener, "enterGreet" ):
|
||||
listener.enterGreet(self)
|
||||
|
||||
def exitRule(self, listener:ParseTreeListener):
|
||||
if hasattr( listener, "exitGreet" ):
|
||||
listener.exitGreet(self)
|
||||
|
||||
|
||||
|
||||
|
||||
def greet(self):
|
||||
|
||||
localctx = HelloParser.GreetContext(self, self._ctx, self.state)
|
||||
self.enterRule(localctx, 4, self.RULE_greet)
|
||||
self._la = 0 # Token type
|
||||
try:
|
||||
self.enterOuterAlt(localctx, 1)
|
||||
self.state = 20
|
||||
_la = self._input.LA(1)
|
||||
if not(_la==1 or _la==2):
|
||||
self._errHandler.recoverInline(self)
|
||||
else:
|
||||
self._errHandler.reportMatch(self)
|
||||
self.consume()
|
||||
except RecognitionException as re:
|
||||
localctx.exception = re
|
||||
self._errHandler.reportError(self, re)
|
||||
self._errHandler.recover(self, re)
|
||||
finally:
|
||||
self.exitRule()
|
||||
return localctx
|
||||
|
||||
|
||||
class StartContext(ParserRuleContext):
|
||||
__slots__ = 'parser'
|
||||
|
||||
@@ -73,12 +245,61 @@ class HelloParser ( Parser ):
|
||||
def start(self):
|
||||
|
||||
localctx = HelloParser.StartContext(self, self._ctx, self.state)
|
||||
self.enterRule(localctx, 0, self.RULE_start)
|
||||
self.enterRule(localctx, 6, self.RULE_start)
|
||||
try:
|
||||
self.enterOuterAlt(localctx, 1)
|
||||
self.state = 2
|
||||
self.state = 22
|
||||
self.match(HelloParser.T__0)
|
||||
self.state = 3
|
||||
self.state = 23
|
||||
self.match(HelloParser.ID)
|
||||
except RecognitionException as re:
|
||||
localctx.exception = re
|
||||
self._errHandler.reportError(self, re)
|
||||
self._errHandler.recover(self, re)
|
||||
finally:
|
||||
self.exitRule()
|
||||
return localctx
|
||||
|
||||
|
||||
class ByeContext(ParserRuleContext):
|
||||
__slots__ = 'parser'
|
||||
|
||||
def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1):
|
||||
super().__init__(parent, invokingState)
|
||||
self.parser = parser
|
||||
|
||||
def ID(self):
|
||||
return self.getToken(HelloParser.ID, 0)
|
||||
|
||||
def getRuleIndex(self):
|
||||
return HelloParser.RULE_bye
|
||||
|
||||
def enterRule(self, listener:ParseTreeListener):
|
||||
if hasattr( listener, "enterBye" ):
|
||||
listener.enterBye(self)
|
||||
|
||||
def exitRule(self, listener:ParseTreeListener):
|
||||
if hasattr( listener, "exitBye" ):
|
||||
listener.exitBye(self)
|
||||
|
||||
|
||||
|
||||
|
||||
def bye(self):
|
||||
|
||||
localctx = HelloParser.ByeContext(self, self._ctx, self.state)
|
||||
self.enterRule(localctx, 8, self.RULE_bye)
|
||||
self._la = 0 # Token type
|
||||
try:
|
||||
self.enterOuterAlt(localctx, 1)
|
||||
self.state = 25
|
||||
_la = self._input.LA(1)
|
||||
if not(_la==3 or _la==4):
|
||||
self._errHandler.recoverInline(self)
|
||||
else:
|
||||
self._errHandler.reportMatch(self)
|
||||
self.consume()
|
||||
self.state = 26
|
||||
self.match(HelloParser.ID)
|
||||
except RecognitionException as re:
|
||||
localctx.exception = re
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
from antlr4.Utils import escapeWhitespace
|
||||
from antlr4.tree.Tree import Tree
|
||||
from antlr4.tree.Trees import Trees
|
||||
|
||||
# need forward declaration
|
||||
Parser = None
|
||||
|
||||
class TreesUser(Trees):
|
||||
|
||||
# Print out a whole tree in prettier form
|
||||
# Based on Trees.toStringTree()
|
||||
# indent is the number of indents to use in the display
|
||||
@classmethod
|
||||
def PrettyPrint(cls, t:Tree, ruleNames:list=None, recog:Parser=None, indent:int=0):
|
||||
# indentation is the indent to actually display
|
||||
indentation = ''
|
||||
for n in range(indent):
|
||||
indentation += ' '
|
||||
if recog is not None:
|
||||
ruleNames = recog.ruleNames
|
||||
s = escapeWhitespace(cls.getNodeText(t, ruleNames), False)
|
||||
retval = indentation + s + "\n"
|
||||
if t.getChildCount()>0:
|
||||
# we have children to deal with
|
||||
for i in range(0, t.getChildCount()):
|
||||
retval += cls.PrettyPrint(t.getChild(i), ruleNames, indent=indent+1)
|
||||
return retval
|
||||
Whitespace-only changes.
@@ -0,0 +1,5 @@
|
||||
grammar week3;
|
||||
|
||||
start : 'minimal' ID ;
|
||||
ID : [a-zA-Z]+ ;
|
||||
WS : [ \t\r\n]+ -> skip ;
|
||||
@@ -0,0 +1,18 @@
|
||||
token literal names:
|
||||
null
|
||||
'minimal'
|
||||
null
|
||||
null
|
||||
|
||||
token symbolic names:
|
||||
null
|
||||
null
|
||||
ID
|
||||
WS
|
||||
|
||||
rule names:
|
||||
start
|
||||
|
||||
|
||||
atn:
|
||||
[4, 1, 3, 6, 2, 0, 7, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 4, 0, 2, 1, 0, 0, 0, 2, 3, 5, 1, 0, 0, 3, 4, 5, 2, 0, 0, 4, 1, 1, 0, 0, 0, 0]
|
||||
@@ -0,0 +1,4 @@
|
||||
T__0=1
|
||||
ID=2
|
||||
WS=3
|
||||
'minimal'=1
|
||||
@@ -0,0 +1,26 @@
|
||||
token literal names:
|
||||
null
|
||||
'minimal'
|
||||
null
|
||||
null
|
||||
|
||||
token symbolic names:
|
||||
null
|
||||
null
|
||||
ID
|
||||
WS
|
||||
|
||||
rule names:
|
||||
T__0
|
||||
ID
|
||||
WS
|
||||
|
||||
channel names:
|
||||
DEFAULT_TOKEN_CHANNEL
|
||||
HIDDEN
|
||||
|
||||
mode names:
|
||||
DEFAULT_MODE
|
||||
|
||||
atn:
|
||||
[4, 0, 3, 27, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 4, 1, 17, 8, 1, 11, 1, 12, 1, 18, 1, 2, 4, 2, 22, 8, 2, 11, 2, 12, 2, 23, 1, 2, 1, 2, 0, 0, 3, 1, 1, 3, 2, 5, 3, 1, 0, 2, 2, 0, 65, 90, 97, 122, 3, 0, 9, 10, 13, 13, 32, 32, 28, 0, 1, 1, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 5, 1, 0, 0, 0, 1, 7, 1, 0, 0, 0, 3, 16, 1, 0, 0, 0, 5, 21, 1, 0, 0, 0, 7, 8, 5, 109, 0, 0, 8, 9, 5, 105, 0, 0, 9, 10, 5, 110, 0, 0, 10, 11, 5, 105, 0, 0, 11, 12, 5, 109, 0, 0, 12, 13, 5, 97, 0, 0, 13, 14, 5, 108, 0, 0, 14, 2, 1, 0, 0, 0, 15, 17, 7, 0, 0, 0, 16, 15, 1, 0, 0, 0, 17, 18, 1, 0, 0, 0, 18, 16, 1, 0, 0, 0, 18, 19, 1, 0, 0, 0, 19, 4, 1, 0, 0, 0, 20, 22, 7, 1, 0, 0, 21, 20, 1, 0, 0, 0, 22, 23, 1, 0, 0, 0, 23, 21, 1, 0, 0, 0, 23, 24, 1, 0, 0, 0, 24, 25, 1, 0, 0, 0, 25, 26, 6, 2, 0, 0, 26, 6, 1, 0, 0, 0, 3, 0, 18, 23, 1, 6, 0, 0]
|
||||
@@ -0,0 +1,56 @@
|
||||
# Generated from ./week3.g4 by ANTLR 4.13.2
|
||||
from antlr4 import *
|
||||
from io import StringIO
|
||||
import sys
|
||||
if sys.version_info[1] > 5:
|
||||
from typing import TextIO
|
||||
else:
|
||||
from typing.io import TextIO
|
||||
|
||||
|
||||
def serializedATN():
|
||||
return [
|
||||
4,0,3,27,6,-1,2,0,7,0,2,1,7,1,2,2,7,2,1,0,1,0,1,0,1,0,1,0,1,0,1,
|
||||
0,1,0,1,1,4,1,17,8,1,11,1,12,1,18,1,2,4,2,22,8,2,11,2,12,2,23,1,
|
||||
2,1,2,0,0,3,1,1,3,2,5,3,1,0,2,2,0,65,90,97,122,3,0,9,10,13,13,32,
|
||||
32,28,0,1,1,0,0,0,0,3,1,0,0,0,0,5,1,0,0,0,1,7,1,0,0,0,3,16,1,0,0,
|
||||
0,5,21,1,0,0,0,7,8,5,109,0,0,8,9,5,105,0,0,9,10,5,110,0,0,10,11,
|
||||
5,105,0,0,11,12,5,109,0,0,12,13,5,97,0,0,13,14,5,108,0,0,14,2,1,
|
||||
0,0,0,15,17,7,0,0,0,16,15,1,0,0,0,17,18,1,0,0,0,18,16,1,0,0,0,18,
|
||||
19,1,0,0,0,19,4,1,0,0,0,20,22,7,1,0,0,21,20,1,0,0,0,22,23,1,0,0,
|
||||
0,23,21,1,0,0,0,23,24,1,0,0,0,24,25,1,0,0,0,25,26,6,2,0,0,26,6,1,
|
||||
0,0,0,3,0,18,23,1,6,0,0
|
||||
]
|
||||
|
||||
class week3Lexer(Lexer):
|
||||
|
||||
atn = ATNDeserializer().deserialize(serializedATN())
|
||||
|
||||
decisionsToDFA = [ DFA(ds, i) for i, ds in enumerate(atn.decisionToState) ]
|
||||
|
||||
T__0 = 1
|
||||
ID = 2
|
||||
WS = 3
|
||||
|
||||
channelNames = [ u"DEFAULT_TOKEN_CHANNEL", u"HIDDEN" ]
|
||||
|
||||
modeNames = [ "DEFAULT_MODE" ]
|
||||
|
||||
literalNames = [ "<INVALID>",
|
||||
"'minimal'" ]
|
||||
|
||||
symbolicNames = [ "<INVALID>",
|
||||
"ID", "WS" ]
|
||||
|
||||
ruleNames = [ "T__0", "ID", "WS" ]
|
||||
|
||||
grammarFileName = "week3.g4"
|
||||
|
||||
def __init__(self, input=None, output:TextIO = sys.stdout):
|
||||
super().__init__(input, output)
|
||||
self.checkVersion("4.13.2")
|
||||
self._interp = LexerATNSimulator(self, self.atn, self.decisionsToDFA, PredictionContextCache())
|
||||
self._actions = None
|
||||
self._predicates = None
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
T__0=1
|
||||
ID=2
|
||||
WS=3
|
||||
'minimal'=1
|
||||
@@ -0,0 +1,21 @@
|
||||
# Generated from ./week3.g4 by ANTLR 4.13.2
|
||||
from antlr4 import *
|
||||
if "." in __name__:
|
||||
from .week3Parser import week3Parser
|
||||
else:
|
||||
from week3Parser import week3Parser
|
||||
|
||||
# This class defines a complete listener for a parse tree produced by week3Parser.
|
||||
class week3Listener(ParseTreeListener):
|
||||
|
||||
# Enter a parse tree produced by week3Parser#start.
|
||||
def enterStart(self, ctx:week3Parser.StartContext):
|
||||
pass
|
||||
|
||||
# Exit a parse tree produced by week3Parser#start.
|
||||
def exitStart(self, ctx:week3Parser.StartContext):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
del week3Parser
|
||||
@@ -0,0 +1,94 @@
|
||||
# Generated from ./week3.g4 by ANTLR 4.13.2
|
||||
# encoding: utf-8
|
||||
from antlr4 import *
|
||||
from io import StringIO
|
||||
import sys
|
||||
if sys.version_info[1] > 5:
|
||||
from typing import TextIO
|
||||
else:
|
||||
from typing.io import TextIO
|
||||
|
||||
def serializedATN():
|
||||
return [
|
||||
4,1,3,6,2,0,7,0,1,0,1,0,1,0,1,0,0,0,1,0,0,0,4,0,2,1,0,0,0,2,3,5,
|
||||
1,0,0,3,4,5,2,0,0,4,1,1,0,0,0,0
|
||||
]
|
||||
|
||||
class week3Parser ( Parser ):
|
||||
|
||||
grammarFileName = "week3.g4"
|
||||
|
||||
atn = ATNDeserializer().deserialize(serializedATN())
|
||||
|
||||
decisionsToDFA = [ DFA(ds, i) for i, ds in enumerate(atn.decisionToState) ]
|
||||
|
||||
sharedContextCache = PredictionContextCache()
|
||||
|
||||
literalNames = [ "<INVALID>", "'minimal'" ]
|
||||
|
||||
symbolicNames = [ "<INVALID>", "<INVALID>", "ID", "WS" ]
|
||||
|
||||
RULE_start = 0
|
||||
|
||||
ruleNames = [ "start" ]
|
||||
|
||||
EOF = Token.EOF
|
||||
T__0=1
|
||||
ID=2
|
||||
WS=3
|
||||
|
||||
def __init__(self, input:TokenStream, output:TextIO = sys.stdout):
|
||||
super().__init__(input, output)
|
||||
self.checkVersion("4.13.2")
|
||||
self._interp = ParserATNSimulator(self, self.atn, self.decisionsToDFA, self.sharedContextCache)
|
||||
self._predicates = None
|
||||
|
||||
|
||||
|
||||
|
||||
class StartContext(ParserRuleContext):
|
||||
__slots__ = 'parser'
|
||||
|
||||
def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1):
|
||||
super().__init__(parent, invokingState)
|
||||
self.parser = parser
|
||||
|
||||
def ID(self):
|
||||
return self.getToken(week3Parser.ID, 0)
|
||||
|
||||
def getRuleIndex(self):
|
||||
return week3Parser.RULE_start
|
||||
|
||||
def enterRule(self, listener:ParseTreeListener):
|
||||
if hasattr( listener, "enterStart" ):
|
||||
listener.enterStart(self)
|
||||
|
||||
def exitRule(self, listener:ParseTreeListener):
|
||||
if hasattr( listener, "exitStart" ):
|
||||
listener.exitStart(self)
|
||||
|
||||
|
||||
|
||||
|
||||
def start(self):
|
||||
|
||||
localctx = week3Parser.StartContext(self, self._ctx, self.state)
|
||||
self.enterRule(localctx, 0, self.RULE_start)
|
||||
try:
|
||||
self.enterOuterAlt(localctx, 1)
|
||||
self.state = 2
|
||||
self.match(week3Parser.T__0)
|
||||
self.state = 3
|
||||
self.match(week3Parser.ID)
|
||||
except RecognitionException as re:
|
||||
localctx.exception = re
|
||||
self._errHandler.reportError(self, re)
|
||||
self._errHandler.recover(self, re)
|
||||
finally:
|
||||
self.exitRule()
|
||||
return localctx
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,27 +1,38 @@
|
||||
import sys
|
||||
|
||||
from antlr4 import *
|
||||
|
||||
from HelloLexer import HelloLexer
|
||||
from HelloParser import HelloParser
|
||||
from HelloListener import HelloListener
|
||||
|
||||
from antlr4.tree.Trees import Trees
|
||||
|
||||
from TreesUser import TreesUser # contains Marina's PrettyPrint()
|
||||
|
||||
def main(argv):
|
||||
if len(argv) == 1:
|
||||
InputStream = StdinStream()
|
||||
inpstream = StdinStream()
|
||||
else:
|
||||
inpname = argv[1]
|
||||
InputStream = FileStream(inpname)
|
||||
lexer = HelloLexer(InputStream)
|
||||
inpstream = FileStream(inpname)
|
||||
# The lexer analyses the characters on inpstream and produces tokens
|
||||
lexer = HelloLexer(inpstream)
|
||||
# on the token stream (belonging to a standard ANTLR class)
|
||||
tokstream = CommonTokenStream(lexer)
|
||||
# The parser produces an abstract syntax tree
|
||||
parser = HelloParser(tokstream)
|
||||
# When we execute the parse, we need to give it the starting rule of our grammar,
|
||||
listener = HelloListener()
|
||||
|
||||
tree = parser.start()
|
||||
# which in our case is 'greeting' (as defined in hello.g4)
|
||||
tree = parser.greeting()
|
||||
tree = parser.bye()
|
||||
|
||||
print(Trees.toStringTree(tree, None, parser))
|
||||
# traditional Lisp-style parse tree
|
||||
print(TreesUser.toStringTree(tree, None, parser))
|
||||
# pretty-print the parse tree
|
||||
print(TreesUser.PrettyPrint(tree, None, parser))
|
||||
if __name__ == '__main__':
|
||||
main(sys.argv)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user