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
+34 -16

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>
+5 -4
View File
@@ -1,7 +1,7 @@
grammar RobotLang;
prog: stat+ ;
prog: header stat+ ;
header: ID_PREFIX NUMBER ;
stat: moveStat
| grabStat
| releaseStat
@@ -17,6 +17,7 @@ waitStat: 'WAIT' NUMBER ';' ;
logStat: 'LOG' STRING ';' ;
repeatStat: 'REPEAT' NUMBER '{' stat+ '}' ;
ID_PREFIX: '// STUDENT ID:' ;
NUMBER: [0-9]+ ;
STRING: '"' .*? '"' ;
WS: [ \t\r\n]+ -> skip ;
STRING: '"' .*? '"' {self.text = self.text[1:-1]} ;
WS: [ \t\r\n]+ -> skip ;
+8 -2
View File
@@ -1,8 +1,14 @@
from RobotLangVisitor import RobotLangVisitor
class RobotLangVisitorUser(RobotLangVisitor):
def __init__(self, student_id="2320390"):
self.student_id = student_id
def __init__(self):
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):
val = ctx.NUMBER().getText()
+6 -10
View File
@@ -9,22 +9,18 @@ def main(argv):
if len(argv) == 1:
inpstream = StdinStream()
else:
inpname = argv[9]
inpname = argv[1]
inpstream = FileStream(inpname)
# 1. Lexer analyses the characters
# Lexer analyses the characters
lexer = RobotLangLexer(inpstream)
# 2. Create a stream of tokens
# Create a stream of tokens
tokstream = CommonTokenStream(lexer)
# 3. Parser produces the abstract syntax tree
# Parser produces the abstract syntax tree
parser = RobotLangParser(tokstream)
# 4. Start parsing from the top rule ('prog' in your grammar)
# Start parsing from the top rule
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 = RobotLangVisitorUser()
visitor.visit(tree)
if __name__ == '__main__':