diff --git a/.idea/git_toolbox_prj.xml b/.idea/git_toolbox_prj.xml new file mode 100644 index 0000000..02b915b --- /dev/null +++ b/.idea/git_toolbox_prj.xml @@ -0,0 +1,15 @@ + + + + + + + \ No newline at end of file diff --git a/Q1/Artefacts/RobotLang.g4 b/Q1/Artefacts/RobotLang.g4 index cd203d3..104c9e1 100644 --- a/Q1/Artefacts/RobotLang.g4 +++ b/Q1/Artefacts/RobotLang.g4 @@ -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 ; \ No newline at end of file diff --git a/Q1/Artefacts/RobotLangVisitorUser.py b/Q1/Artefacts/RobotLangVisitorUser.py index 608ec7d..488929a 100644 --- a/Q1/Artefacts/RobotLangVisitorUser.py +++ b/Q1/Artefacts/RobotLangVisitorUser.py @@ -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() diff --git a/Q1/Artefacts/main.py b/Q1/Artefacts/main.py index a03be5e..4333379 100644 --- a/Q1/Artefacts/main.py +++ b/Q1/Artefacts/main.py @@ -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__':