示例#1
0
	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") #letter
		digit = plex.Range("09") #digit

		identifier = letter+plex.Rep(letter|digit) #always starts with letter
		b_var = plex.Str('true','TRUE','True','t','T','false','FALSE','False','f','F','0','1') #t,T,f,F after full words for correct matching

		assign = plex.Str('=')
		parenthesis = plex.Str('(',')')

		keyword = plex.Str('print')
		operator = plex.Str('not','and','or')
		space=plex.Rep1(plex.Any(' \n\t'))

		# the scanner lexicon - constructor argument is a list of (pattern,action ) tuples
		lexicon = plex.Lexicon([
			(operator,plex.TEXT),
			(parenthesis,plex.TEXT),
			(assign,plex.TEXT),
			(space,plex.IGNORE),
			(keyword, plex.TEXT), #keywords and b_vars before identifiers for correct matching
			(b_var,'b_var'),
			(identifier,'id'),
			])
		
		# create and store the scanner object
		self.scanner = plex.Scanner(lexicon,fp)
		
		# get initial lookahead
		self.la,self.val = self.next_token()
示例#2
0
class MyParser:
	
	
	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"))

           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()
示例#3
0
    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")
        demicalDigit = plex.Range("09")
        binaryDigit = plex.Range('01')

        equals = plex.Str('=')
        parentheniL = plex.Str('(')
        parentheniR = plex.Str(')')
        binaryNumber = plex.Rep1(binaryDigit)
        name = letter + plex.Rep(letter | demicalDigit)
        space = plex.Any(" \t\n")
        print_key = plex.Str('print')
        andOperator = plex.Str('and')
        orOperator = plex.Str('or')
        xorOperator = plex.Str('xor')
        self.variableList = {}

        # the scanner lexicon - constructor argument is a list of (pattern,action ) tuples
        lexicon = plex.Lexicon([(space, plex.IGNORE), (parentheniL, plex.TEXT),
                                (parentheniR, plex.TEXT), (equals, plex.TEXT),
                                (print_key, 'print'), (binaryNumber, 'number'),
                                (andOperator, plex.TEXT),
                                (orOperator, plex.TEXT),
                                (xorOperator, plex.TEXT), (name, 'id')])

        # create and store the scanner object
        self.scanner = plex.Scanner(lexicon, fp)

        # get initial lookahead
        self.la, self.text = 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()
示例#5
0
    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()
示例#6
0
    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()
示例#7
0
    def create_scanner(self, fp):

        # define some pattern constructs
        decimal = plex.Range('09')
        binary = plex.Range('01')
        letter = plex.Range('azAZ')

        equals = plex.Str('=')
        lPar = plex.Str('(')
        rPar = plex.Str(')')
        space = plex.Any(' \n\t')
        binary = plex.Rep1(binaryDigit)
        name = letter + plex.Rep(letter | decimalDigit)
        printOp = plex.Str('print')
        andOp = plex.Str('and')
        orOp = plex.Str('or')
        xorOp = plex.Str('xor')

        # the scanner lexicon - constructor argument is a list of (pattern,action ) tuples
        self.lexicon = plex.Lexicon([(space, plex.IGNORE), (lPar, plex.TEXT),
                                     (rPar, plex.TEXT), (equals, plex.TEXT),
                                     (printOp, plex.TEXT),
                                     (binary, 'BINARY_NUM'),
                                     (andOp, plex.TEXT), (orOp, plex.TEXT),
                                     (xorOp, plex.TEXT), (name, 'id')])

        # create and store the scanner object
        self.scanner = plex.Scanner(self.lexicon, fp)

        # get initial lookahead
        self.la, self.val = self.next_token()
示例#8
0
    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()
示例#9
0
    def create_scanner(self, fp):
        """ Creates a plex scanner for a particular grammar 
        to operate on file object fp.
        """
        # Define some pattern constructs
        and_op = plex.Str('and')
        or_op = plex.Str('or')
        xor_op = plex.Str('xor')
        assignment_op = plex.Str('=')
        print_op = plex.Str('print')

        space = plex.Any(' \t\n')
        parenthesis_open = plex.Str('(')
        parenthesis_close = plex.Str(')')
        binary = plex.Rep1(plex.Range('01'))
        digit = plex.Range('09')
        letter = plex.Range('AZaz')
        variable = letter + plex.Rep(letter | digit)

        # The scanner lexicon - constructor argument is
        # a list of (pattern, action) tuples
        lexicon = plex.Lexicon([(and_op, 'and'), (or_op, 'or'),
                                (xor_op, 'xor'), (assignment_op, '='),
                                (print_op, 'print'), (space, plex.IGNORE),
                                (parenthesis_open, '('),
                                (parenthesis_close, ')'), (binary, 'bin'),
                                (variable, 'id')])

        # Create and store the scanner object, and get initial lookahead
        self.scanner = plex.Scanner(lexicon, fp)
        self.la, self.val = self.next_token()

        # Create a dictionary to store variables from file
        self.vars = {}
示例#10
0
    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()
示例#11
0
    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()
示例#12
0
	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()
示例#13
0
    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()
