diff --git a/.idea/K2320390-Cl6115-CW2-DYOPL.iml b/.idea/K2320390-Cl6115-CW2-DYOPL.iml index 893457f..2add4fa 100644 --- a/.idea/K2320390-Cl6115-CW2-DYOPL.iml +++ b/.idea/K2320390-Cl6115-CW2-DYOPL.iml @@ -4,7 +4,7 @@ - + \ No newline at end of file diff --git a/Q1/Artefacts/MyScript.txt b/Q1/Artefacts/MyScript.txt new file mode 100644 index 0000000..105c028 --- /dev/null +++ b/Q1/Artefacts/MyScript.txt @@ -0,0 +1,17 @@ +// STUDENT ID: 2320390 +WAIT 1; +RELEASE; +LOG "Log entry 592"; +REPEAT 3 { +GRAB; +GRAB; +} +REPEAT 3 { +GRAB; +LOG "Finished 962"; +} +REPEAT 4 { +WAIT 2; +MOVE 7; +MOVE 14; +} diff --git a/Q1/Artefacts/RobotLang.g4 b/Q1/Artefacts/RobotLang.g4 new file mode 100644 index 0000000..cd203d3 --- /dev/null +++ b/Q1/Artefacts/RobotLang.g4 @@ -0,0 +1,22 @@ +grammar RobotLang; + +prog: stat+ ; + +stat: moveStat + | grabStat + | releaseStat + | waitStat + | logStat + | repeatStat + ; + +moveStat: 'MOVE' NUMBER ';' ; +grabStat: 'GRAB' ';' ; +releaseStat: 'RELEASE' ';' ; +waitStat: 'WAIT' NUMBER ';' ; +logStat: 'LOG' STRING ';' ; +repeatStat: 'REPEAT' NUMBER '{' stat+ '}' ; + +NUMBER: [0-9]+ ; +STRING: '"' .*? '"' ; +WS: [ \t\r\n]+ -> skip ; diff --git a/Q1/Artefacts/RobotLangVisitorUser.py b/Q1/Artefacts/RobotLangVisitorUser.py new file mode 100644 index 0000000..0b3b239 --- /dev/null +++ b/Q1/Artefacts/RobotLangVisitorUser.py @@ -0,0 +1,43 @@ +from antlr4 import * +if "." in __name__: + from .RobotLangParser import RobotLangParser +else: + from RobotLangParser import RobotLangParser + +class RobotLangVisitorUser(RobotLangVisitor): + def __init__(self, student_id="2320390"): + self.student_id = student_id + + def visitMoveStat(self, ctx): + val = ctx.NUMBER().getText() + print(f"{self.student_id} is moving by {val}.") + return self.visitChildren(ctx) + + def visitGrabStat(self, ctx): + print(f"{self.student_id} is grabbing.") + return self.visitChildren(ctx) + + def visitReleaseStat(self, ctx): + print(f"{self.student_id} is releasing.") + return self.visitChildren(ctx) + + def visitWaitStat(self, ctx): + val = ctx.NUMBER().getText() + print(f"{self.student_id} is waiting for {val}.") + return self.visitChildren(ctx) + + def visitLogStat(self, ctx): + val = ctx.STRING().getText() + 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 + + +del RobotLangParser \ No newline at end of file diff --git a/Q1/Artefacts/main.py b/Q1/Artefacts/main.py new file mode 100644 index 0000000..a03be5e --- /dev/null +++ b/Q1/Artefacts/main.py @@ -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) \ No newline at end of file diff --git a/Q1/Q1.docx b/Q1/Q1.docx index 8d2cb78..ad02b1a 100644 Binary files a/Q1/Q1.docx and b/Q1/Q1.docx differ diff --git a/Q2/Q2.docx b/Q2/Q2.docx index e69de29..cdbbd6e 100644 Binary files a/Q2/Q2.docx and b/Q2/Q2.docx differ