web123456

Write a syntax parser based on Python

"""

Syntax parser

"""

class Lexer:

    def __init__(self, in_fn, text):

        """

        :param in_fn: Where to enter the text (the file where the text is located, the standard input, and the output is also a file)

        It's actually the file name~~~

        :param text: text to be parsed

        """

        self.in_fn = in_fn

        self.text = text

        self.pos = Position(-1, 0, -1)

        self.cur_char = None

        self.advance()

        #Basic symbol processing

        self.char_pro_base = {

            '+':TT_PLUS,

            '-':TT_MINUS,

            '*':TT_MUL,

            '/':TT_DIV,

            '^':TT_POWER,

            '(':TT_LPAREN,

            ')':TT_RPAREN

        }

    def advance(self):

        self.(self.cur_char)

        self.cur_char = self.text[self.] if self. < len(self.text) else None

    def __char_process(self,tokens,TT):

        """

        How to process basic characters,

        Add token and move character pointer

        :return:

        """

        (Token(TT))

        self.advance()

    def make_tokens(self):

        """

        Add characters in the text to the syntax parser and encapsulate content that complies with the syntax specifications as Tokens,

        (Just like Spring encapsulates object information into Wapper, it is convenient for subsequent operations.)

        :return:

        """

        tokens = []

        while self.cur_char != None:

            if self.cur_char in ' \t':

                #Tab (space), meaningless, move forward

                self.advance()

            elif self.cur_char in DIGITS:

                #If it is a number, automatically search forward, add the number and determine the type.

                #The number is quite special, not one character or one character participates (there is a similar thing to define keywords later)

                (self.make_number())

            else:

                TT = self.char_pro_base.get(self.cur_char)

                if(TT):

                    self.__char_process(tokens,TT)

                else:

                    char = self.cur_char

                    self.advance()

                    return [], IllegalCharError(self.,self.in_fn, "'" + char + "'")

        return tokens, None

    def make_number(self):

        num_str = ''

        dot_count = 0

        while self.cur_char != None and self.cur_char in DIGITS + '.':

            if self.cur_char == '.':

                if dot_count == 1: break

                dot_count += 1

                num_str += '.'

            else:

                num_str += self.cur_char

            self.advance()

        if dot_count == 0:

            return Token(TT_INT, int(num_str))

        else:

            return Token(TT_FLOAT, float(num_str))