def p_assignExpr(p): '''AssignExpr : ID EQUALS expression COMMA AssignExpr | ID EQUALS expression SEMICOLON''' p[1] = [p[1], ast.newLeaf(p[1])] p[0] = [p[3][0], ast.newNode('=', p[3][1], p[1][1])] threeAddressCode.generate_icg('=', p[3][0], '', p[1][0]) p[0][1].printAST()
def p_term_div(p): '''term : term DIVIDE factor''' p[0] = [None, 0] flag = True if type(p[1][0]) == int and type(p[3][0]) == int: flag = False p[0][0] = p[1][0] / p[3][0] threeAddressCode.generate_icg('/', p[1][0], p[3][0], p[0][0]) if flag: p[0][0] = threeAddressCode.getLatestTemp() p[0][1] = ast.newNode('/', p[1][1], p[3][1])
def p_expression_minus(p): '''expression : expression MINUS term''' p[0] = [None, 0] flag = True if type(p[1][0]) == int and type(p[3][0]) == int: flag = False p[0][0] = p[1][0] - p[3][0] threeAddressCode.generate_icg('-', p[1][0], p[3][0], p[0][0]) if flag: p[0][0] = threeAddressCode.getLatestTemp() p[0][1] = ast.newNode('-', p[1][1], p[3][1])
def p_condition_gthan(p): '''condition : term LTHAN term''' p[0] = [None, ast.newNode('<', p[1][1], p[3][1])] threeAddressCode.generate_icg("<F", p[1][0], p[3][0], "goto S")
def p_condition_lthanequ(p): '''condition : term LTHANEQU term''' p[0] = [None, ast.newNode('<=', p[1][1], p[3][1])] threeAddressCode.generate_icg("<=F", p[1][0], p[3][0], "goto S")
def p_condition_notequ(p): '''condition : term NOTEQUALS term''' p[0] = [None, ast.newNode('!=', p[1][1], p[3][1])] threeAddressCode.generate_icg("!=F", p[1][0], p[3][0], "goto S")
def p_condition_equequ(p): '''condition : term EQUALSEQUALS term''' p[0] = [None, ast.newNode('==', p[1][1], p[3][1])] threeAddressCode.generate_icg("==F", p[1][0], p[3][0], "goto S")