Enhance RobotLang grammar and visitor to support student ID extraction

This commit is contained in:
bobbert committed 2026-04-27 16:09:28 +01:00
1 parent 3fc4eac47b
commit 936f807449
4 files changed
+33 -15

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GitToolBoxProjectSettings">
<option name="commitMessageIssueKeyValidationOverride">
<BoolValueOverride>
<option name="enabled" value="true" />
</BoolValueOverride>
</option>
<option name="commitMessageValidationEnabledOverride">
<BoolValueOverride>
<option name="enabled" value="true" />
</BoolValueOverride>
</option>
</component>
</project>
+4 -3
View File
@@ -1,7 +1,7 @@
grammar RobotLang; grammar RobotLang;
prog: stat+ ; prog: header stat+ ;
header: ID_PREFIX NUMBER ;
stat: moveStat stat: moveStat
| grabStat | grabStat
| releaseStat | releaseStat
@@ -17,6 +17,7 @@ waitStat: 'WAIT' NUMBER ';' ;
logStat: 'LOG' STRING ';' ; logStat: 'LOG' STRING ';' ;
repeatStat: 'REPEAT' NUMBER '{' stat+ '}' ; repeatStat: 'REPEAT' NUMBER '{' stat+ '}' ;
ID_PREFIX: '// STUDENT ID:' ;
NUMBER: [0-9]+ ; NUMBER: [0-9]+ ;
STRING: '"' .*? '"' ; STRING: '"' .*? '"' {self.text = self.text[1:-1]} ;
WS: [ \t\r\n]+ -> skip ; WS: [ \t\r\n]+ -> skip ;
+8 -2
View File
@@ -1,8 +1,14 @@
from RobotLangVisitor import RobotLangVisitor from RobotLangVisitor import RobotLangVisitor
class RobotLangVisitorUser(RobotLangVisitor): class RobotLangVisitorUser(RobotLangVisitor):
def __init__(self, student_id="2320390"): def __init__(self):
self.student_id = student_id self.student_id = "Unknown"
def visitHeader(self, ctx):
# Extract the number directly from the script
self.student_id = ctx.NUMBER().getText()
# Continue walking the rest of the tree
return self.visitChildren(ctx)
def visitMoveStat(self, ctx): def visitMoveStat(self, ctx):
val = ctx.NUMBER().getText() val = ctx.NUMBER().getText()
+6 -10
View File
@@ -9,22 +9,18 @@ def main(argv):
if len(argv) == 1: if len(argv) == 1:
inpstream = StdinStream() inpstream = StdinStream()
else: else:
inpname = argv[9] inpname = argv[1]
inpstream = FileStream(inpname) inpstream = FileStream(inpname)
# 1. Lexer analyses the characters # Lexer analyses the characters
lexer = RobotLangLexer(inpstream) lexer = RobotLangLexer(inpstream)
# 2. Create a stream of tokens # Create a stream of tokens
tokstream = CommonTokenStream(lexer) tokstream = CommonTokenStream(lexer)
# 3. Parser produces the abstract syntax tree # Parser produces the abstract syntax tree
parser = RobotLangParser(tokstream) parser = RobotLangParser(tokstream)
# 4. Start parsing from the top rule ('prog' in your grammar) # Start parsing from the top rule
tree = parser.prog() tree = parser.prog()
# Printing the parse tree using Marina's PrettyPrint and toStringTree methods (Not-needed for the assignment) visitor = RobotLangVisitorUser()
# print(TreesUser.toStringTree(tree, None, parser))
# print(TreesUser.PrettyPrint(tree, None, parser))
visitor = RobotLangVisitorUser("2320390")
visitor.visit(tree) visitor.visit(tree)
if __name__ == '__main__': if __name__ == '__main__':