def p_incDecArgs(p): '''incDecArgs : LPAREN ID COMMA expression RPAREN''' argsNode = TreeNode("args") id1Node = TreeNode(p[2]) id1Node.add_child(variables[p[2]][-1]) argsNode.add_children([id1Node, p[4]]) p[0] = argsNode
def p_structure(p): '''structure : COMMENT\ BEGIN optionalComment\ main\ game\ game\ game\ game\ END SEMICOLON''' keyList = ["main", "game1", "game2", "game3", "game4"] index = 0 for i in range(4, 9): node = TreeNode(keyList[index]) if p[i]: node.add_child(p[i]) astRoot.add_child(node) index += 1 p[0] = astRoot
def p_loop_for(p): 'loop : FOR expression TIMES USING ID statements FOREND SEMICOLON' loopNode = TreeNode("loop") loopNode.add_children([p[1], p[2], p[5]]) for child in funcList: loopNode.add_child(child) p[0] = loopNode funcList.clear()
def p_loop_dow(p): 'loop : DOW LPAREN expression RPAREN statements ENDDO SEMICOLON' loopNode = TreeNode("loop") loopNode.add_children([p[1], p[3]]) for child in funcList: loopNode.add_child(child) p[0] = loopNode funcList.clear()
def p_loop_forAssignWord(p): 'loop : FORASSIGNWORD LPAREN expression COMMA expression RPAREN DO\ statements FOREND SEMICOLON' loopNode = TreeNode("loop") loopNode.add_children([p[1], p[3], p[5]]) for child in funcList: loopNode.add_child(child) p[0] = loopNode funcList.clear()
# Last modified 19 /10 /19 # Description: Grammar for main structures # # TEC 2019 | CE3104 - Lenguajes, Compiladores e Interpretes # ------------------------------------------------------------- import ply.yacc as yacc # Get the token map from the lexer. from src.compiler.syntactic.Statements import * from src.compiler.lexer.Lexer import tokens from src.compiler.datastructures.TreeNode import * import copy symbolsTable = {} astRoot = TreeNode("structure") start = 'structure' # Main structure for program def p_structure(p): '''structure : COMMENT\ BEGIN optionalComment\ main\ game\ game\ game\ game\ END SEMICOLON''' keyList = ["main", "game1", "game2", "game3", "game4"]
def p_assignWordArgs(p): '''assignWordArgs : LPAREN ID COMMA ID RPAREN''' # Validar que primer parametro sea Texto y segundo sea entero. argsNode = TreeNode("args") id1Node = TreeNode(p[2]) id1Node.add_child(variables[p[2]][-1]) id2Node = TreeNode(p[4]) id2Node.add_child(variables[p[4]][-1]) argsNode.add_children([id1Node, id2Node]) p[0] = argsNode
def p_reservedFunction_assignword(p): 'reservedFunction : ASSIGNWORD assignWordArgs SEMICOLON' functionNode = TreeNode("function") functionNode.add_children([p[1], p[2]]) p[0] = functionNode
def p_reservedFunction_telaarana(p): 'reservedFunction : TELAARANA twoArgs SEMICOLON' functionNode = TreeNode("function") functionNode.add_children([p[1], p[2]]) p[0] = functionNode
def p_randomObjectArgs(p): '''randomObjectArgs : LPAREN expression COMMA ID LBRACKET ID RBRACKET COMMA expression RPAREN''' argsNode = TreeNode("args") id1Node = TreeNode(p[4]) id1Node.add_child(variables[p[4]][-1]) id2Node = TreeNode(p[6]) id2Node.add_child(variables[p[6]][-1]) argsNode.add_children([p[2], id1Node, id2Node, p[9]]) p[0] = argsNode
def p_reservedFunction_randomInFor(p): 'reservedFunction : RANDOM twoArgs SEMICOLON ' functionNode = TreeNode("function") functionNode.add_children([p[1], p[2]]) p[0] = functionNode
def p_reservedFunction_dec(p): 'reservedFunction : DEC incDecArgs SEMICOLON' functionNode = TreeNode("function") functionNode.add_children([p[1], p[2]]) p[0] = functionNode
def p_twoArgs(p): '''twoArgs : LPAREN ID COMMA ID RPAREN''' argsNode = TreeNode("args") id1Node = TreeNode(p[2]) id1Node.add_child(variables[p[2]][-1]) id2Node = TreeNode(p[4]) id2Node.add_child(variables[p[4]][-1]) argsNode.add_children([id1Node, id2Node]) p[0] = argsNode
def p_reservedFunction_balloon(p): 'reservedFunction : BALLOON twoArgs SEMICOLON' functionNode = TreeNode("function") functionNode.add_children([p[1], p[2]]) p[0] = functionNode
def p_reservedFunction_object(p): 'reservedFunction : OBJECT randomObjectArgs SEMICOLON' functionNode = TreeNode("function") functionNode.add_children([p[1], p[2]]) p[0] = functionNode