diff --git a/Project-02-03/parser.out b/Project-02-03/parser.out index 0631c7f..c11e96d 100644 --- a/Project-02-03/parser.out +++ b/Project-02-03/parser.out @@ -572,8 +572,7 @@ state 28 (4) E -> E . AOP E (8) E -> E . SEMICOLON E - ! shift/reduce conflict for AOP resolved as shift - ! shift/reduce conflict for SEMICOLON resolved as shift + SEMICOLON reduce using rule 7 (E -> ID ASSIGN E .) $end reduce using rule 7 (E -> ID ASSIGN E .) RPAREN reduce using rule 7 (E -> ID ASSIGN E .) EQOP reduce using rule 7 (E -> ID ASSIGN E .) @@ -585,10 +584,9 @@ state 28 DO reduce using rule 7 (E -> ID ASSIGN E .) RBRACE reduce using rule 7 (E -> ID ASSIGN E .) AOP shift and go to state 8 - SEMICOLON shift and go to state 9 ! AOP [ reduce using rule 7 (E -> ID ASSIGN E .) ] - ! SEMICOLON [ reduce using rule 7 (E -> ID ASSIGN E .) ] + ! SEMICOLON [ shift and go to state 9 ] state 29 @@ -783,7 +781,6 @@ state 38 (4) E -> E . AOP E (8) E -> E . SEMICOLON E - SEMICOLON reduce using rule 1 (E -> LET D IN E .) $end reduce using rule 1 (E -> LET D IN E .) RPAREN reduce using rule 1 (E -> LET D IN E .) EQOP reduce using rule 1 (E -> LET D IN E .) @@ -795,9 +792,10 @@ state 38 DO reduce using rule 1 (E -> LET D IN E .) RBRACE reduce using rule 1 (E -> LET D IN E .) AOP shift and go to state 8 + SEMICOLON shift and go to state 9 ! AOP [ reduce using rule 1 (E -> LET D IN E .) ] - ! SEMICOLON [ shift and go to state 9 ] + ! SEMICOLON [ reduce using rule 1 (E -> LET D IN E .) ] state 39 @@ -1130,8 +1128,6 @@ WARNING: WARNING: Conflicts: WARNING: WARNING: shift/reduce conflict for ID in state 23 resolved as shift -WARNING: shift/reduce conflict for AOP in state 28 resolved as shift -WARNING: shift/reduce conflict for SEMICOLON in state 28 resolved as shift WARNING: shift/reduce conflict for EQOP in state 45 resolved as shift WARNING: shift/reduce conflict for LOP in state 45 resolved as shift WARNING: shift/reduce conflict for COMMA in state 56 resolved as shift diff --git a/Project-02-03/parsetab.py b/Project-02-03/parsetab.py index 5e1dc51..de9115a 100644 --- a/Project-02-03/parsetab.py +++ b/Project-02-03/parsetab.py @@ -6,9 +6,9 @@ _tabversion = '3.10' _lr_method = 'LALR' -_lr_signature = 'EleftSEMICOLONrightINleftCOMPleftEQOPleftAOPAOP ASSIGN COMMA COMP CONST DO ELSE EQOP FALSE ID IF IN LBRACE LET LOP LPAREN RBRACE RPAREN SEMICOLON THEN TRUE WHILEE : LET D IN EE : IDE : ID LPAREN A RPARENE : E AOP EE : LPAREN E RPARENE : CONSTE : ID ASSIGN EE : E SEMICOLON EE : IF B THEN E ELSE EE : WHILE B DO LBRACE E RBRACEA : EA : A COMMA ED : ID LPAREN V RPAREN LBRACE E RBRACED : D DV : IDV : V COMMA VB : E EQOP EB : E COMP EB : B EQOP BB : B LOP BB : TRUEB : FALSEB : LPAREN B RPAREN' +_lr_signature = 'ErightINleftSEMICOLONrightASSIGNleftCOMPleftEQOPleftAOPAOP ASSIGN COMMA COMP CONST DO ELSE EQOP FALSE ID IF IN LBRACE LET LOP LPAREN RBRACE RPAREN SEMICOLON THEN TRUE WHILEE : LET D IN EE : IDE : ID LPAREN A RPARENE : E AOP EE : LPAREN E RPARENE : CONSTE : ID ASSIGN EE : E SEMICOLON EE : IF B THEN E ELSE EE : WHILE B DO LBRACE E RBRACEA : EA : A COMMA ED : ID LPAREN V RPAREN LBRACE E RBRACED : D DV : IDV : V COMMA VB : E EQOP EB : E COMP EB : B EQOP BB : B LOP BB : TRUEB : FALSEB : LPAREN B RPAREN' -_lr_action_items = {'LET':([0,4,6,7,8,9,12,13,19,24,30,31,32,33,34,42,49,53,55,],[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,]),'ID':([0,2,4,6,7,8,9,10,12,13,19,23,24,25,30,31,32,33,34,42,49,51,53,55,60,],[3,11,3,3,3,3,3,11,3,3,3,11,3,39,3,3,3,3,3,3,3,39,3,3,-13,]),'LPAREN':([0,3,4,6,7,8,9,11,12,13,19,24,30,31,32,33,34,42,49,53,55,],[4,12,4,19,19,4,4,25,4,4,19,4,4,19,19,4,4,4,4,4,4,]),'CONST':([0,4,6,7,8,9,12,13,19,24,30,31,32,33,34,42,49,53,55,],[5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,]),'IF':([0,4,6,7,8,9,12,13,19,24,30,31,32,33,34,42,49,53,55,],[6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,]),'WHILE':([0,4,6,7,8,9,12,13,19,24,30,31,32,33,34,42,49,53,55,],[7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,]),'$end':([1,3,5,21,22,28,29,38,41,57,58,],[0,-2,-6,-4,-8,-7,-5,-1,-3,-9,-10,]),'AOP':([1,3,5,14,16,21,22,27,28,29,36,38,41,43,46,47,52,54,57,58,59,],[8,-2,-6,8,8,-4,8,8,8,-5,8,8,-3,8,8,8,8,8,8,-10,8,]),'SEMICOLON':([1,3,5,14,16,21,22,27,28,29,36,38,41,43,46,47,52,54,57,58,59,],[9,-2,-6,9,9,-4,-8,9,9,-5,9,-1,-3,9,9,9,9,9,9,-10,9,]),'RPAREN':([3,5,14,17,18,21,22,26,27,28,29,35,36,38,39,40,41,44,45,46,47,48,52,56,57,58,],[-2,-6,29,-21,-22,-4,-8,41,-11,-7,-5,48,29,-1,-15,50,-3,-19,-20,-17,-18,-23,-12,-16,-9,-10,]),'EQOP':([3,5,15,16,17,18,20,21,22,28,29,35,36,38,41,44,45,46,47,48,57,58,],[-2,-6,31,33,-21,-22,31,-4,-8,-7,-5,31,33,-1,-3,-19,31,-17,-18,-23,-9,-10,]),'COMP':([3,5,16,21,22,28,29,36,38,41,57,58,],[-2,-6,34,-4,-8,-7,-5,34,-1,-3,-9,-10,]),'COMMA':([3,5,21,22,26,27,28,29,38,39,40,41,52,56,57,58,],[-2,-6,-4,-8,42,-11,-7,-5,-1,-15,51,-3,-12,51,-9,-10,]),'ELSE':([3,5,21,22,28,29,38,41,43,57,58,],[-2,-6,-4,-8,-7,-5,-1,-3,53,-9,-10,]),'THEN':([3,5,15,17,18,21,22,28,29,38,41,44,45,46,47,48,57,58,],[-2,-6,30,-21,-22,-4,-8,-7,-5,-1,-3,-19,-20,-17,-18,-23,-9,-10,]),'LOP':([3,5,15,17,18,20,21,22,28,29,35,38,41,44,45,46,47,48,57,58,],[-2,-6,32,-21,-22,32,-4,-8,-7,-5,32,-1,-3,-19,32,-17,-18,-23,-9,-10,]),'DO':([3,5,17,18,20,21,22,28,29,38,41,44,45,46,47,48,57,58,],[-2,-6,-21,-22,37,-4,-8,-7,-5,-1,-3,-19,-20,-17,-18,-23,-9,-10,]),'RBRACE':([3,5,21,22,28,29,38,41,54,57,58,59,],[-2,-6,-4,-8,-7,-5,-1,-3,58,-9,-10,60,]),'ASSIGN':([3,],[13,]),'TRUE':([6,7,19,31,32,],[17,17,17,17,17,]),'FALSE':([6,7,19,31,32,],[18,18,18,18,18,]),'IN':([10,23,60,],[24,-14,-13,]),'LBRACE':([37,50,],[49,55,]),} +_lr_action_items = {'LET':([0,4,6,7,8,9,12,13,19,24,30,31,32,33,34,42,49,53,55,],[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,]),'ID':([0,2,4,6,7,8,9,10,12,13,19,23,24,25,30,31,32,33,34,42,49,51,53,55,60,],[3,11,3,3,3,3,3,11,3,3,3,11,3,39,3,3,3,3,3,3,3,39,3,3,-13,]),'LPAREN':([0,3,4,6,7,8,9,11,12,13,19,24,30,31,32,33,34,42,49,53,55,],[4,12,4,19,19,4,4,25,4,4,19,4,4,19,19,4,4,4,4,4,4,]),'CONST':([0,4,6,7,8,9,12,13,19,24,30,31,32,33,34,42,49,53,55,],[5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,]),'IF':([0,4,6,7,8,9,12,13,19,24,30,31,32,33,34,42,49,53,55,],[6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,]),'WHILE':([0,4,6,7,8,9,12,13,19,24,30,31,32,33,34,42,49,53,55,],[7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,]),'$end':([1,3,5,21,22,28,29,38,41,57,58,],[0,-2,-6,-4,-8,-7,-5,-1,-3,-9,-10,]),'AOP':([1,3,5,14,16,21,22,27,28,29,36,38,41,43,46,47,52,54,57,58,59,],[8,-2,-6,8,8,-4,8,8,8,-5,8,8,-3,8,8,8,8,8,8,-10,8,]),'SEMICOLON':([1,3,5,14,16,21,22,27,28,29,36,38,41,43,46,47,52,54,57,58,59,],[9,-2,-6,9,9,-4,-8,9,-7,-5,9,9,-3,9,9,9,9,9,9,-10,9,]),'RPAREN':([3,5,14,17,18,21,22,26,27,28,29,35,36,38,39,40,41,44,45,46,47,48,52,56,57,58,],[-2,-6,29,-21,-22,-4,-8,41,-11,-7,-5,48,29,-1,-15,50,-3,-19,-20,-17,-18,-23,-12,-16,-9,-10,]),'EQOP':([3,5,15,16,17,18,20,21,22,28,29,35,36,38,41,44,45,46,47,48,57,58,],[-2,-6,31,33,-21,-22,31,-4,-8,-7,-5,31,33,-1,-3,-19,31,-17,-18,-23,-9,-10,]),'COMP':([3,5,16,21,22,28,29,36,38,41,57,58,],[-2,-6,34,-4,-8,-7,-5,34,-1,-3,-9,-10,]),'COMMA':([3,5,21,22,26,27,28,29,38,39,40,41,52,56,57,58,],[-2,-6,-4,-8,42,-11,-7,-5,-1,-15,51,-3,-12,51,-9,-10,]),'ELSE':([3,5,21,22,28,29,38,41,43,57,58,],[-2,-6,-4,-8,-7,-5,-1,-3,53,-9,-10,]),'THEN':([3,5,15,17,18,21,22,28,29,38,41,44,45,46,47,48,57,58,],[-2,-6,30,-21,-22,-4,-8,-7,-5,-1,-3,-19,-20,-17,-18,-23,-9,-10,]),'LOP':([3,5,15,17,18,20,21,22,28,29,35,38,41,44,45,46,47,48,57,58,],[-2,-6,32,-21,-22,32,-4,-8,-7,-5,32,-1,-3,-19,32,-17,-18,-23,-9,-10,]),'DO':([3,5,17,18,20,21,22,28,29,38,41,44,45,46,47,48,57,58,],[-2,-6,-21,-22,37,-4,-8,-7,-5,-1,-3,-19,-20,-17,-18,-23,-9,-10,]),'RBRACE':([3,5,21,22,28,29,38,41,54,57,58,59,],[-2,-6,-4,-8,-7,-5,-1,-3,58,-9,-10,60,]),'ASSIGN':([3,],[13,]),'TRUE':([6,7,19,31,32,],[17,17,17,17,17,]),'FALSE':([6,7,19,31,32,],[18,18,18,18,18,]),'IN':([10,23,60,],[24,-14,-13,]),'LBRACE':([37,50,],[49,55,]),} _lr_action = {} for _k, _v in _lr_action_items.items(): @@ -27,27 +27,27 @@ for _k, _v in _lr_goto_items.items(): del _lr_goto_items _lr_productions = [ ("S' -> E","S'",1,None,None,None), - ('E -> LET D IN E','E',4,'p_E_let','triplayacc.py',25), - ('E -> ID','E',1,'p_E_id','triplayacc.py',29), - ('E -> ID LPAREN A RPAREN','E',4,'p_E_call','triplayacc.py',33), - ('E -> E AOP E','E',3,'p_E_aop','triplayacc.py',38), - ('E -> LPAREN E RPAREN','E',3,'p_E_paren','triplayacc.py',42), - ('E -> CONST','E',1,'p_E_const','triplayacc.py',47), - ('E -> ID ASSIGN E','E',3,'p_E_assign','triplayacc.py',51), - ('E -> E SEMICOLON E','E',3,'p_E_seq','triplayacc.py',55), - ('E -> IF B THEN E ELSE E','E',6,'p_E_if','triplayacc.py',59), - ('E -> WHILE B DO LBRACE E RBRACE','E',6,'p_E_while','triplayacc.py',63), - ('A -> E','A',1,'p_A_single','triplayacc.py',72), - ('A -> A COMMA E','A',3,'p_A_multiple','triplayacc.py',76), - ('D -> ID LPAREN V RPAREN LBRACE E RBRACE','D',7,'p_D_single','triplayacc.py',84), - ('D -> D D','D',2,'p_D_concat','triplayacc.py',88), - ('V -> ID','V',1,'p_V_single','triplayacc.py',96), - ('V -> V COMMA V','V',3,'p_V_multiple','triplayacc.py',100), - ('B -> E EQOP E','B',3,'p_B_eqop_E','triplayacc.py',108), - ('B -> E COMP E','B',3,'p_B_comp','triplayacc.py',112), - ('B -> B EQOP B','B',3,'p_B_eqop_B','triplayacc.py',116), - ('B -> B LOP B','B',3,'p_B_lop','triplayacc.py',120), - ('B -> TRUE','B',1,'p_B_true','triplayacc.py',124), - ('B -> FALSE','B',1,'p_B_false','triplayacc.py',128), - ('B -> LPAREN B RPAREN','B',3,'p_B_paren','triplayacc.py',132), + ('E -> LET D IN E','E',4,'p_E_let','triplayacc.py',27), + ('E -> ID','E',1,'p_E_id','triplayacc.py',31), + ('E -> ID LPAREN A RPAREN','E',4,'p_E_call','triplayacc.py',35), + ('E -> E AOP E','E',3,'p_E_aop','triplayacc.py',40), + ('E -> LPAREN E RPAREN','E',3,'p_E_paren','triplayacc.py',44), + ('E -> CONST','E',1,'p_E_const','triplayacc.py',49), + ('E -> ID ASSIGN E','E',3,'p_E_assign','triplayacc.py',53), + ('E -> E SEMICOLON E','E',3,'p_E_seq','triplayacc.py',57), + ('E -> IF B THEN E ELSE E','E',6,'p_E_if','triplayacc.py',61), + ('E -> WHILE B DO LBRACE E RBRACE','E',6,'p_E_while','triplayacc.py',65), + ('A -> E','A',1,'p_A_single','triplayacc.py',74), + ('A -> A COMMA E','A',3,'p_A_multiple','triplayacc.py',78), + ('D -> ID LPAREN V RPAREN LBRACE E RBRACE','D',7,'p_D_single','triplayacc.py',86), + ('D -> D D','D',2,'p_D_concat','triplayacc.py',90), + ('V -> ID','V',1,'p_V_single','triplayacc.py',98), + ('V -> V COMMA V','V',3,'p_V_multiple','triplayacc.py',102), + ('B -> E EQOP E','B',3,'p_B_eqop_E','triplayacc.py',110), + ('B -> E COMP E','B',3,'p_B_comp','triplayacc.py',114), + ('B -> B EQOP B','B',3,'p_B_eqop_B','triplayacc.py',118), + ('B -> B LOP B','B',3,'p_B_lop','triplayacc.py',122), + ('B -> TRUE','B',1,'p_B_true','triplayacc.py',126), + ('B -> FALSE','B',1,'p_B_false','triplayacc.py',130), + ('B -> LPAREN B RPAREN','B',3,'p_B_paren','triplayacc.py',134), ] diff --git a/Project-02-03/tramcodes/homework.tram b/Project-02-03/tramcodes/homework.tram new file mode 100644 index 0000000..4cfdd5e --- /dev/null +++ b/Project-02-03/tramcodes/homework.tram @@ -0,0 +1,71 @@ + GOTO L1 +L2: NOP + LOAD 0 0 + CONST 0 + EQ + IFZERO L4 + GOTO L6 +L7: NOP + LOAD 0 0 + LOAD 1 1 + MUL + LOAD 1 0 + ADD + RETURN +L6: NOP + CONST 2 + LOAD 1 0 + MUL + LOAD 0 0 + INVOKE 2 L7 0 + GOTO L5 +L4: NOP + LOAD 0 0 + CONST 1 + ADD +L5: NOP + RETURN +L3: NOP + LOAD 1 0 + CONST 0 + GT + IFZERO L9 + GOTO L11 +L8: NOP + LOAD 1 0 + CONST 0 + GT + IFZERO L10 + POP +L11: NOP + LOAD 0 0 + LOAD 0 0 + LOAD 1 0 + INVOKE 2 L2 1 + MUL + STORE 0 0 + LOAD 0 0 + POP + LOAD 1 0 + CONST 1 + SUB + STORE 1 0 + LOAD 1 0 + GOTO L8 +L9: NOP + CONST None +L10: NOP + POP + LOAD 0 0 + CONST 42 + ADD + RETURN +L1: NOP + CONST 1 + CONST 2 + INVOKE 2 L2 0 + POP + CONST 3 + CONST 3 + INVOKE 2 L3 0 + HALT diff --git a/Project-02-03/triplaprograms/homework.tripla b/Project-02-03/triplaprograms/homework.tripla new file mode 100644 index 0000000..8d79595 --- /dev/null +++ b/Project-02-03/triplaprograms/homework.tripla @@ -0,0 +1,16 @@ +let f(x, y) { + if (x == 0) then + let g(x, z) { + x * y + z + } in g(2 * y, x) + else + x + 1 + } + g (a, i) { + while (i > 0) do { + a = a * f(a, i); + i = i - 1 + }; + a + 42 + } +in f (1, 2); g(3, 3) diff --git a/Project-02-03/triplayacc.py b/Project-02-03/triplayacc.py index cf3fd41..04fcace 100644 --- a/Project-02-03/triplayacc.py +++ b/Project-02-03/triplayacc.py @@ -6,10 +6,10 @@ import ply.yacc as yacc import compiler as ast from triplalex import tokens -# Operator precedence precedence = ( - ('left', 'SEMICOLON'), ('right', 'IN'), + ('left', 'SEMICOLON'), + ('right', 'ASSIGN'), ('left', 'COMP'), ('left', 'EQOP'), ('left', 'AOP'), diff --git a/Project-02-03/vistram/tram.py b/Project-02-03/vistram/tram.py index 9c4b090..ab68c8e 100644 --- a/Project-02-03/vistram/tram.py +++ b/Project-02-03/vistram/tram.py @@ -145,10 +145,11 @@ class const(Instruction): def toString(self): return super().toString()+"CONST "+str(self.k) class store(Instruction): - def __init__(self, k, assigned_label=None): + def __init__(self, k, d, assigned_label=None): super().__init__(assigned_label=assigned_label) - self.k=k - self.d=d + self.k = k + self.d = d + def execute(self,tram): tram.stack[tram.spp(self.d,tram.pp,tram.fp)+self.k]=tram.stack[tram.top]