mirror of
https://github.com/mudabbir-ahmad/UNI-PROG3-CW2-DYOPL.git
synced 2026-10-07 20:00:20 +00:00
Q2 prep
This commit is contained in:
1 parent
b3fd98703a
commit
d20c595e0f
10 files changed
+204
No files matched your search
@@ -0,0 +1,64 @@
|
||||
# main.py
|
||||
"""
|
||||
Parse and execute statements either in stdin or in file
|
||||
"""
|
||||
|
||||
import sys
|
||||
|
||||
from antlr4 import *
|
||||
from fspowLexer import fspowLexer
|
||||
from fspowParser import fspowParser
|
||||
# from fspowListener import fspowListener
|
||||
# from antlr4.tree.Trees import Trees
|
||||
from TreesUser import TreesUser
|
||||
from Q2.Artefacts.fspowVisitorUser import fspowVisitorUser
|
||||
|
||||
|
||||
def main(argv):
|
||||
# argv[1] s/be name of source file
|
||||
# if missing, use stdin
|
||||
if len(argv) == 1:
|
||||
# StdinStream is a standard ANTLR class read by the lexer
|
||||
# (It is not a file descriptor)
|
||||
inpstream = StdinStream()
|
||||
else:
|
||||
inpname = argv[1]
|
||||
# FileStream is a standard ANTLR class read by the lexer
|
||||
# (It is not a file descriptor)
|
||||
inpstream = FileStream(inpname)
|
||||
|
||||
|
||||
|
||||
# The lexer analyses the characters on inpstream and produces tokens
|
||||
lexer = fspowLexer(inpstream)
|
||||
# .. on the token stream (belonging to a standard ANTLR class)
|
||||
tokstream = CommonTokenStream(lexer)
|
||||
|
||||
# The parser produces an abstract syntax tree
|
||||
parser = fspowParser(tokstream)
|
||||
# When we execute the parse, we need to give it the starting rule of our grammar,
|
||||
# which in our case is 'start' (as defined in fspow.g4),
|
||||
# so here we invoke the method named after this rule: start()
|
||||
tree = parser.prog()
|
||||
|
||||
# print the parse tree
|
||||
#print(TreesUser.toStringTree(tree, None, parser))
|
||||
print(TreesUser.PrettyPrint(tree, None, parser))
|
||||
|
||||
visitor = fspowVisitorUser()
|
||||
visitor.visit(tree)
|
||||
|
||||
# The listener defines the actions to take upon entering an exiting nodes for
|
||||
# specific tokens on the AST
|
||||
# listener = fspowListener()
|
||||
|
||||
# A ParseTreeWalker is annother standard ANTLR class, that performs a walk on the AST
|
||||
# walker = ParseTreeWalker()
|
||||
|
||||
# Get the tree walker to perform a walk on the AST, getting the listener to perform
|
||||
# the appropriate actions
|
||||
# walker.walk~(listener, tree)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.argv)
|
||||
Reference in new issue
Block a user