class Parser(object): """ generated source for class Parser """ def __init__(self): self.scanner = None def match(self, s): """ generated source for method match """ self.scanner.match(Token(s)) def curr(self): """ generated source for method curr """ return self.scanner.curr() def pos(self): """ generated source for method pos """ return self.scanner.position() def parse(self, program): """ generated source for method parse """ if program == '': return None self.scanner = Scanner(program) self.scanner.next() return self.parseBlock() def parseAssn(self): id = self.curr().lex() # Gets current lexeme self.match('id') # Checks if current lexeme is id self.match('=') # Checks if current lexeme is = num = self.curr().lex() # Saves current number self.match('num') # Checks if current lexeme is num return AssnNode(id, num) def parseStmt(self): assn = self.parseAssn() # Parses an assignment return NodeStmt(assn) def parseBlock(self): stmt = self.parseStmt() # Parses a statement block_two = None # Optional second block curr = self.curr() # Gets current token if curr.lexeme == ";": self.scanner.next() # Advances to the next token so we don't parse the semicolon block_two = self.parseBlock() # Parses the next block return NodeBlock(stmt, block_two)
class Parser(object): """ generated source for class Parser """ def __init__(self): self.scanner = None def match(self, s): """ generated source for method match """ self.scanner.match(Token(s)) def curr(self): """ generated source for method curr """ return self.scanner.curr() def pos(self): """ generated source for method pos """ return self.scanner.position() def parseMulop(self): """ generated source for method parseMulop """ if self.curr() == Token("*"): self.match("*") return NodeMulop(self.pos(), "*") if self.curr() == Token("/"): self.match("/") return NodeMulop(self.pos(), "/") return None def parseRelop(self): """ generated source for method parseRelop """ if self.curr() == Token("<"): self.match("<") return NodeRelop(self.pos(), "<") if self.curr() == Token("<="): self.match("<=") return NodeRelop(self.pos(), "<=") if self.curr() == Token(">"): self.match(">") return NodeRelop(self.pos(), ">") if self.curr() == Token(">="): self.match(">=") return NodeRelop(self.pos(), ">=") if self.curr() == Token("<>"): self.match("<>") return NodeRelop(self.pos(), "<>") if self.curr() == Token("=="): self.match("==") return NodeRelop(self.pos(), "==") return None def parseAddop(self): """ generated source for method parseAddop """ if self.curr() == Token("+"): self.match("+") return NodeAddop(self.pos(), "+") if self.curr() == Token("-"): self.match("-") return NodeAddop(self.pos(), "-") return None def parseBoolExpr(self): """ generated source for method parseBoolExpr """ exprl = self.parseExpr() relop = self.parseRelop() exprr = self.parseExpr() boolexpr = NodeBoolExpr(exprl, relop, exprr) return boolexpr def parseFact(self): """ generated source for method parseFact """ if self.curr() == Token("("): self.match("(") expr = self.parseExpr() self.match(")") return NodeFactExpr(expr) if self.curr() == Token("-"): self.match("-") fact = self.parseFact() return NodeFactFact(fact) if self.curr() == Token("id"): nid = self.curr() self.match("id") if(self.curr() == Token("(")): self.match("(") expr = self.parseExpr() self.match(")") return NodeCall(self.pos(), nid.lex(), expr) return NodeFactId(self.pos(), nid.lex()) num = self.curr() self.match("num") return NodeFactNum(num.lex()) def parseTerm(self): """ generated source for method parseTerm """ fact = self.parseFact() mulop = self.parseMulop() if mulop == None: return NodeTerm(fact, None, None) term = self.parseTerm() term.append(NodeTerm(fact, mulop, None)) return term def parseExpr(self): """ generated source for method parseExpr """ term = self.parseTerm() addop = self.parseAddop() if addop == None: return NodeExpr(term, None, None) expr = self.parseExpr() expr.append(NodeExpr(term, addop, None)) return expr def parseAssn(self): """ generated source for method parseAssn """ nid = self.curr() self.match("id") self.match("=") expr = self.parseExpr() assn = NodeAssn(nid.lex(), expr) return assn def parseRd(self): """ generated source for method parseRd """ self.match("rd") nid = self.curr() self.match("id") rd = NodeRd(nid.lex()) return rd def parseWr(self): """ generated source for method parseWr """ self.match("wr") expr = self.parseExpr() wr = NodeWr(expr) return wr def parseIfElse(self): """ generated source for method parseIfElse """ self.match("if") boolexpr = self.parseBoolExpr() self.match("then") thenstmt = self.parseStmt() elsestmt = None if self.curr() == Token("else"): self.match("else") elsestmt = self.parseStmt() ifelse = NodeIfElse(boolexpr, thenstmt, elsestmt) return ifelse def parseWhileDo(self): """ generated source for method parseWhileDo """ self.match("while") boolexpr = self.parseBoolExpr() self.match("do") stmt = self.parseStmt() whiledo = NodeWhileDo(boolexpr, stmt) return whiledo def parseBegin(self): """ generated source for method parseBegin """ self.match("begin") block = self.parseBlock() self.match("end") begin = NodeBegin(block) return begin def parseFunc(self): self.match("def") funcID = self.curr().lex() self.match("id") self.match("(") funcParam = self.curr().lex() self.match("id") self.match(")") self.match("=") expr = self.parseExpr() return NodeFunc(funcID, funcParam, expr) def parseStmt(self): """ generated source for method parseStmt """ if self.curr() == Token("id"): assn = self.parseAssn() return NodeStmt(assn) if self.curr() == Token("rd"): rd = self.parseRd() return NodeStmt(rd) if self.curr() == Token("wr"): wr = self.parseWr() return NodeStmt(wr) if self.curr() == Token("if"): ifelse = self.parseIfElse() return NodeStmt(ifelse) if self.curr() == Token("while"): whiledo = self.parseWhileDo() return NodeStmt(whiledo) if self.curr() == Token("def"): func = self.parseFunc() return func begin = self.parseBegin() return NodeStmt(begin) def parseBlock(self): """ generated source for method parseBlock """ stmt = self.parseStmt() rest = None if self.curr() == Token(";"): self.match(";") rest = self.parseBlock() block = NodeBlock(stmt, rest) return block def parseProg(self): """ generated source for method parseProg """ block = self.parseBlock() if not self.scanner.done(): raise SyntaxException(self.pos(), Token("EOF"), self.curr()) prog = NodeProg(block) return prog def parse(self, program): """ generated source for method parse """ self.scanner = Scanner(program) self.scanner.next() return self.parseProg()
def parse(self, program): """ generated source for method parse """ self.scanner = Scanner(program) self.scanner.next() return self.parseProg()
class Parser(object): """ generated source for class Parser """ builtins = ['cos', 'sin', 'tan', 'abs', 'log', 'ln'] def __init__(self): self.scanner = None def match(self, s): """ generated source for method match """ self.scanner.match(Token(s)) def curr(self): """ generated source for method curr """ return self.scanner.curr() def pos(self): """ generated source for method pos """ return self.scanner.position() def parseMulop(self): """ generated source for method parseMulop """ if self.curr() == Token("*"): self.match("*") return NodeMulop(self.pos(), "*") if self.curr() == Token("/"): self.match("/") return NodeMulop(self.pos(), "/") return None def parseAddop(self): """ generated source for method parseAddop """ if self.curr() == Token("+"): self.match("+") return NodeAddop(self.pos(), "+") if self.curr() == Token("-"): self.match("-") return NodeAddop(self.pos(), "-") return None def parseRelop(self): if self.curr() == Token("<"): self.match("<") return NodeRelop(self.pos(), "<") if self.curr() == Token("<="): self.match("<=") return NodeRelop(self.pos(), "<=") if self.curr() == Token(">"): self.match(">") return NodeRelop(self.pos(), ">") if self.curr() == Token(">="): self.match(">=") return NodeRelop(self.pos(), ">=") if self.curr() == Token("<>"): self.match("<>") return NodeRelop(self.pos(), "<>") if self.curr() == Token("=="): self.match("==") return NodeRelop(self.pos(), "==") return None def parseFact(self): """ generated source for method parseFact """ if self.curr() == Token("("): self.match("(") expr = self.parseExpr() self.match(")") return NodeFactExpr(expr) if self.curr() == Token("-"): self.match("-") fact = self.parseFact() return NodeFactFact(fact) if self.curr() == Token("id"): id = self.curr() self.match("id") if self.curr() == Token("("): self.match("(") expr = self.parseExpr() self.match(")") if id.lex() == "cos": return NodeCos(expr) if id.lex() == "sin": return NodeSin(expr) if id.lex() == "tan": return NodeTan(expr) if id.lex() == "abs": return NodeAbs(expr) if id.lex() == "log": return NodeLog(expr) if id.lex() == "ln": return NodeLn(expr) return NodeFuncCall(id.lex(), expr) return NodeFactId(self.pos(), id.lex()) num = self.curr() self.match("num") return NodeFactNum(num.lex()) def parseTerm(self): """ generated source for method parseTerm """ fact = self.parseFact() mulop = self.parseMulop() if mulop is None: return NodeTerm(fact, None, None) term = self.parseTerm() term.append(NodeTerm(fact, mulop, None)) return term def parseExpr(self): """ generated source for method parseExpr """ term = self.parseTerm() addop = self.parseAddop() if addop is None: return NodeExpr(term, None, None) expr = self.parseExpr() expr.append(NodeExpr(term, addop, None)) return expr def parseBoolExpr(self): expr_1 = self.parseExpr() relop = self.parseRelop() expr_2 = self.parseExpr() bool_expr = NodeBoolExpr(expr_1, relop, expr_2) return bool_expr def parseAssn(self): """ generated source for method parseAssn """ id = self.curr() self.match("id") self.match("=") expr = self.parseExpr() assn = NodeAssn(id.lex(), expr) return assn def parseWr(self): """ generated source for method parseWr """ self.match("wr") expr = self.parseExpr() wr = NodeWr(expr) return wr def parseRd(self): self.match("rd") id = self.curr() self.match("id") num = input() rd = NodeRd(id.lex(), float(num)) return rd def parseIf(self): self.match("if") bool_expr = self.parseBoolExpr() self.match("then") stmt = self.parseStmt() sub_stmt = None if self.curr() == Token("else"): self.match("else") sub_stmt = self.parseStmt() if_stmt = NodeIf(bool_expr, stmt, sub_stmt) return if_stmt def parseWhile(self): self.match("while") bool_expr = self.parseBoolExpr() self.match("do") stmt = self.parseStmt() while_stmt = NodeWhile(bool_expr, stmt) return while_stmt def parseBegin(self): self.match("begin") block = self.parseBlock() self.match("end") begin = NodeBegin(block) return begin def parseFuncDecl(self): self.match("def") id = self.curr() if id.lex() in self.builtins: raise SyntaxException(self.pos(), "func header", id) self.match("id") self.match("(") param_id = self.curr() self.match("id") self.match(")") self.match("=") expr = self.parseExpr() func_decl = NodeFuncDecl(id.lex(), param_id.lex(), expr) return func_decl def parseStmt(self): """ generated source for method parseStmt """ if self.curr() == Token("rd"): rd = self.parseRd() return NodeStmt(rd) if self.curr() == Token("wr"): wr = self.parseWr() return NodeStmt(wr) if self.curr() == Token("if"): if_stmt = self.parseIf() return NodeStmt(if_stmt) if self.curr() == Token("while"): while_stmt = self.parseWhile() return NodeStmt(while_stmt) if self.curr() == Token("begin"): begin = self.parseBegin() return NodeStmt(begin) if self.curr() == Token("def"): func_decl = self.parseFuncDecl() return NodeStmt(func_decl) if self.curr() == Token("id"): assn = self.parseAssn() return NodeStmt(assn) return None def parseBlock(self): """ generated source for method parseBlock """ stmt = self.parseStmt() rest = None if self.curr() == Token(";"): self.match(";") rest = self.parseBlock() block = NodeBlock(stmt, rest) return block def parseProg(self): block = self.parseBlock() return NodeProg(block) def parse(self, program): """ generated source for method parse """ self.scanner = Scanner(program) self.scanner.next() return self.parseProg()
class Parser(object): """ generated source for class Parser """ def __init__(self): self.scanner = None def match(self, s): """ generated source for method match """ self.scanner.match(Token(s)) def curr(self): """ generated source for method curr """ return self.scanner.curr() def pos(self): """ generated source for method pos """ return self.scanner.position() #TODO def parseAssn(self): """ generated source for method parseAssn """ nid = self.curr() self.match("id") self.match("=") num = self.curr() self.match("num") assn = NodeAssn(nid.lex(), num) return assn #TODO def parseStmt(self): """ generated source for method parseStmt """ if self.curr() == Token("id"): assn = self.parseAssn() return NodeStmt(assn) return None def parseBlock(self): """ generated source for method parseBlock """ stmt = self.parseStmt() rest = None if self.curr() == Token(";"): self.match(";") rest = self.parseBlock() block = NodeBlock(stmt, rest) return block #TODO def parseTerm(self): return def parseFact(self): return def parseAddOp(self): return def parseMulOp(self): return def parse(self, program): """ generated source for method parse """ if program == '': return None self.scanner = Scanner(program) self.scanner.next() return self.parseBlock()
def parse(self, program): """ generated source for method parse """ if program == '': return None self.scanner = Scanner(program) self.scanner.next() return self.parseBlock()
class Parser(object): """ generated source for class Parser """ def __init__(self): self.scanner = None self.env = Environment() def match(self, s): """ generated source for method match """ self.scanner.match(Token(s)) def curr(self): """ generated source for method curr """ return self.scanner.curr() def pos(self): """ generated source for method pos """ return self.scanner.position() def parse(self, program): """ generated source for method parse """ if program == '': return None self.scanner = Scanner(program) self.scanner.next() return self.parseProg() def parseAssn(self): id = self.curr().lex() # Gets current lexeme self.match('id') # Checks if current lexeme is id self.match('=') # Checks if current lexeme is = expr = self.parseExpr() return NodeAssn(id, expr) def parseFact(self): #If a fact is an id then match the token as an id and return the node fact with an id if (self.curr().tok() == "id"): id = self.curr().lex() self.match('id') return NodeFact(id, None, None, None) # If we have a ( then we have an expression elif ("(" in self.curr().lex()): self.match('(') expr = self.parseExpr() self.match(')') return NodeFact(None, None, expr, None) # If not a ( or expr then expect a num elif (self.curr().tok() == "num"): num = self.curr().lex() self.match('num') return NodeFact(None, num, None, None) elif (self.curr().tok() == "-"): self.match("-") fact = self.parseFact() return NodeFact(None, None, None, fact) # returns appropriate Addop Node def parseAddOp(self): addop = self.curr().tok() if (addop == "+"): self.match("+") return NodeAddOp(addop) elif (addop == "-"): self.match("-") return NodeAddOp(addop) else: return NodeAddOp(None) # Returns appropriate mulop node def parseMulOp(self): mulop = self.curr().lex() if (mulop == "*"): self.match("*") return NodeMulOp(mulop) elif (mulop == "/"): self.match("/") return NodeMulOp(mulop) else: return NodeMulOp(None) def parseTerm(self): # Parses fact fact = self.parseFact() # Parses mulop mulop = self.parseMulOp() # If we don't have a mulop then return term node with just fact if mulop.mulop is None: return NodeTerm(fact, None, None) # If we have a mulop then parse a term and append the fact and mulop onto the term node term = self.parseTerm() term.append(NodeTerm(fact, mulop, None)) return term def parseExpr(self): # Parses term term = self.parseTerm() # parses addop addop = self.parseAddOp() # If we don't have an addop then return a node with just a term if addop.addop is None: return NodeExpr(term, None, None) # If we do have addop then parse an expression and add a term to the node expr = self.parseExpr() expr.append(NodeExpr(term, addop, None)) return expr def parseWr(self): # Parses an expression and returns a wr node with an expression inside expr = self.parseExpr() return NodeWr(expr) def parseStmt(self): # Checks if the current token is a wr keyword if so parse a wr node if self.curr().tok() == "wr": self.match("wr") wr = self.parseWr() return NodeStmt(None, None, wr, None, None, None) # If not a wr then must be assn node elif self.curr().tok() == "rd": self.match("rd") rd = self.parseRd() return NodeStmt(None, rd, None, None, None, None) elif self.curr().tok() == "if": self.match("if") ifKey = self.parseIf() return NodeStmt(None, None, None, ifKey, None, None) elif (self.curr().tok() == "while"): self.match("while") whileKey = self.parseWhile() return NodeStmt(None, None, None, None, whileKey, None) elif (self.curr().tok() == "begin"): self.match("begin") begin = self.parseBegin() return NodeStmt(None, None, None, None, None, begin) else: assn = self.parseAssn() # Parses an assignment return NodeStmt(assn, None, None, None, None, None) def parseBlock(self): stmt = self.parseStmt() # Parses a statement block_two = None # Optional second block curr = self.curr() # Gets current token if curr.lexeme == ";": self.scanner.next( ) # Advances to the next token so we don't parse the semicolon block_two = self.parseBlock() # Parses the next block return NodeBlock(stmt, block_two) def parseProg(self): block = self.parseBlock() # Parses a block return NodeProg(block) def parseRd(self): rID = self.curr().lex() # Gets id to save user input self.match('id') return NodeRd(rID) def parseIf(self): boolExpr = self.parseBoolExpr() # Parses boolean expression self.match('then') stmt = self.parseStmt() # Parses statement stmt2 = None if (self.curr().tok() == "else"): # Checks for else self.match('else') stmt2 = self.parseStmt() return NodeIf(boolExpr, stmt, stmt2) def parseBoolExpr(self): expr = self.parseExpr() # Parse expression, relop, and expr2 relop = self.parseRelOp() expr2 = self.parseExpr() return NodeBoolExpr(expr, relop, expr2) # Checks for relop and returns node def parseRelOp(self): if (self.curr().tok() == "<"): self.match("<") return NodeRelOp("<") elif (self.curr().tok() == "<="): self.match("<=") return NodeRelOp("<=") elif (self.curr().tok() == ">"): self.match(">") return NodeRelOp(">") elif (self.curr().tok() == ">="): self.match(">=") return NodeRelOp(">=") elif (self.curr().tok() == "<>"): self.match("<>") return NodeRelOp("<>") else: self.match("==") return NodeRelOp("==") # Parses while statement def parseWhile(self): boolExpr = self.parseBoolExpr() self.match("do") stmt = self.parseStmt() return NodeWhile(boolExpr, stmt) # Parses begin end block def parseBegin(self): block = self.parseBlock() self.match("end") return NodeBegEnd(block)
class Parser(object): """ generated source for class Parser """ def __init__(self): self.scanner = None self.env = Environment() def match(self, s): """ generated source for method match """ self.scanner.match(Token(s)) def curr(self): """ generated source for method curr """ return self.scanner.curr() def pos(self): """ generated source for method pos """ return self.scanner.position() def parse(self, program): """ generated source for method parse """ if program == '': return None self.scanner = Scanner(program) self.scanner.next() return self.parseBlock() def parseAssn(self): id = self.curr().lex() # Gets current lexeme self.match('id') # Checks if current lexeme is id self.match('=') # Checks if current lexeme is = expr = self.parseExpr() return NodeAssn(id, expr) def parseFact(self): #If a fact is an id then match the token as an id and return the node fact with an id if(self.curr().tok() == "id"): id = self.curr().lex() self.match('id') return NodeFact(id, None, None) # If we have a ( then we have an expression elif("(" in self.curr().lex()): self.match('(') expr = self.parseExpr() self.match(')') return NodeFact(None, None, expr) # If not a ( or expr then expect a num else: num = self.curr().lex() self.match('num') return NodeFact(None, num, None) # returns appropriate Addop Node def parseAddOp(self): addop = self.curr().lex() if(addop == "+"): self.match("+") return NodeAddOp(addop) elif(addop == "-"): self.match("-") return NodeAddOp(addop) else: return NodeAddOp(None) # Returns appropriate mulop node def parseMulOp(self): mulop = self.curr().lex() if(mulop == "*"): self.match("*") return NodeMulOp(mulop) elif(mulop == "/"): self.match("/") return NodeMulOp(mulop) else: return NodeMulOp(None) def parseTerm(self): # Parses fact fact = self.parseFact() # Parses mulop mulop = self.parseMulOp() # If we don't have a mulop then return term node with just fact if mulop.mulop is None: return NodeTerm(fact, None, None) # If we have a mulop then parse a term and append the fact and mulop onto the term node term = self.parseTerm() term.append(NodeTerm(fact, mulop, None)) return term def parseExpr(self): # Parses term term = self.parseTerm() # parses addop addop = self.parseAddOp() # If we don't have an addop then return a node with just a term if addop.addop is None: return NodeExpr(term, None, None) # If we do have addop then parse an expression and add a term to the node expr = self.parseExpr() expr.append(NodeExpr(term, addop, None)) return expr def parseWr(self): # Parses an expression and returns a wr node with an expression inside expr = self.parseExpr() return NodeWr(expr) def parseStmt(self): # Checks if the current token is a wr keyword if so parse a wr node if self.curr().tok() == "wr": self.match("wr") wr = self.parseWr() return NodeStmt(None, wr) # If not a wr then must be assn node else: assn = self.parseAssn() # Parses an assignment return NodeStmt(assn, None) def parseBlock(self): stmt = self.parseStmt() # Parses a statement block_two = None # Optional second block curr = self.curr() # Gets current token if curr.lexeme == ";": self.scanner.next() # Advances to the next token so we don't parse the semicolon block_two = self.parseBlock() # Parses the next block return NodeBlock(stmt, block_two)