Untitled

 avatar
unknown
plain_text
2 years ago
1.9 kB
14
Indexable
import pandas as pd

class node_stack:
  def __init__(self, symbol, lexeme):
    global count
    self.symbol = symbol
    self.lexeme = lexeme
    self.id = count + 1
    count += 1

class node_tree:
  def __init__(self, id, symbol, lexeme):
    self.id = id
    self.symbol = symbol
    self.lexeme = lexeme
    self.children = []
    self.father = None

tabla = pd.read_csv("tabla.csv", index_col = 0)
count = 0
stack = []

# init stack
symbol_E = node_stack('E', None)
symbol_dollar = node_stack('$', None)
stack.append(symbol_dollar)
stack.append(symbol_E)

# init tree
root = node_tree(symbol_E.id, symbol_E.symbol, symbol_E.lexeme)

input = [ 
          {"symbol":"int", "lexeme":"4", "nroline":2, "col":2},
          {"symbol":"+", "lexeme":"+", "nroline":2, "col":4},
          {"symbol":"int", "lexeme":"5", "nroline":2, "col":6},
          {"symbol":"$", "lexeme":"$", "nroline":0, "col":0},
        ]

index_input = 0
while len(stack) > 0:
  if input[index_input]["symbol"] not in tabla.columns:
    print("Error de sintaxis")
    break 
  if stack[-1].symbol == input[index_input]["symbol"]:
    stack.pop()
    index_input += 1
  else:
    production = tabla.loc[stack[-1].symbol, input[index_input]["symbol"]]
    print(production)
    if production != ('e'):
      stack.pop()
      for symbol in reversed(production.split()):
        node = node_stack(symbol, None)
        print ('produccion:', node.symbol)
        print ('input actual', input[index_input]["symbol"])
        stack.append(node)
        #child = node_tree(node.id, node.symbol, node.lexeme)
      #child.father = root
      #root.children.append(child)
    #root = child
    else:
      stack.pop()
  #print ('stack actual', stack[-1].symbol)
      
if len(stack) == 0:
  print("Parser exitoso")
  # llamar a la función "imprimir_arbol" para imprimir el árbol sintáctico resultante
else:
  print("Error de sintaxis")
Editor is loading...