def p_lookup(p): """lookup : load DOT NAME """ p[0] = Node("Lookup") p[0].var = p[1] p[0].var.ctl = "store" p[0].attr = p[3]
def t_Sstring(t): r"'.*?[^\\]'" t.type='STRING' x=Node('String') x.value=t.value[1:-1] t.value = x return t
def p_array(p): '''array : unary_expr list %prec LBRACKET | tuple list %prec LBRACKET ''' p[0]= Node('ARRAY') p[0].var = p[1] p[0].elems = p[2].elems
def p_fcall(p): '''fcall : unary_expr tuple | fcall tuple ''' p[0] = Node('FCALL') p[0].func = p[1] p[0].args = p[2].elems
def t_Dstring(t): r'".*?[^\\]"' t.type='STRING' x=Node('String') x.value=t.value[1:-1] t.value = x return t
def p_halfcblock(p): '''halfcblock : Lcurly | halfcblock pvar | halfcblock dot | halfcblock dvar | halfcblock spaces | halfcblock identifier | halfcblock newline | halfcblock cblock | halfcblock pblock | halfcblock colon | halfcblock string | halfcblock num | halfcblock eq | halfcblock lt | halfcblock gt | halfcblock not | halfcblock tab | halfcblock misc | halfcblock comma | halfcblock fcall ''' p[0] = Node('halfc','') if len(p)>2: p[0].args=p[1].args n=p[2] if isinstance(p[2],str): x= p[2] if x=='\n': x='newline' n=Node("token") n.str = x p[0].args.append(n) else: p[0].args= []
def p_for1(p): """for : FOR NAME IN load block """ p[0] = Node("ForEach") p[0].iter = p[2] p[0].list = p[4] p[0].body = p[5]
def p_fDef(p): """fDef : DEF NAME defTuple ARROW EXCLAMATION NAME block""" p[0] = Node("Fdef") p[0].func = p[2] p[0].args = p[3] p[0].rettype = p[6] p[0].body = p[7]
def p_for3(p): """for : FOR NAME EQ range block """ p[0] = Node("For") p[0].iter = p[2] p[0].range = p[4] p[0].body = p[5]
def p_array(p): '''array : unary_expr list %prec LBRACKET | tuple list %prec LBRACKET ''' p[0]= Node('Subscript') p[0].var = p[1] p[0].slice = p[2]
def p_if2(p): '''if : IF expr block elif | IF expr block else ''' p[0] = Node('IF') p[0].cond = p[1] p[0].body=p[3].body p[0].orelse = p[4]
def p_ptr(p): '''ptr : AMP unary_expr | AMP tuple | AMP list ''' p[0] =Node('PTR') p[0].var = p[2]
def p_assign4(p): '''assign : commalist_expr EQ commalist_expr ''' p[0] = Node('Assign') p[0].target =Node('tuple') p[0].target.elems =p[1].elems p[0].value =Node('tuple') p[0].value.elems =p[3].elems
def t_COMPLEXF(t): r'\d+\.\d+i' x = Node('Complex') x.real = 0 x.imag = float(t.value[:-1]) t.type='COMPLEX' t.value=x return t
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_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_elif2(p): '''elif : ELIF expr block elif | ELIF expr block else ''' p[0] =Node('IF') p[0].cond = p[2] p[0].body = p[3].body p[0].orelse = p[4]
def p_lookup(p): '''lookup : unary_expr DOT IDENTIFIER %prec DOT | list DOT IDENTIFIER %prec DOT | tuple DOT IDENTIFIER %prec DOT ''' p[0] = Node('LOOKUP') p[0].var = p[1] p[0].attr = p[3]
def p_foreach(p): '''foreach : FOR IDENTIFIER IN expr block | FOR vdef IN expr block ''' p[0] = Node('Foreach') p[0].iter = p[2] p[0].target = p[4] p[0].body = p[5].body
def p_arrayAccess1(p): """arrayAccess : load LBRACKET expr RBRACKET | load LBRACKET range RBRACKET """ p[0] = Node("ArrayAccess") p[0].var = p[1] p[0].var.ctl = "store" p[0].args = p[3]
def p_assign(p): """assign : store EQ expr | varDef EQ expr | varDef EQ assign | store EQ assign """ p[0] = Node("Assign") p[0].left = p[1] p[0].right = p[3]
def p_fdef(p): '''fdef : DEF IDENTIFIER tuple block ''' p[0] = Node('FuncDef') p[0].name = p[2].value p[0].args = Node('Arguments') p[0].args.args = p[3].elems p[0].body = p[4].body
def p_atptr(p): '''atptr : AT unary_expr | AT vdef | AT tuple | AT list ''' p[0] = Node('AT') etype = p[1].type p[0].ptr = p[2]
def p_for2(p): """for : FOR nameList IN load block """ x = Node("Tuple") p[0] = Node("ForEach") p[0].iter = x p[0].list = p[4] p[0].body = p[5] p[0].iter.elems = p[2]
def p_logical_expr1(p): '''logical_expr : expr AND expr | expr OR expr | expr XOR expr ''' p[0] = Node('COMPARE') p[0].left = p[1] p[0].op = p[2] p[0].right = p[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] = Node('IF',[p[3],p[5]]) else: p[0] = Node('IF',[p[3],p[5],p[7]])
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_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 t_NAME(t): r"[a-zA-Z_]([a-zA-Z0-9_])*" if t.value in keyword_map: t.type=keyword_map[t.value] else: x = Node('Name') x.value=t.value t.value = x return t
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_cast(p): '''cast : list tuple %prec AT | list list %prec AT | list unary_expr %prec AT ''' p[0] = Node('CAST') if len(p[1].elems)>1: print 'Syntax error while type casting at %s'%p.lineno(1) p[0].ctype = p[1].elems[0] p[0].value = p[2]
def p_vdef1(p): '''vdef : IDENTIFIER unary_expr %prec DEF | ptr unary_expr %prec DEF | fptr unary_expr %prec DEF | tuple unary_expr %prec DEF ''' p[0] = Node('VDEF') t = p[1] p[0].datatype = t p[0].var = p[2]
def p_commalist_def1(p): '''commalist_def : vdef COMMA vdef | vdef COMMA assign | vdef COMMA unary_expr | assign COMMA vdef | assign COMMA assign | assign COMMA unary_expr ''' p[0] = Node('COMMALIST') p[0].elems = [p[1],p[3]]
def p_arith_expr1(p): '''arith_expr : expr PLUS expr | expr MINUS expr | expr MULT expr | expr DIV expr ''' p[0] = Node('Arith') p[0].left = p[1] p[0].op = p[2] p[0].right = 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_Rparen(p): '''Rparen : rparen | spaces Rparen ''' if len(p)>2: s = p[1].str s+=p[2].str else: s=p[1] p[0] = Node('Rparen') p[0].str=s
def p_varDef(p): """varDef : type NAME | type NAME LBRACKET INT RBRACKET """ p[0] = Node("VarDef") p[0].dtype = p[1] p[0].var = p[2] if len(p) > 4: p[0].size = p[4].value else: p[0].size = None
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_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_address(p): ''' address : AMPERSAND pointer | AMPERSAND address | AMPERSAND var ''' p[0] = Node('ADDR',[p[2]],False)
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_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_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_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_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_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_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_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_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_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_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_pointer(p): ''' pointer : TIMES pointer %prec STAR | TIMES address %prec STAR | TIMES var %prec STAR ''' p[0] = Node('DEREF',[p[2]],False)
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_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 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_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_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])
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])