jcramalho

LP - anasin

May 13th, 2022 (edited)
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.20 KB | None | 0 0
  1. import ply.yacc as yacc
  2. from lp_lex import tokens, literals
  3.  
  4. def p_Program(p):
  5.     "Program : Declarations Statements"
  6.     p[0] = p[1] + "start\n" + p[2] + "stop\n"
  7.  
  8. def p_Declarations_list(p):
  9.     "Declarations : Declarations Declaration"
  10.     p[0] = p[1] + p[2]
  11.  
  12. def p_Declarations_empty(p):
  13.     "Declarations : "
  14.     p[0] = ""
  15.  
  16. def p_Declaration(p):
  17.     'Declaration : Type IdList'
  18.     res = ""
  19.     for id in p[2]:
  20.         if p[1] == 'int':
  21.             p.parser.ts[id] = {'type': 'int', 'pos': p.parser.contaPos}
  22.             res += "\tpushi 0\n"
  23.         else:
  24.             p.parser.ts[id] = {'type': 'str', 'pos': p.parser.contaPos}
  25.             res += "\tpushs \"\"\n"
  26.         p.parser.contaPos += 1
  27.     p[0] = res
  28.  
  29. def p_Type_int(p):
  30.     'Type : INT'
  31.     p[0] = p[1]
  32.  
  33. def p_Type_str(p):
  34.     'Type : STR'
  35.     p[0] = p[1]
  36.  
  37. def p_IdList_list(p):
  38.     "IdList : IdList ',' id"
  39.     p[0] = p[1] + [p[3]]
  40.  
  41. def p_IdList_single(p):
  42.     "IdList : id"
  43.     p[0] = [p[1]]
  44.  
  45. def p_Statements_list(p):
  46.     "Statements : Statements Statement"
  47.     p[0] = p[1] + p[2]
  48.  
  49. def p_Statements_single(p):
  50.     "Statements : Statement"
  51.     p[0] = p[1]
  52.  
  53. def p_Statement_atrib(p):
  54.     "Statement : id '=' Exp"
  55.     p[0] = p[3] + "\tstoreg " + str(p.parser.ts[p[1]]['pos']) + "\n"
  56.  
  57. def p_Statement_print(p):
  58.     "Statement : PRINT '(' PrintArgs ')'"
  59.     p[0] = p[3]
  60.  
  61. def p_PrintArgs_list(p):
  62.     "PrintArgs : PrintArgs ',' PrintArg"
  63.     p[0] = p[1] + p[3]
  64.  
  65. def p_PrintArgs_single(p):
  66.     "PrintArgs : PrintArg"
  67.     p[0] = p[1]
  68.    
  69. def p_PrintArg_str(p):
  70.     "PrintArg : str"
  71.     p[0] = "\tpushs " + p[1] + "\n\twrites\n"
  72.  
  73. def p_PrintArg_exp(p):
  74.     "PrintArg : Exp"
  75.     p[0] = p[1] + "\twritei\n"
  76.  
  77. def p_Exp_ad(p):
  78.     "Exp : Exp '+' Term"
  79.     p[0] = p[1] + p[3] + "\tadd\n"
  80.  
  81. def p_Exp_sub(p):
  82.     "Exp : Exp '-' Term"
  83.     p[0] = p[1] + p[3] + "\tsub\n"
  84.  
  85. def p_Exp_term(p):
  86.     "Exp : Term"
  87.     p[0] = p[1]
  88.  
  89. def p_Term_mul(p):
  90.     "Term : Term '*' Factor"
  91.     p[0] = p[1] + p[3] + "\tmul\n"
  92.  
  93. def p_Term_div(p):
  94.     "Term : Term '/' Factor"
  95.     p[0] = p[1] + p[3] + "\tdiv\n"
  96.  
  97. def p_Term_factor(p):
  98.     "Term : Factor"
  99.     p[0] = p[1]
  100.  
  101. def p_Factor_int(p):
  102.     "Factor : INT '(' Exp ')'"
  103.     p[0] = p[3] + "\tatoi\n"
  104.  
  105. def p_Factor_input(p):
  106.     "Factor : INPUT '(' str ')'"
  107.     p[0] = "\tpushs " + p[3] + "\n\twrites\n\tread\n"
  108.  
  109. def p_Factor_id(p):
  110.     "Factor : id"
  111.     p[0] = "\tpushg " + str(p.parser.ts[p[1]]['pos']) + "\n"
  112.  
  113. def p_Factor_num(p):
  114.     "Factor : num"
  115.     p[0] = "\tpushi " + p[1] + "\n"
  116.  
  117. def p_Statement_while(p):
  118.     "Statement : WHILE '(' Cond ')' CondStatements"
  119.     p.parser.contaWhiles += 1
  120.     p[0] = "while" + str(p.parser.contaWhiles) + ":\n"
  121.     p[0] += p[3] + "\tjz endwhile" + str(p.parser.contaWhiles) + "\n"
  122.     p[0] += p[5] + "\tjump while" + str(p.parser.contaWhiles) + "\n"
  123.     p[0] += "endwhile" + str(p.parser.contaWhiles) + ":\n"
  124.  
  125. def p_Statement_if(p):
  126.     "Statement : IF '(' Cond ')' CondStatements"
  127.     p.parser.contaIfs += 1
  128.     p[0] = p[3] + "\tjz endif" + str(p.parser.contaIfs) + "\n"
  129.     p[0] += p[5] + "endif" + str(p.parser.contaIfs) + ":\n"
  130.  
  131. def p_Statement_if_else(p):
  132.     "Statement : IF '(' Cond ')' CondStatements ELSE CondStatements"
  133.     p.parser.contaIfs += 1
  134.     p[0] = p[3] + "\tjz else" + str(p.parser.contaIfs) + "\n"
  135.     p[0] += p[5] + "\tjump endif" + str(p.parser.contaIfs) + "\n"
  136.     p[0] += "else" + str(p.parser.contaIfs) + ":\n" + p[7]
  137.     p[0] += "endif" + str(p.parser.contaIfs) + ":\n"
  138.  
  139. def p_CondStatements_simple(p):
  140.     "CondStatements : Statement"
  141.     p[0] = p[1]
  142.  
  143. def p_CondStatements_compound(p):
  144.     "CondStatements : '{' Statements '}'"
  145.     p[0] = p[2]
  146.  
  147. def p_Cond_or(p):
  148.     "Cond : Cond OR Cond2"
  149.     p[0] = p[1] + p[3] + "\tadd\n"
  150.  
  151. def p_Cond(p):
  152.     "Cond : Cond2"
  153.     p[0] = p[1]
  154.  
  155. def p_Cond2_and(p):
  156.     "Cond2 : Cond2 AND Cond3"
  157.     p[0] = p[1] + p[3] + "\tmul\n"
  158.  
  159. def p_Cond2(p):
  160.     "Cond2 : Cond3"
  161.     p[0] = p[1]
  162.  
  163. def p_Cond3_not(p):
  164.     "Cond3 : NOT ExpRel"
  165.     p[0] = p[2] + "\tnot\n"
  166.  
  167. def p_Cond3(p):
  168.     "Cond3 : ExpRel"
  169.     p[0] = p[1]
  170.  
  171. def p_Cond3_true(p):
  172.     "Cond3 : TRUE"
  173.     p[0] = "\tpushi 1\n"
  174.  
  175. def p_Cond3_false(p):
  176.     "Cond3 : FALSE"
  177.     p[0] = "\tpushi 0\n"
  178.  
  179. def p_ExpRel_gt(p):
  180.     "ExpRel : Exp '>' Exp"
  181.     p[0] = p[1] + p[3] + "\tsup\n"
  182.  
  183. def p_ExpRel_lt(p):
  184.     "ExpRel : Exp '<' Exp"
  185.     p[0] = p[1] + p[3] + "\tinf\n"
  186.  
  187. def p_ExpRel_ge(p):
  188.     "ExpRel : Exp GE Exp"
  189.     p[0] = p[1] + p[3] + "\tsupeq\n"
  190.  
  191. def p_ExpRel_le(p):
  192.     "ExpRel : Exp LE Exp"
  193.     p[0] = p[1] + p[3] + "\tinfeq\n"
  194.  
  195. def p_ExpRel_eq(p):
  196.     "ExpRel : Exp EQ Exp"
  197.     p[0] = p[1] + p[3] + "\tequal\n"
  198.  
  199. def p_ExpRel_neq(p):
  200.     "ExpRel : Exp NEQ Exp"
  201.     p[0] = p[1] + p[3] + "\tequal\n\tnot\n"
  202.  
  203. def p_error(p):
  204.     print('Erro sintático: ', p)
  205.     parser.success = False
  206.  
  207. # Build the parser
  208. parser = yacc.yacc()
  209. parser.ts = {}
  210. parser.contaPos = 0
  211. parser.contaIfs = 0
  212. parser.contaWhiles = 0
  213.  
  214. # Read line from input and parse it
  215. import sys
  216. parser.success = True
  217. program = sys.stdin.read()
  218. codigo = parser.parse(program)
  219. if parser.success:
  220.     print(parser.ts)
  221.     print(codigo)
  222. else:
  223.     print("Programa com erros... Corrija e tente novamente!")
Add Comment
Please, Sign In to add comment