def create_scanner(self, fp): letter = plex.Range('azAZ') #ena gramma digit = plex.Range('09') #ena pshfio telesths3 = plex.Str('=') parenthesis = plex.Str('(', ')') name = letter + plex.Rep( letter | digit) #gramma kai perissotera pshfia h grammata boolFalse = plex.NoCase(plex.Str('false', 'f', '0')) boolTrue = plex.NoCase(plex.Str('true', 't', '1')) space = plex.Rep1(plex.Any(' \n\t')) keyword = plex.Str('print', 'not', 'or', 'and') simple_comment = plex.Str('//') + plex.Rep( plex.AnyBut('\n')) #sbhnei ta sxolia kati pou den einai new line lexicon = plex.Lexicon([ (keyword, plex.TEXT), (boolTrue, 'true'), (boolFalse, 'false'), (name, 'IDENTIFIER'), (space, plex.IGNORE), (telesths3, '='), (parenthesis, plex.TEXT), (simple_comment, plex.IGNORE) #kanei ignore ta comment ]) self.scanner = plex.Scanner(lexicon, fp) self.la, self.val = self.next_token()
def create_scanner(self, fp): logvalTrue = plex.NoCase(plex.Str('true', 't', '1')) logvalFalse = plex.NoCase(plex.Str('false', 'f', '0')) digit = plex.Range('09') letter = plex.Range('azAZ') identifier = letter + plex.Rep(letter | digit) keyword = plex.Str('print') Moperator = plex.Str('and', 'or') Soperator = plex.Str('not') equals = plex.Str("=") parenthesis = plex.Any("()") space = plex.Rep1(plex.Any(' \n\t')) lexicon = plex.Lexicon([(keyword, plex.TEXT), (Moperator, plex.TEXT), (Soperator, plex.TEXT), (logvalTrue, 'LOGVALTRUE'), (logvalFalse, 'LOGVALFALSE'), (identifier, 'IDENTIFIER'), (space, plex.IGNORE), (Moperator, plex.TEXT), (Soperator, plex.TEXT), (parenthesis, plex.TEXT), (equals, plex.TEXT)]) self.scanner = plex.Scanner(lexicon, fp) self.la, self.val = self.next_token()
def create_scanner(self,fp): """ Creates a plex scanner for a particular grammar to operate on file object fp. """ letter = plex.Range("azAZ") digit = plex.Range("09") ops = plex.Str("not", "and", "or") kprint = plex.Str("print") true = plex.NoCase(plex.Str("true", "t", "1")) false = plex.NoCase(plex.Str("false", "f", "0")) identifier = letter + plex.Rep(letter | digit) assign = plex.Str("=") space = plex.Rep1(plex.Any(" \n\t")) par = plex.Any("()") lexicon = plex.Lexicon([ (kprint, "PRINT"), (ops, plex.TEXT), (true, "TRUE"), (false, "FALSE"), (identifier, "ID"), (assign, "="), (par, plex.TEXT), (space, plex.IGNORE) ]) self.scanner = plex.Scanner(lexicon, fp) self.la, self.val = self.next_token()
def create_scanner(self, fp): """ Creates a plex scanner for a particular grammar to operate on file object fp. """ # define some pattern constructs letter = plex.Range("AZaz") digit = plex.Range("09") identifier = letter + plex.Rep(letter | digit) telestes = plex.Str('=') parenthesi = plex.Any('()') keyword = plex.Str('print') AndOrOp = plex.Str('and', 'or') NotOp = plex.Str('not') BoolTrue = plex.NoCase(plex.Str('true', 't', '1')) BoolFalse = plex.NoCase(plex.Str('false', 'f', '0')) space = plex.Any(" \t\n") # the scanner lexicon - constructor argument is a list of (pattern,action ) tuples lexicon = plex.Lexicon([(keyword, plex.TEXT), (space, plex.IGNORE), (NotOp, plex.TEXT), (AndOrOp, plex.TEXT), (BoolTrue, 'TRUE'), (BoolFalse, 'FALSE'), (parenthesi, plex.TEXT), (identifier, 'IDENTIFIER'), (telestes, plex.TEXT)]) # create and store the scanner object self.scanner = plex.Scanner(lexicon, fp) # get initial lookahead self.la, self.val = self.next_token()
def create_scanner(self, fp): letter = plex.Range("azAZ") digit = plex.Range("09") name = letter + plex.Rep(letter | digit) space = plex.Rep1(plex.Any(" \n\t")) keyword = plex.Str("print") operator_and_or = plex.Str("and", "or") operator_not = plex.Str("not") operator_equal = plex.Str("=") operator_parenth = plex.Str("(", ")") operator_false = plex.NoCase(plex.Str( '0', 'f', 'false')) #den exei shmasia an einai kefalaia h mikra operator_true = plex.NoCase(plex.Str( '1', 't', 'true')) # den exei shmasia an einai kefalaia h mikra lexicon = plex.Lexicon([(keyword, plex.TEXT), (operator_and_or, plex.TEXT), (operator_not, plex.TEXT), (operator_false, "FALSE"), (operator_true, "TRUE"), (name, "IDENTIFIER"), (space, plex.IGNORE), (operator_parenth, plex.TEXT), (operator_equal, plex.TEXT)]) self.scanner = plex.Scanner(lexicon, fp) self.la, self.val = self.next_token()
def create_scanner(self, fp): """ Creates a plex scanner for a particular grammar to operate on file object fp. """ # define some pattern constructs notop = plex.Str("not") andop = plex.Str("and") orop = plex.Str("or") true = plex.NoCase(plex.Str("true", "t", "1")) false = plex.NoCase(plex.Str("false", "f", "0")) equals = plex.Str("=") letter = plex.Range("AZaz") digit = plex.Range("09") variable = letter + plex.Rep(letter | digit) parenthesis = plex.Str("(", ")") keyword = plex.Str("print") space = plex.Any(" \t\n") # the scanner lexicon - constructor argument is a list of (pattern,action ) tuples lexicon = plex.Lexicon([(notop, plex.TEXT), (andop, plex.TEXT), (orop, plex.TEXT), (true, 'TRUE'), (false, 'FALSE'), (equals, plex.TEXT), (parenthesis, plex.TEXT), (space, plex.IGNORE), (keyword, plex.TEXT), (variable, 'VAR')]) # create and store the scanner object self.scanner = plex.Scanner(lexicon, fp) # get initial lookahead self.la, self.val = self.next_token()
def create_scanner(self,fp): letter = plex.Range('azAZ') digit = plex.Range('09') identifier = letter + plex.Rep(letter|digit) keyword = plex.Str('Print') AndOrOp = plex.Str('and','or') NotOp = plex.Str('not') equals = plex.Str('=') parenthesis = plex.Any('()') space = plex.Rep1(plex.Any(' \n\t')) booleanFalse = plex.NoCase(plex.Str('false','f','0')) booleanTrue = plex.NoCase(plex.Str('true','t','1'))
def create_scanner(self,fp): """ Creates a plex scanner for a particular grammar to operate on file object fp. """ # define some pattern constructs letter = plex.Range("AZaz") digit = plex.Range("09") keywords = plex.Str("print", "not", "and", "or") true_keyword = plex.NoCase(plex.Str("true", "t", "1")) false_keyword = plex.NoCase(plex.Str("false", "f", "0")) identifier = letter + plex.Rep(letter | digit) assign = plex.Str("=") parenthesis = plex.Str("(", ")") space = plex.Rep1(plex.Any(" \n\t"))
def create_scanner(self, text): """ Creates a plex scanner for a particular grammar """ # define some pattern constructs letter = plex.Range("AZaz") digit = plex.Range("09") boolean = plex.NoCase(plex.Str("true", "false", "t", "f", "0", "1")) andoroperators = plex.Str("and", "or") notoperator = plex.Str("not") equal = plex.Str("=") v = letter + plex.Rep(letter | digit) parenthesis = plex.Str("(", ")") output = plex.Str("print") spaces = plex.Any(" \t\n") # the scanner lexicon - constructor argument is a list of (pattern,action ) tuples lexicon = plex.Lexicon([ (andoroperators, 'AND/OR'), (notoperator, 'NOT'), (boolean, 'BOOLEAN'), (equal, '='), (parenthesis, plex.TEXT), (output, 'PRINT'), (spaces, plex.IGNORE), (v, 'VARIABLE' ) #v for variable to match grammar rules given in report ]) # create and store the scanner object self.scanner = plex.Scanner(lexicon, text) # get initial lookahead self.la, self.val = self.next_token()
def create_scanner(self, fp): """ Creates a plex scanner for a particular grammar to operate on file object fp. """ # define some pattern constructs letter = plex.Range("AZaz") digit = plex.Range("09") binary = plex.Rep1(plex.Any("01")) id = letter + plex.Rep(letter | digit) operator = plex.Str("and", "or", "xor", "=") paren = plex.Any("()") space = plex.Any(" \t\n") comment = plex.Str("{") + plex.Rep(plex.AnyBut("}")) + plex.Str("}") keyword = plex.NoCase(plex.Str("print")) # the scanner lexicon - constructor argument is a list of (pattern,action ) tuples lexicon = plex.Lexicon([(binary, "binary"), (operator, plex.TEXT), (keyword, plex.TEXT), (paren, plex.TEXT), (space | comment, plex.IGNORE), (id, 'id')]) # create and store the scanner object self.scanner = plex.Scanner(lexicon, fp) # get initial lookahead self.la, self.val = self.next_token()
def create_scanner(self, fp): """ Creates a plex scanner for a particular grammar to operate on file object fp. """ self.run_values = [] self.vars = {} # define some pattern constructs letter = plex.Range('azAZ') digit = plex.Range('09') id = letter + plex.Rep(letter | digit) bool_values = plex.NoCase(plex.Str('true', 'false', 't', 'f', '0', '1')) operator = plex.Str('and', 'or', 'not', '(', ')', '=') space = plex.Any(' \t\n') print_keyword = plex.Str('print') # the scanner lexicon - constructor argument is a list of (pattern,action ) tuples lexicon = plex.Lexicon([ (print_keyword, plex.TEXT), (operator, plex.TEXT), (bool_values, 'BOOL'), (id, 'IDENTIFIER'), (space, plex.IGNORE), ]) # create and store the scanner object self.scanner = plex.Scanner(lexicon, fp) # get initial lookahead self.la, self.val = self.next_token()
def __init__(self): letter = plex.Range('azAz') digit = plex.Range('09') ID = letter + plex.Rep(letter | digit) BINARY = plex.Rep1(plex.Range('01')) AND = plex.Str('and') OR = plex.Str('or') XOR = plex.Str('xor') ASSIGN = plex.Str('=') LEFTPAR = plex.Str('(') RIGHTPAR = plex.Str(')') PRINT = plex.NoCase(plex.Str('PRINT')) self.lexicon = plex.Lexicon([(AND, 'AND'), (OR, 'OR'), (XOR, 'XOR'), (ASSIGN, 'ASSIGN'), (LEFTPAR, 'LEFTPAR'), (RIGHTPAR, 'RIGHTPAR'), (PRINT, 'PRINT'), (BINARY, 'BINARY'), (ID, 'ID'), (plex.Rep1(plex.Any(" \t\n")), plex.IGNORE)]) self.stack = {}
def create_scanner(self, fp): letter = plex.Range("azAZ") digit = plex.Range("09") keywords = plex.Str("print", "not", "and", "or") true_keyword = plex.NoCase(plex.Str("true", "t", "1")) false_keyword = plex.NoCase(plex.Str("false", "f", "0")) identifier = letter + plex.Rep(letter | digit) assign = plex.Str("=") parenthesis = plex.Str("(", ")") space = plex.Rep1(plex.Any(" \n\t")) lexicon = plex.Lexicon([(keywords, plex.TEXT), (true_keyword, "True"), (false_keyword, "False"), (identifier, "IDENTIFIER"), (assign, "="), (parenthesis, plex.TEXT), (space, plex.IGNORE)]) self.scanner = plex.Scanner(lexicon, fp) self.la, self.val = self.next_token()
def create_scanner(self, fp): letter = plex.Range('azAZ') digit = plex.Range('09') ID = letter + plex.Rep(letter | digit) keyword = plex.Str('print') OP1 = plex.Str('and', 'or') OP2 = plex.Str('not') equals = plex.Str('=') parenthesis = plex.Any('()') space = plex.Rep1(plex.Any(' \n\t')) tfFalse = plex.NoCase(plex.Str('false', 'f', '0')) tfTrue = plex.NoCase(plex.Str('true', 't', '1')) lexicon = plex.Lexicon([(keyword, plex.TEXT), (OP1, plex.TEXT), (OP2, plex.TEXT), (tfTrue, 'TRUE'), (tfFalse, 'FALSE'), (ID, 'ID'), (space, plex.IGNORE), (parenthesis, plex.TEXT), (equals, plex.TEXT)]) self.scanner = plex.Scanner(lexicon, fp) self.la, self.val = self.next_token() #look ahead
def create_scanner(self, fp): keyword = plex.Str("print") assignment = plex.Str("=") parenthesis = plex.Str("(",")") Logical_Operator = plex.Str("not", "and", "or") letter = plex.Range("azAZ") digit = plex.Str("0","1") condition = letter + plex.Rep(letter | digit) true = plex.NoCase(plex.Str("true", "t", "1")) false = plex.NoCase(plex.Str("false", "f", "0")) space_or_new_line = plex.Any(' \n\t') lexicon = plex.Lexicon([ (keyword, plex.TEXT), (Logical_Operator, plex.TEXT), (assignment, plex.TEXT), (parenthesis,plex.TEXT), (true, "True"), (false, "False"), (condition, "Condition"), (space_or_new_line, plex.IGNORE) ]) self.scanner = plex.Scanner(lexicon, fp) self.next_value, self.value = self.next_token()
def create_scanner(self, fp): letter = plex.Range('azAZ') digit = plex.Range('09') telestis = plex.Str('=') parenthesi = plex.Str('(', ')') int_num = plex.Rep1(digit) name = letter + plex.Rep(letter | digit) space = plex.Rep1(plex.Any(' \n\t')) keyword = plex.Str('print') logic = plex.NoCase(plex.Str('and', 'or')) logicNot = plex.NoCase(plex.Str('not')) booleanT = plex.NoCase(plex.Str('true', 't', '1')) booleanF = plex.NoCase(plex.Str('false', 'f', '0')) lexicon = plex.Lexicon([(keyword, plex.TEXT), (name, 'Identifier'), (space, plex.IGNORE), (telestis, '='), (parenthesi, plex.TEXT), (int_num, 'INTEGER'), (logicNot, 'not'), (booleanT, 'BooleanT'), (booleanF, 'BooleanF'), (logic, plex.TEXT)]) self.scanner = plex.Scanner(lexicon, fp) self.la, self.val = self.next_token()
def create_scanner(self, fp): space = plex.Rep1(plex.Any(' \n\t')) digit = plex.Range('09') true = plex.NoCase(plex.Str('TRUE', 't', '1')) false = plex.NoCase(plex.Str('FALSE', 'f', '0')) andop = plex.Str("and") orop = plex.Str("or") notop = plex.Str("not") opereitor = plex.Any('=()') keyword = plex.Str('print') letter = plex.Range('azAZ') name = letter + plex.Rep(letter | digit) single_coment = plex.Str('//') + plex.Rep(plex.AnyBut('\n')) lexicon = plex.Lexicon([(keyword, plex.TEXT), (orop, 'OR'), (andop, 'AND'), (notop, 'NOT'), (true, 'TRUE'), (false, 'FALSE'), (name, 'IDENTIFIR'), (space, plex.IGNORE), (opereitor, plex.TEXT), (single_coment, plex.IGNORE)]) # create and store the scanner object self.scanner = plex.Scanner(lexicon, fp) # get initial lookahead self.la, self.val = self.next_token()
def create_scanner(self,fp): """ Creates a plex scanner for a particular grammar to operate on file object fp. """ # define some pattern constructs letter = plex.Range("AZaz") digit = plex.Range("09") string = plex.Rep1(letter | digit) logOp = plex.Str("and", "or", "not") assignOp = plex.Str("=") booleanVal = plex.NoCase(plex.Str("true","false","t","f","0","1")) printCommand = plex.Str("print") leftpar = plex.Str("(") rightpar = plex.Str(")") space = plex.Any(" \t\n") # the scanner lexicon - constructor argument is a list of (pattern,action ) tuples lexicon = plex.Lexicon([ (logOp,'LOG_OP_TOKEN'), (booleanVal,'BOOLEAN_VAL_TOKEN'), (space,plex.IGNORE), (assignOp, 'ASSIGN_OP'), (printCommand, 'PRINT_COMMAND'), (string, 'string'), (leftpar, '('), (rightpar, ')') ]) # create and store the scanner object self.scanner = plex.Scanner(lexicon,fp) # get initial lookahead # la is the type of token # val is the value of the token self.la,self.val = self.next_token()
class MyParser(): def __init__(self): self.st = {} def create_scanner (self, fp): letter = plex.Range("azAZ") digit = plex.Range("09") name = letter + plex.Rep(letter | digit) space = plex.Rep1(plex.Any(" \n\t")) keyword = plex.Str("print") operator_and_or=plex.Str("and","or") operator_not=plex.Str("not") operator_equal=plex.Str("=") operator_parenth=plex.Str("(",")") operator_false=plex.NoCase(plex.Str('0','f','false'))#den exei shmasia an einai kefalaia h mikra operator_true = plex.NoCase(plex.Str('1', 't', 'true')) # den exei shmasia an einai kefalaia h mikra lexicon = plex.Lexicon([ (keyword, plex.TEXT), (operator_and_or,plex.TEXT), (operator_not,plex.TEXT), (operator_false,"FALSE"), (operator_true,"TRUE"), (name, "IDENTIFIER"), (space, plex.IGNORE), (operator_parenth,plex.TEXT), (operator_equal,plex.TEXT) ]) self.scanner = plex.Scanner(lexicon, fp) self.la, self.val = self.next_token() def parse(self, fp): self.create_scanner(fp) self.stmt_list() def next_token(self): return self.scanner.read() def parse(self,fp): self.create_scanner(fp) self.stmt_list() def stmt_list(self): if self.la == "IDENTIFIER" or self.la=="print": self.stmt() self.stmt_list() elif self.la is None: raise ParseError("Expecting", self.la) def stmt(self): if self.la == "IDENTIFIER": self.match("IDENTIFIER") self.match("=") self.expr() elif self.la == "print": self.match("print") self.expr() else: raise ParseError("Invalid command(Id or print command expected)") def expr(self): if self.la=="(" or self.la=="IDENTIFIER" or self.la=="FALSE" or self.la=="TRUE": self.term() self.term_tail() else: raise ParseError("Expected",self.la) def term_tail(self): if self.la == "and" or self.la == "or": self.operator_and_or() self.term() self.term_tail() elif self.la == "IDENTIFIER" or self.la == "print" or self.la == ")" or self.la == None: return else: raise ParseError("Expected keyword AND or OR") def term(self): if self.la=="(" or self.la=="IDENTIFIER" or self.la=="FALSE" or self.la=="TRUE" or self.la=="not": self.factor() self.factor_tail() else: raise ParseError("Expected id ,boolean operator or 'not' operator") def factor_tail(self): if self.la=="not": self.operator_not() self.factor() self.factor_tail() if ft is None: return op,ft if ft[0]=="*": return op,f*ft[1] elif self.la=="and" or self.la=="or" or self.la=="IDENTIFIER" or self.la=="print" or self.la==None or self.la== ")": return else: raise ParseError("Expected 'not' operator") def factor(self): if self.la == "(": self.match("(") self.expr() self.match(")") elif self.la == "IDENTIFIER": self.match("IDENTIFIER") elif self.la == "TRUE": self.match("TRUE") elif self.la == "FALSE": self.match("FALSE") elif self.la == "and" or self.la == "or" or self.la == "not" or self.la == "print" or self.la == None or self.la == ")": return else: raise ParseError("Expected id or boolean operator", self.la) def match(self, token): if self.la == token: self.la, self.val = self.next_token() else: raise ParseError("Expected ", self.la) def operator_and_or(self): if self.la== "and": self.match('and') elif self.la=="or": self.match('or') else: raise ParseError("Expected AND or OR") def operator_not(self): if self.la == "not" self.match('not') else: raise ParseError("Expected NOT operator")
class MyParser: def __init__(self): self.st = {} def create_scanner(self,fp): letter = plex.Range('azAZ') digit = plex.Range('09') identifier = letter + plex.Rep(letter|digit) keyword = plex.Str('print') NotOp = plex.Str('not') AndOrOp = plex.Str('and','or') equals = plex.Str('=') parenthesis = plex.Any('()') space = plex.Rep1(plex.Any(' \n\t')) booleanFalse = plex.NoCase(plex.Str('false','f','0')) booleanTrue = plex.NoCase(plex.Str('true','t','1')) lexicon = plex.Lexicon([ (keyword,plex.TEXT), (NotOp,plex.TEXT), (AndOrOp,plex.TEXT), (booleanFalse,'FALSE'), (booleanTrue,'TRUE'), (identifier,'IDENTIFIER'), (space,plex.IGNORE), (parenthesis,plex.TEXT), (equals,plex.TEXT) ])