diff --git a/Q1/Artefacts/RobotLang.g4 b/Q1/Artefacts/RobotLang.g4 index be57e12..42cf48b 100644 --- a/Q1/Artefacts/RobotLang.g4 +++ b/Q1/Artefacts/RobotLang.g4 @@ -18,6 +18,6 @@ logStat: 'LOG' STRING ';' ; repeatStat: 'REPEAT' NUMBER '{' stat+ '}' ; ID_PREFIX: '// STUDENT ID:' ; -NUMBER: [1-9]+ ; +NUMBER: [0-9]+ ; STRING: '"' .*? '"' ; WS: [ \t\r\n]+ -> skip ; \ No newline at end of file diff --git a/Q1/Artefacts/RobotLangVisitorUser.py b/Q1/Artefacts/RobotLangVisitorUser.py index 488929a..f965080 100644 --- a/Q1/Artefacts/RobotLangVisitorUser.py +++ b/Q1/Artefacts/RobotLangVisitorUser.py @@ -5,9 +5,7 @@ class RobotLangVisitorUser(RobotLangVisitor): 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): @@ -29,14 +27,13 @@ class RobotLangVisitorUser(RobotLangVisitor): return self.visitChildren(ctx) def visitLogStat(self, ctx): - val = ctx.STRING().getText() + val = ctx.STRING().getText()[1:-1] print(f"{self.student_id} is logging: {val}.") return self.visitChildren(ctx) def visitRepeatStat(self, ctx): times = int(ctx.NUMBER().getText()) for _ in range(times): - # Visit all statements inside the repeat block for stat_ctx in ctx.stat(): self.visit(stat_ctx) return None \ No newline at end of file diff --git a/Q1/Artefacts/main.py b/Q1/Artefacts/main.py index 8ccf574..44724ed 100644 --- a/Q1/Artefacts/main.py +++ b/Q1/Artefacts/main.py @@ -11,13 +11,9 @@ def main(argv): inpname = argv[1] inpstream = FileStream(inpname) lexer = RobotLangLexer(inpstream) - #Create a stream of tokens tokstream = CommonTokenStream(lexer) - #Parser produces the abstract syntax tree parser = RobotLangParser(tokstream) - #Start parsing from the top rule tree = parser.prog() - #execute script visitor = RobotLangVisitorUser() visitor.visit(tree)