示例#14
0
    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")

        id = plex.Rep1(letter | digit)
        var = plex.Rep1(letter | digit)
        operator = plex.Any("!?()=#")
        space = plex.Any(" \t\n")

        # the scanner lexicon - constructor argument is a list of (pattern,action ) tuples
        lexicon = plex.Lexicon([(operator, plex.TEXT), (space, plex.IGNORE),
                                (Str("id"), plex.TEXT),
                                (Str("print"), plex.TEXT),
                                (Str("or"), plex.TEXT),
                                (Str("and"), plex.TEXT),
                                (Str("not"), plex.TEXT), (var, 'var')])

        # create and store the scanner object
        self.scanner = plex.Scanner(lexicon, fp)

        # get initial lookahead
        self.la, self.val = self.next_token()
示例#15
0
	    def create_scanner(self, fp):
	        
	       
	        and_operator = plex.Str('and')
	        or_operator = plex.Str('or')
	        xor_operator = plex.Str('xor')
	        assignment_operator = plex.Str('=')
	        print_operator = plex.Str('print')


	        space = plex.Any(' \t\n')
	        parenthesis_opened = plex.Str('(')
	        parenthesis_closed = plex.Str(')')
	        binary = plex.Rep1(plex.Range('01'))
	        digit = plex.Range('09')
	        letter = plex.Range('AZaz')        
	        variable = letter + plex.Rep(letter|digit)


	      
	        lexicon = plex.Lexicon([
	            (and_operator, 'and'),
	            (or_operator, 'or'),
	            (xor_operator, 'xor'),
	            (assignment_operator, '='),
	            (print_operator, 'print'),            
	            (space, plex.IGNORE),
	            (parenthesis_opened, '('),
	            (parenthesis_closed, ')'),
	            (binary, 'bin'),
	            (variable, 'var')            
	            ])


	       	        self.scanner = plex.Scanner(lexicon, fp)
示例#16
0
    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()
示例#17
0
    def create_scanner(self, fp):
        # Initializes scanner lexicon, patterns etc.
        self.command = ''
        boole = plex.Str('0', '1', 't', 'f', 'true', 'false', 'T', 'F', 'True',
                         'False', 'TRUE', 'FALSE')  # boolean value
        variable = plex.Rep1(plex.Range("AZaz")) + plex.Rep(
            plex.Range("AZaz09"))  # variable
        whitespace = plex.Any(" \t\n")  # ignore these

        lexicon = plex.Lexicon([  #lexicon
            (whitespace, plex.IGNORE), (boole, 'BOOLEAN'),
            (plex.Str('print'), 'PRINT'), (plex.Str("or"), 'OR'),
            (plex.Str("and"), 'AND'), (plex.Str("not"), 'NOT'),
            (plex.Str("("), '('), (plex.Str(")"), ')'), (plex.Str('='), '='),
            (plex.Str(''), 'None'), (variable, 'VAR')
        ])

        self.scanner = plex.Scanner(lexicon, fp)
        self.la, self.val = self.next_token()
示例#18
0
    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()
示例#19
0
    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
示例#20
0
	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()
示例#21
0
	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")
		
		bools1 = plex.Str("true","false")											# orismos boolean operator
		bools2 = plex.Str("t","f")													# orismos boolean operator
		bools3 = plex.Str("0", "1")													# orismos boolean operator
	
		AND = plex.Str("and")														# orismos logical operator
		OR = plex.Str("or")															# orismos logical operator
		NOT = plex.Str("not")														# orismos logical operator
		
		string = plex.Rep1(letter | digit)
		operator = plex.Any("!?()")		
		space = plex.Any(" \t\n")

		# the scanner lexicon - constructor argument is a list of (pattern,action ) tuples
		lexicon = plex.Lexicon([
			(bools1,plex.TEXT),
			(bools2,plex.TEXT),
			(bools3,plex.TEXT),
			(AND,plex.TEXT),
			(NOT,plex.TEXT),
			(OR,plex.TEXT),
			(operator,plex.TEXT),
			(space,plex.IGNORE),
			(string, 'string')
			])
		
		# create and store the scanner object
		self.scanner = plex.Scanner(lexicon,fp)
		
		# get initial lookahead
		self.la,self.val = self.next_token()
示例#22
0
    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()
示例#23
0
    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()
示例#24
0
    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()
示例#25
0
 def create_scanner(self, fp):
     self.SCANNER = plex.Scanner(self.LEXICON, fp)
     self.LA, self.TEXT = self.next_token()
示例#26
0
	def create_scanner(self,fp):
		
		# create and store the scanner object
		self.scanner = plex.Scanner(self.LEXICON, fp)
		# get initial lookahead
       		self.la, self.text = self.next_token()
示例#27
0
	def create_scanner(self,fp):
		self.scanner = plex.Scanner(self.lexicon,fp)
		self.la,self.text=self.next_token()
示例#28
0
        return MacOS.GetTicks() / 60.0

    timekind = "real"
else:

    def time():
        t = os.times()
        return t[0] + t[1]

    timekind = "cpu"

time1 = time()
lexicon = pascal.make_lexicon()
time2 = time()
print "Constructing scanner took %s %s seconds" % (time2 - time1, timekind)

f = open("speedtest.in", "r")
scanner = plex.Scanner(lexicon, f)
time1 = time()
while 1:
    value, text = scanner.read()
    if value is None:
        break

time2 = time()
_, lines, _ = scanner.position()
time = time2 - time1
lps = float(lines) / float(time)
print "Scanning %d lines took %s %s seconds (%s lines/sec)" % (lines, time,
                                                               timekind, lps)
示例#29
0
 def create_scanner(self, fp):
     """ Creates a plex scanner for a particular grammar 
     to operate on file object fp. """
     self.scanner = plex.Scanner(self.lexicon, fp)
     self.la, self.text = self.next_token()
示例#30
0
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")