def parse(): string = request.form['string'] tokenList = scanner.parse(app.typedef, string, ["space"]) pt, log = Parser.parse(tokenList, app.typedef, app.cfg, app.action, app.goto, needLog=True) return { 'pt': str(pt), 'log': log }
elif actionType == 2: if needLog: log.append((str(stateStack), str(nodeStack), "ACCEPTED")) break else: assert False # print(stateStack, nodeStack, tokenType, actionType, nextState) # print(nodeStack) if needLog: return ParseTree(nodeStack[-1]), log return ParseTree(nodeStack[-1]) if __name__ == "__main__": typedef = TypeDefinition.load(FOLDER + "typedef") cfg = ContextFreeGrammar.load(typedef, FOLDER + "simpleJavaCFG") # action.save(FOLDER + "simpleJavaAction") # goto.save(FOLDER + "simpleJavaGoto") # exit() action = Action.load(cfg, FOLDER + "simpleJavaAction") goto = Goto.load(cfg, FOLDER + "simpleJavaGoto") with open(FOLDER + "test.sjava", "r") as f: src = f.read() tokenList = scanner.parse(typedef, src, ['line_comment', 'block_comment', 'space']) print(tokenList) pt = parse(tokenList, typedef, cfg, action, goto) print(pt)
def run(): global currentPointer, memoriaEjecucion, constantes, inner, funcionesDir direccion = 90 offsetDireccion = 0 scanner.parse() scanner.cuadruplos.append(['FIN', -1, -1, -1]) cargaDatosEnMemoria() memoriaEjecucion[3] = constantes cuadruploActual = cuadruplos[currentPointer - 1] currentPointer += 1 while cuadruploActual[0] != 'FIN': instruccion = cuadruploActual[0] if (instruccion < 12): operacion(cuadruploActual) elif (instruccion == 12): saveValueMemory(getMemoryValue(cuadruploActual[1]), cuadruploActual[3]) elif (instruccion == 13): currentPointer = int(cuadruploActual[3]) - 1 elif (instruccion == 14): if (getMemoryValue(cuadruploActual[1]) == False): currentPointer = cuadruploActual[3] - 1 elif (instruccion == 15): print "IMPRIME: ", getMemoryValue(cuadruploActual[3]) elif (instruccion == 16): turtle.right(270) turtle.fd(int(getMemoryValue(cuadruploActual[3]))) elif (instruccion == 17): turtle.right(90) turtle.fd(int(getMemoryValue(cuadruploActual[3]))) elif (instruccion == 18): turtle.right(180) turtle.fd(int(getMemoryValue(cuadruploActual[3]))) elif (instruccion == 19): turtle.right(0) turtle.fd(int(getMemoryValue(cuadruploActual[3]))) elif (instruccion == 20): reiniciaEra() currentPointer = memoriesStack.pop() tipoFuncion.pop() memoriaParametros() elif (instruccion == 21): iniciaEra() inner = 1 tipoFuncion.append(cuadruploActual[3]) elif (instruccion == 22): memoriesStack.append(currentPointer) inner = 0 currentPointer = funcionesDir[cuadruploActual[3]]['cuadruplo'] - 1 memoriaParametros() elif (instruccion == 23): tipo = funcionesDir[tipoFuncion[-1]]['parametros'][ cuadruploActual[3] - 1] temp = getMemoryParam(tipo) setMemoryParameter(temp, cuadruploActual[1]) elif (instruccion == 24): saveValueMemory(getMemoryValue(cuadruploActual[3]), funcionesDir[tipoFuncion[-1]]['memoria']) reiniciaEra() currentPointer = memoriesStack.pop() tipoFuncion.pop() elif (instruccion == 25): valor = getMemoryValue(cuadruploActual[1]) if (not (int(valor) >= int(cuadruploActual[2]) and int(valor) <= int(cuadruploActual[3]))): print("error") break elif (instruccion == 26): value = int(getMemoryValue(cuadruploActual[3])) offset = int(int(getMemoryValue(cuadruploActual[3])) / 2) direccion = turtle.heading() turtle.penup() turtle.fd(offset) turtle.pendown() turtle.right(90) turtle.fd(offset) turtle.right(90) turtle.fd(value) turtle.right(90) turtle.fd(value) turtle.right(90) turtle.fd(value) turtle.right(90) turtle.fd(offset) turtle.penup() turtle.setheading(direccion) turtle.back(offset) turtle.pendown() elif (instruccion == 27): offset = int(int(getMemoryValue(cuadruploActual[3])) / 2) direccion = turtle.heading() turtle.penup() turtle.right(90) turtle.fd(offset) turtle.left(90) turtle.pendown() turtle.circle(offset) turtle.penup() turtle.left(90) turtle.fd(offset) turtle.right(90) turtle.setheading(direccion) turtle.pendown() elif (instruccion == 28): value1 = int(getMemoryValue(cuadruploActual[2])) value2 = int(getMemoryValue(cuadruploActual[3])) offset1 = int(value1 / 2) offset2 = int(value2 / 2) direccion = turtle.heading() turtle.penup() turtle.fd(offset1) turtle.pendown() turtle.right(90) turtle.fd(offset2) turtle.right(90) turtle.fd(value1) turtle.right(90) turtle.fd(value2) turtle.right(90) turtle.fd(value1) turtle.right(90) turtle.fd(offset2) turtle.right(90) turtle.setheading(direccion) turtle.penup() turtle.back(offset1) turtle.pendown() elif (instruccion == 29): turtle.left(int(getMemoryValue(cuadruploActual[3]))) # # elif(instruccion == 29): # side1=int(getMemoryValue(cuadruploActual[1])) # side2=int(getMemoryValue(cuadruploActual[2])) # side3=int(getMemoryValue(cuadruploActual[3])) # if((side1+side2>side3) and (side1+side3 > side2) and (side2 + side3 > side1)): # cos3 = -((side3*side3) - (side1*side1) - (side2*side2))/(2*side1*side2) # ang3 = math.acos(cos3) # cos2 = -((side2*side2) - (side1*side1) - (side3*side3))/(2*side1*side3) # ang2 = math.acos(cos2) # ang2 = (ang2 * 180) / math.pi # ang3 = (ang3 * 180) / math.pi # ang1 = 180 - ang2 - ang3 # direccion = turtle.heading() # s = (side1 + side2 + side3)/2 # area = math.sqrt(s*(s-side1)*(s-side2)*(s-side3)) # base = max(side1,side2,side3) # h = (area*2)/base # turtle.penup() # turtle.fd(h/2) # turtle.pendown() # if(base != side1 and base != side2): # print ang1,ang2,ang3 # turtle.left(180 - (ang3/2)) # turtle.fd(side1) # turtle.left(180-ang2) # turtle.fd(side3) # turtle.left(180 - ang1) # turtle.fd(side2) # turtle.left(180 - (ang3 / 2)) # # elif(base != side2 and base != side3): # print ang1, ang2, ang3 # turtle.left(180 - (ang3 / 2)) # turtle.fd(side2) # turtle.left(180 - ang1) # turtle.fd(side3) # turtle.left(180 - ang2) # turtle.fd(side1) # turtle.left(180 - (ang3 / 2)) # else: # print ang1, ang2, ang3 # turtle.left(180 - (ang3 / 2)) # turtle.fd(side1) # turtle.left(180 - ang2) # turtle.fd(side3) # turtle.left(180 - ang1) # turtle.fd(side2) # turtle.left(180 - (ang3 / 2)) # turtle.setheading(direccion) # turtle.back(h/2) # # else: # turtle.penup() # turtle.back(250) # turtle.pendown() # turtle.write("GIVEN SIDES OF THE TRIANGLE ARE WRONG", font=("Arial", 16, "normal")) cuadruploActual = cuadruplos[currentPointer] currentPointer += 1 turtle.getscreen()._root.mainloop()
@ar.production('G -> int_const') def __g1(g, int_const): g.val = int(int_const.getContent()) @ar.production('G -> id') def __g2(g, id_): global d g.val = d[id_.getContent()] @ar.production('Assignment -> id = E') def __assign(assi, id_, _, E): d[id_.getContent()] = E.val assi.val = d[id_.getContent()] action = Action.load(cfg, "simpleCalc/calc_action") goto = Goto.load(cfg, "simpleCalc/calc_goto") while True: try: inputString = input(">>> ") tokenList = scanner.parse(typedef, inputString) pt = Parser.parse(tokenList, typedef, cfg, action, goto) pt.evaluate(ar) except EOFError: break
from cfg import ContextFreeGrammar from typeDef import TypeDefinition from action import Action from goto import Goto from parseTree import ParseTreeActionRegister import scanner import Parser typedef = TypeDefinition.load("simpleSQL/sqltypedef") cfg = ContextFreeGrammar.load(typedef, "simpleSQL/SQL") action, goto = Parser.genActionGoto(typedef, cfg) tokenList = scanner.parse(typedef, input()) pt = Parser.parse(tokenList, typedef, cfg, action, goto) print(pt)