def p_procedure_heading(t): """ procedure_heading : PROCEDURE identifier | PROCEDURE identifier LPAREN parameter_list RPAREN""" if len(t) == 3: t[0] = Node("procedure_head", t[2]) else: t[0] = Node("procedure_head", t[2], t[4])
def p_procedure_or_function_call(t): """ procedure_or_function_call : identifier LPAREN param_list RPAREN | identifier """ if len(t) == 2: t[0] = Node("function_call", t[1]) else: t[0] = Node("function_call", t[1], t[3])
def p_ifstatement(p): ''' ifstatement : IF LPAREN condition RPAREN controlbody %prec IFX | IF LPAREN condition RPAREN controlbody ELSE controlbody ''' if len(p) == 6: p[0] = SDTS(Node('IF', [p[3].node, p[5].node])) else: p[0] = SDTS(Node('IF', [p[3].node, p[5].node, p[7].node]))
def p_if_statement(t): """if_statement : IF expression THEN statement ELSE statement | IF expression THEN statement """ if len(t) == 5: t[0] = Node('if', t[2], t[4]) else: t[0] = Node('if', t[2], t[4], t[6])
def p_ifstatement(p): ''' ifstatement : IF LPAREN condition RPAREN controlbody %prec IFX | IF LPAREN condition RPAREN controlbody ELSE controlbody ''' if len(p) == 6: p[0] = Node('IF',[p[3],p[5]]) else: p[0] = Node('IF',[p[3],p[5],p[7]])
def p_root(p): """ root : ROOT start | ROOT KDELIMITER start KDELIMITER start """ if (len(p) == 3): # \sqrt{x} p[0] = Node(content=p[1], right=p[2]) elif (len(p) == 6): # \sqrt[4]{y} p[0] = Node(content=p[1], right=p[5], superscript=p[3])
def p_function_heading(t): """ function_heading : FUNCTION type | FUNCTION identifier COLON type | FUNCTION identifier LPAREN parameter_list RPAREN COLON type""" if len(t) == 3: t[0] = Node("function_head", t[2]) elif len(t) == 5: t[0] = Node("function_head", t[2], t[3]) else: t[0] = Node("function_head", t[2], t[4], t[7])
def p_condition(p): ''' condition : expression LT expression | expression GT expression | expression LE expression | expression GE expression | expression EQ expression | expression NE expression | condition AND condition | condition OR condition | NOT condition | LPAREN condition RPAREN ''' if len(p) == 2: p[0] = Node('NOT',[p[2]],p[2].is_const) if p[2] == '<': p[0] = Node('LT',[p[1],p[3]],p[1].is_const and p[3].is_const) elif p[2] == '>': p[0] = Node('GT',[p[1],p[3]],p[1].is_const and p[3].is_const) elif p[2] == '<=': p[0] = Node('LE',[p[1],p[3]],p[1].is_const and p[3].is_const) elif p[2] == '>=': p[0] = Node('GE',[p[1],p[3]],p[1].is_const and p[3].is_const) elif p[2] == '==': p[0] = Node('EQ',[p[1],p[3]],p[1].is_const and p[3].is_const) elif p[2] == '!=': p[0] = Node('NE',[p[1],p[3]],p[1].is_const and p[3].is_const) elif p[2] == '&&': p[0] = Node('AND',[p[1],p[3]],p[1].is_const and p[3].is_const) elif p[2] == '||': p[0] = Node('OR',[p[1],p[3]],p[1].is_const and p[3].is_const) else : p[0] = p[2]
def p_controlbody(p): ''' controlbody : LBRACE statements RBRACE | statement | SEMICOLON ''' if len(p) == 4: p[0] = p[2] elif p[1] == ';': p[0] = SDTS(Node('STMTS', [])) else: p[0] = SDTS(Node('STMTS', [p[1].node]))
def p_address(p): ''' address : AMPERSAND pointer | AMPERSAND address | AMPERSAND var ''' p[0] = Node('ADDR',[p[2]],False)
def p_cin(p): ''' cin : CIN DOBLEMAYORQUE repcin PUNTOYCOMA ''' p[0] = p[3] listacin = [] listacin2 = [] listacin3 = [] for x in p[0].split(">>"): i = 0 listacin.append(x) while i < len(listacin): comillas = '"' cin = "$(" + comillas + "#consola" + comillas + ").append(" + "Ejem_" todo = cin + listacin[ i] + "=prompt('Inserte el dato:',''));$('#consola').append('<br>');" #Ejem_"+listacin[i]+"=parseInt(Ejem_"+listacin[i]+"); if(isNaN("+ "Ejem_"+listacin[i]+")==true) "+ "Ejem_"+listacin[i]+" = 0; $('#consola').append('<br>');" listacin2 = todo i = i + 1 listacin3.append(listacin2) cadena = '' l = len(listacin3) for x in range(0, l): cadena = cadena + listacin3[x] p[0] = Node("cin", cadena)
def p_declaracion(p): '''declaracion : tipo ID IGUAL NUMBER PUNTOYCOMA ''' todo = "Ejem_" + str(p[2]) + " " + str(p[3]) + " " + str(p[4]) + " " + str( p[5]) p[1] = 'var' p[0] = Node("parametros", todo, p[1])
def p_declaracion_f_3(p): '''declaracionfun : tipo ID PARENTESISIZQUIERDO PARENTESISDERECHO PUNTOYCOMA | tipo ID PARENTESISIZQUIERDO paramdeclaraciones PARENTESISDERECHO PUNTOYCOMA ''' p[1] = "" todo = "" p[0] = Node("parametros", todo, p[1])
def p_contenidos(p): ''' contenidos : contenidos cout | contenidos cin | contenidos seleccion | contenidos iteracion | contenidos repeticion | contenidos operaciones | contenidos inc | contenidos dec | inc contenidos | dec contenidos | cout | cin | seleccion | iteracion | repeticion | operaciones | inc | dec | return | contenidos return ''' if len(p) == 3: p[0] = str(p[1]) + str(p[2]) sub = p[0] elif len(p) == 2: p[0] = p[1] sub = p[0] p[0] = Node("todo", sub)
def p_expression_s(t): """ expression_s : element | expression_s psign element""" if len(t) == 2: t[0] = t[1] else: t[0] = Node('op', t[2], t[1], t[3])
def p_repeticion(p): '''repeticion : FOR PARENTESISIZQUIERDO ciclos PUNTOYCOMA ciclos PUNTOYCOMA ciclos PARENTESISDERECHO LLAVEIZQUIERDA contenidos LLAVEDERECHA ''' contenido = str(p[1]) + str(p[2]) + "Ejem_" + str(p[3]) + str( p[4]) + " Ejem_" + str(p[5]) + str(p[6]) + str(p[7]) + str(p[8]) + str( p[9]) + str(p[10]) + str(p[11]) p[0] = Node("for", contenido)
def p_expression_m(t): """ expression_m : expression_s | expression_m sign expression_s""" if len(t) == 2: t[0] = t[1] else: t[0] = Node('op', t[2], t[1], t[3])
def p_todo(p): ''' todo : todo cout | todo declaracion | todo cin | todo dec | todo inc | todo seleccion | todo iteracion | todo repeticion | todo operaciones | todo callfun | todo return | todo powgen | powgen | return | declaracion | cout | cin | dec | inc | seleccion | iteracion | repeticion | operaciones | callfun ''' if p[1].type == "todo": if p[1] is None: sub = str(p[2]) else: sub = str(p[1].sub) + str(p[2]) else: sub = str(p[1]) p[0] = Node("todo", sub)
def p_ciclos(p): '''ciclos : ID IGUAL NUMBER | ID DOBLEIGUAL NUMBER | ID MENORQUE NUMBER | ID MAYORQUE NUMBER | ID MENOROIGUAL NUMBER | ID MAYOROIGUAL NUMBER | ID IGUAL ID | ID DOBLEIGUAL ID | ID MENORQUE ID | ID MAYORQUE ID | ID MENOROIGUAL ID | ID MAYOROIGUAL ID | RETURN | ID DISTINTODE NUMBER | ID DISTINTODE ID ''' if len(p) == 4: if str(p[3]).isdigit(): todo = str(p[1]) + str(p[2]) + str(p[3]) id = str(p[1]) else: todo = str(p[1]) + str(p[2]) + " Ejem_" + str(p[3]) id = str(p[1]) elif len(p) == 3: todo = str(p[1]) + +str(p[2]) + str(p[3]) id = str(p[1]) p[0] = Node("ciclos", todo, id)
def p_param_list(t): """ param_list : param_list COMMA param | param """ if len(t) == 2: t[0] = t[1] else: t[0] = Node("parameter_list", t[1], t[3])
def p_pointer(p): ''' pointer : TIMES pointer %prec STAR | TIMES address %prec STAR | TIMES var %prec STAR ''' p[0] = Node('DEREF',[p[2]],False)
def p_statement_sequence(t): """statement_sequence : statement SEMICOLON statement_sequence | statement""" if len(t) == 2: t[0] = t[1] else: t[0] = Node('statement_list', t[1], t[3])
def p_parameter_list(t): """ parameter_list : parameter COMMA parameter_list | parameter""" if len(t) == 4: t[0] = Node("parameter_list", t[1], t[3]) else: t[0] = t[1]
def p_expression(t): """expression : expression and_or expression_m | expression_m """ if len(t) == 2: t[0] = t[1] else: t[0] = Node('op', t[2], t[1], t[3])
def p_retstatement(p): ''' retstatement : RETURN expression SEMICOLON | RETURN SEMICOLON ''' ## Check the return type and the function return type if len(p) == 3 and glob.curr_sym_table.ftype != 'void': raiseExpectedReturn(glob.curr_sym_table.fname,glob.curr_sym_table.ftype,'void',0,0,glob.line_number) elif len(p) == 4: # p[2] is a tuple of type, indirection if glob.curr_sym_table.ftype != p[2].syminfo[0] or glob.curr_sym_table.findirection != p[2].syminfo[1]: raiseExpectedReturn(glob.curr_sym_table.fname,glob.curr_sym_table.ftype,p[2].syminfo[0],glob.curr_sym_table.findirection,p[2].syminfo[1],glob.line_number) if len(p) == 3: p[0] = SDTS(Node('RETURN',[])) else: p[0] = SDTS(Node('RETURN',[p[2].node]))
def createTree(self, l=[], level=0, parent=None): sentType = l[0] children = l[1:] node = Node(value=sentType, level=level) if parent: parent.addChild(node) if level == 0: self.tree.setStart(node) for elem in children: if type(elem) is tuple: self.createTree(l=elem, level=level + 1, parent=node) else: child = Node(level=level + 1, value=elem) node.addChild(child)
def p_pointer(p): ''' pointer : TIMES pointer %prec STAR | TIMES address %prec STAR | TIMES var %prec STAR ''' p[2].syminfo.indirection += 1 p[0] = SDTS(Node('DEREF', [p[2].node], False), p[2].syminfo)
def p_declaracion_2(p): '''declaracion : tipo ID IGUAL ID PUNTOYCOMA ''' if p[1] == "int": todo = "Ejem_" + str(p[2]) + " " + str(p[3]) + " Ejem_" + str( p[4]) + " " + str(p[5]) elif p[1] == "string": if p[2] == p[4]: p[4] = "0" todo = "Ejem_" + str(p[2]) + " " + str(p[3]) + " " + str( p[4]) + str(p[5]) p[0] = Node("parametros", todo, p[1]) else: todo = " Ejem_" + str(p[2]) + str(p[3]) + " Ejem_" + str( p[4]) + str(p[5]) p[0] = Node("parametros", todo, p[1]) p[0] = Node("parametros", todo)
def p_variable_declaration_list(t): """variable_declaration_list : variable_declaration variable_declaration_list | variable_declaration """ # function and procedure missing here if len(t) == 2: t[0] = t[1] else: t[0] = Node('var_list', t[1], t[2])
def p_element(t): """element : identifier | real | integer | string | char | LPAREN expression RPAREN | NOT element | function_call_inline """ if len(t) == 2: t[0] = Node("element", t[1]) elif len(t) == 3: # not e t[0] = Node('not', t[2]) else: # ( e ) t[0] = Node('element', t[2])