Add ANTLR grammar rules for 'bye' and 'goodbye' with parser and listener updates

This commit is contained in:
bobbert committed 2026-03-26 16:04:26 +00:00
1 parent 6205020013
commit d913d95b8b
9 files changed
+147 -29

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
import sys
from antlr4 import *
from week3Listener import week3Listener
from week3Lexer import week3Lexer
from week3Parser import week3Parser
from antlr4.tree.Trees import Trees
def main(argv):
if len(argv) == 1 :
InputStream = StdinStream()
else :
inpname = argv[1]
InputStream = FileStream(inpname)
# create lexer, token stream and parser using the selected input
lexer = week3Lexer(InputStream)
tokstream = CommonTokenStream(lexer)
parser = week3Parser(tokstream)
tree = parser.start()
print(Trees.toStringTree(tree, None, parser))
if __name__ == '__main__' :
main(sys.argv)
+1
View File
@@ -3,3 +3,4 @@ grammar week3;
start : 'minimal' ID ;
ID : [a-zA-Z]+ ;
WS : [ \t\r\n]+ -> skip ;
bye : ('bye'| 'goodbye') ID ;
+6 -1
View File
@@ -1,18 +1,23 @@
token literal names:
null
'minimal'
'bye'
'goodbye'
null
null
token symbolic names:
null
null
null
null
ID
WS
rule names:
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, 5, 11, 2, 0, 7, 0, 2, 1, 7, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 2, 0, 2, 0, 1, 1, 0, 2, 3, 8, 0, 4, 1, 0, 0, 0, 2, 7, 1, 0, 0, 0, 4, 5, 5, 1, 0, 0, 5, 6, 5, 4, 0, 0, 6, 1, 1, 0, 0, 0, 7, 8, 7, 0, 0, 0, 8, 9, 5, 4, 0, 0, 9, 3, 1, 0, 0, 0, 0]
+6 -2
View File
@@ -1,4 +1,8 @@
T__0=1
ID=2
WS=3
T__1=2
T__2=3
ID=4
WS=5
'minimal'=1
'bye'=2
'goodbye'=3
+7 -1
View File
@@ -1,17 +1,23 @@
token literal names:
null
'minimal'
'bye'
'goodbye'
null
null
token symbolic names:
null
null
null
null
ID
WS
rule names:
T__0
T__1
T__2
ID
WS
@@ -23,4 +29,4 @@ 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]
[4, 0, 5, 43, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 3, 4, 3, 33, 8, 3, 11, 3, 12, 3, 34, 1, 4, 4, 4, 38, 8, 4, 11, 4, 12, 4, 39, 1, 4, 1, 4, 0, 0, 5, 1, 1, 3, 2, 5, 3, 7, 4, 9, 5, 1, 0, 2, 2, 0, 65, 90, 97, 122, 3, 0, 9, 10, 13, 13, 32, 32, 44, 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, 1, 11, 1, 0, 0, 0, 3, 19, 1, 0, 0, 0, 5, 23, 1, 0, 0, 0, 7, 32, 1, 0, 0, 0, 9, 37, 1, 0, 0, 0, 11, 12, 5, 109, 0, 0, 12, 13, 5, 105, 0, 0, 13, 14, 5, 110, 0, 0, 14, 15, 5, 105, 0, 0, 15, 16, 5, 109, 0, 0, 16, 17, 5, 97, 0, 0, 17, 18, 5, 108, 0, 0, 18, 2, 1, 0, 0, 0, 19, 20, 5, 98, 0, 0, 20, 21, 5, 121, 0, 0, 21, 22, 5, 101, 0, 0, 22, 4, 1, 0, 0, 0, 23, 24, 5, 103, 0, 0, 24, 25, 5, 111, 0, 0, 25, 26, 5, 111, 0, 0, 26, 27, 5, 100, 0, 0, 27, 28, 5, 98, 0, 0, 28, 29, 5, 121, 0, 0, 29, 30, 5, 101, 0, 0, 30, 6, 1, 0, 0, 0, 31, 33, 7, 0, 0, 0, 32, 31, 1, 0, 0, 0, 33, 34, 1, 0, 0, 0, 34, 32, 1, 0, 0, 0, 34, 35, 1, 0, 0, 0, 35, 8, 1, 0, 0, 0, 36, 38, 7, 1, 0, 0, 37, 36, 1, 0, 0, 0, 38, 39, 1, 0, 0, 0, 39, 37, 1, 0, 0, 0, 39, 40, 1, 0, 0, 0, 40, 41, 1, 0, 0, 0, 41, 42, 6, 4, 0, 0, 42, 10, 1, 0, 0, 0, 3, 0, 34, 39, 1, 6, 0, 0]
+21 -14
View File
@@ -10,16 +10,21 @@ else:
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
4,0,5,43,6,-1,2,0,7,0,2,1,7,1,2,2,7,2,2,3,7,3,2,4,7,4,1,0,1,0,1,
0,1,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,2,1,2,1,2,1,2,1,2,1,2,1,
2,1,2,1,3,4,3,33,8,3,11,3,12,3,34,1,4,4,4,38,8,4,11,4,12,4,39,1,
4,1,4,0,0,5,1,1,3,2,5,3,7,4,9,5,1,0,2,2,0,65,90,97,122,3,0,9,10,
13,13,32,32,44,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,1,11,1,0,0,0,3,19,1,0,0,0,5,23,1,0,0,0,7,32,1,0,0,0,9,
37,1,0,0,0,11,12,5,109,0,0,12,13,5,105,0,0,13,14,5,110,0,0,14,15,
5,105,0,0,15,16,5,109,0,0,16,17,5,97,0,0,17,18,5,108,0,0,18,2,1,
0,0,0,19,20,5,98,0,0,20,21,5,121,0,0,21,22,5,101,0,0,22,4,1,0,0,
0,23,24,5,103,0,0,24,25,5,111,0,0,25,26,5,111,0,0,26,27,5,100,0,
0,27,28,5,98,0,0,28,29,5,121,0,0,29,30,5,101,0,0,30,6,1,0,0,0,31,
33,7,0,0,0,32,31,1,0,0,0,33,34,1,0,0,0,34,32,1,0,0,0,34,35,1,0,0,
0,35,8,1,0,0,0,36,38,7,1,0,0,37,36,1,0,0,0,38,39,1,0,0,0,39,37,1,
0,0,0,39,40,1,0,0,0,40,41,1,0,0,0,41,42,6,4,0,0,42,10,1,0,0,0,3,
0,34,39,1,6,0,0
]
class week3Lexer(Lexer):
@@ -29,20 +34,22 @@ class week3Lexer(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
ID = 4
WS = 5
channelNames = [ u"DEFAULT_TOKEN_CHANNEL", u"HIDDEN" ]
modeNames = [ "DEFAULT_MODE" ]
literalNames = [ "<INVALID>",
"'minimal'" ]
"'minimal'", "'bye'", "'goodbye'" ]
symbolicNames = [ "<INVALID>",
"ID", "WS" ]
ruleNames = [ "T__0", "ID", "WS" ]
ruleNames = [ "T__0", "T__1", "T__2", "ID", "WS" ]
grammarFileName = "week3.g4"
+6 -2
View File
@@ -1,4 +1,8 @@
T__0=1
ID=2
WS=3
T__1=2
T__2=3
ID=4
WS=5
'minimal'=1
'bye'=2
'goodbye'=3
+9
View File
@@ -17,5 +17,14 @@ class week3Listener(ParseTreeListener):
pass
# Enter a parse tree produced by week3Parser#bye.
def enterBye(self, ctx:week3Parser.ByeContext):
pass
# Exit a parse tree produced by week3Parser#bye.
def exitBye(self, ctx:week3Parser.ByeContext):
pass
del week3Parser
+63 -9
View File
@@ -10,8 +10,9 @@ 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,5,11,2,0,7,0,2,1,7,1,1,0,1,0,1,0,1,1,1,1,1,1,1,1,0,0,2,0,2,0,
1,1,0,2,3,8,0,4,1,0,0,0,2,7,1,0,0,0,4,5,5,1,0,0,5,6,5,4,0,0,6,1,
1,0,0,0,7,8,7,0,0,0,8,9,5,4,0,0,9,3,1,0,0,0,0
]
class week3Parser ( Parser ):
@@ -24,18 +25,22 @@ class week3Parser ( Parser ):
sharedContextCache = PredictionContextCache()
literalNames = [ "<INVALID>", "'minimal'" ]
literalNames = [ "<INVALID>", "'minimal'", "'bye'", "'goodbye'" ]
symbolicNames = [ "<INVALID>", "<INVALID>", "ID", "WS" ]
symbolicNames = [ "<INVALID>", "<INVALID>", "<INVALID>", "<INVALID>",
"ID", "WS" ]
RULE_start = 0
RULE_bye = 1
ruleNames = [ "start" ]
ruleNames = [ "start", "bye" ]
EOF = Token.EOF
T__0=1
ID=2
WS=3
T__1=2
T__2=3
ID=4
WS=5
def __init__(self, input:TokenStream, output:TextIO = sys.stdout):
super().__init__(input, output)
@@ -76,9 +81,58 @@ class week3Parser ( Parser ):
self.enterRule(localctx, 0, self.RULE_start)
try:
self.enterOuterAlt(localctx, 1)
self.state = 2
self.state = 4
self.match(week3Parser.T__0)
self.state = 3
self.state = 5
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
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(week3Parser.ID, 0)
def getRuleIndex(self):
return week3Parser.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 = week3Parser.ByeContext(self, self._ctx, self.state)
self.enterRule(localctx, 2, self.RULE_bye)
self._la = 0 # Token type
try:
self.enterOuterAlt(localctx, 1)
self.state = 7
_la = self._input.LA(1)
if not(_la==2 or _la==3):
self._errHandler.recoverInline(self)
else:
self._errHandler.reportMatch(self)
self.consume()
self.state = 8
self.match(week3Parser.ID)
except RecognitionException as re:
localctx.exception = re