def parse(self): # Use one-token lookahead to determine whether it's <int>, <id>, or (<exp>) tokNo = t.tokenizer.get_token() if tokNo == t.Tokens.NUMBER.value: self.__int = Int.Int() self.__int.parse() self.__alternative = 1 elif tokNo == t.Tokens.IDENTIFIER.value: self.__id = Id.Id() self.__id.parse() self.__alternative = 2 elif tokNo == t.Tokens.OPEN_PAREN.value: t.tokenizer.skip_token() # Consume open paren self.__exp = Exp.Exp() self.__exp.parse() self.__alternative = 3 tokNo = t.tokenizer.get_token() t.tokenizer.skip_token() # Consume closed parent if tokNo != t.Tokens.CLOSED_PAREN.value: print("If: Expected token {}, got token {}".format( t.Tokens.CLOSED_PAREN.value, tokNo)) return -1 # print("Loop: Consumed `)` token.") else: print("Op: Invalid Next Token {}!".format(tokNo)) exit(-1) return -1 # Successful error code return 0
def parse(self): # Id self.__id = Id.Id() self.__id.parse() # `=` token tokNo = t.tokenizer.get_token() t.tokenizer.skip_token() if tokNo != t.Tokens.EQUALS.value: print("Assign: Expected token {}, got token {}".format( t.Tokens.EQUALS.value, tokNo)) return -1 # print("Assign: Consumed `=` token.") # Exp self.__exp = Exp.Exp() self.__exp.parse() # `;` token tokNo = t.tokenizer.get_token() t.tokenizer.skip_token() if tokNo != t.Tokens.SEMICOLON.value: print("Assign: Expected token {}, got token {}".format( t.Tokens.SEMICOLON.value, tokNo)) return -1 # print("Assign: Consumed `;` token.") # Successful error code return 0
def parse(self): # Parse based on one token look ahead x = int(self.t.peek()) if x == 31: self.i = Int.Int(self.t) self.i.parse() elif x == 32: self.case = 1 self.idName = self.t.getValue() self.id = Id.Id(self.t) self.id.parse() elif x == 20: self.case = 2 self.t.getToken() self.e = Exp.Exp(self.t) x = int(self.t.getToken()) if x != 21: print "Expected end parenthesis." exit() else: print "Invalid op." exit()
def __init__(self, t): self.e = Exp.Exp(t) self.id = Id.Id(t) self.t = t
def exp(x): return Exp(float(x))