Add initial implementation of RobotLang interpreter and visitor pattern

This commit is contained in:
bobbert committed 2026-04-27 15:29:10 +01:00
1 parent 4929d6b01e
commit ea6d32b726
7 files changed
+114 -1

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
import sys
from antlr4 import *
from RobotLangLexer import RobotLangLexer
from RobotLangParser import RobotLangParser
from RobotLangVisitorUser import RobotLangVisitorUser
def main(argv):
# Determine if reading from a file or standard input
if len(argv) == 1:
inpstream = StdinStream()
else:
inpname = argv[9]
inpstream = FileStream(inpname)
# 1. Lexer analyses the characters
lexer = RobotLangLexer(inpstream)
# 2. Create a stream of tokens
tokstream = CommonTokenStream(lexer)
# 3. Parser produces the abstract syntax tree
parser = RobotLangParser(tokstream)
# 4. Start parsing from the top rule ('prog' in your grammar)
tree = parser.prog()
# Printing the parse tree using Marina's PrettyPrint and toStringTree methods (Not-needed for the assignment)
# print(TreesUser.toStringTree(tree, None, parser))
# print(TreesUser.PrettyPrint(tree, None, parser))
visitor = RobotLangVisitorUser("2320390")
visitor.visit(tree)
if __name__ == '__main__':
main(sys.argv)