preamble
The purpose is pure, to make a simple new simple programming language based on Python. On the one hand, it is to expand my horizons, on the other hand, it is as a temporary transition program for my B.S. (Yes, the algorithmic platform mentioned earlier, there is no certainty that it will be developed quickly, even if I use a lot of scaffolding to complete the development, but the algorithmic containers, the rpc algorithmic scheduling middleware need to build their own wheels, which is difficult, in addition, there is also the user portion of the UI design, etc., and the most important thing is that that the group of teachers simply can't understand) The most important thing is that the teachers can't understand this kind of project. There is no need to engage in too "fancy" but despite this, the project I still want to develop later, mainly because of the algorithm container and rpc algorithm scheduling middleware, this is very worthwhile for me to do. The ideas involved are very useful. Although I'm now conceptualizing inside my head what I'm going to do, the amount of coding for this is just too much. And the target institution changed the exam 11408, now it causes me to be very passive, therefore, I decided to write a sample computer language. at the same time, in order to speed up the development process, directly using Python to write, later switch to Pypy, and then compile the compiler of this language.
The goal then, if you will, is to make it simple and straightforward to do in Chinese, to exercise the mind for small children. Of course, it's also to make it easier to tell me stories. Can in front of that gang of corpses of teachers to say more things they can understand. There is no way an ordinary college, many teachers level is also that, very helpless, but there is no way to change.
selection
target group
There is no sample, look like, then the main purpose of this programming language words, is easy to learn and easy to use. Launched Chinese programming, compatible with Python, easy to cultivate elementary school students to exercise programming thinking, suitable for elementary school students in grades one to two to learn. Unlike graphical programming, Hlang can experience a more realistic programming environment, and will not increase the difficulty. It can develop your child's logical thinking and at the same time. Forget it, can't program it, it's just a dome, and at the same time used to cope with cope with the BSc.
goal
There is no goal, is mixed ~~ The goal of this paper, to implement a simple syntax parser. Anyway, just write a few thousand lines of code and turn in a bunch of jerks!
technological realization
Based on Python, embodies the idea of embodiment, not the pursuit of operational efficiency, focusing on good learning, for small children to play. Don't some parents always say that English is difficult and computers are simple? Come on, let's use this!
Objective of this paper
Write a simple grammar parser and get off work~ When you get tired of high math, play this, and when you get tired of this, study English.
effect
OK, let's see our implementation first:
This one is a simple syntax parser.
realization
As an aside, let's take a look at how the implementation is carried out.
The first step is to define our standard legal characters:
TT_INT = "Integer" TT_FLOAT = "Floating point." TT_PLUS = "Plus." TT_DIV = "Divisor." TT_MINUS = "Minus." TT_LPAREN = "Left bracket" TT_RPAREN = "Right bracket" TT_MUL = "Multiply." TT_POWER = "Power." DIGITS = "123456789"
Then we define a Token to encapsulate these objects
class Token: def __init__(self,is_type,is_value=None): self.is_type = is_type self.is_value = is_value def __repr__(self): if self.is_value: return "|typology:{},(be) worth:{}|".format(self.is_type,self.is_value) return "|typology:{}|".format(self.is_type) def __str__(self): if(self.is_value): return "|{}|".format(self.is_value) return "|这个对象没有(be) worth,typology为:{}|".format(self.is_type)
The purpose of what we're trying to do here is simple, and that is, to take what's typed next, or the text content, and read it, and parse things out, and collect the legal characters. Note that we don't have any concept of variables here yet, we're just in charge of parsing good basic legal characters here. As for the introduction of variables to later, because this time to design a clear basic syntax specification, and then just follow a meal to borrow is finished.
character pointer
After that, we have defined the Token, then we have to read and parse the text, there is no way, we can only scan one character at a time. In order to facilitate, therefore, here on the character pointer for a simple package.
class Position: def __init__(self, idx, ln, col): = idx = ln = col def advance(self, cur_char): += 1 += 1 if cur_char == '\n': += 1 = 0 return self
Type of error
After that, we have to define the error. For example, I want to report an error when I enter an illegal character, just like Python:
So we're going to have a thing about that too:
""" Top Bugs (Boss) """ class HlangError: def __init__(self, pos_ln,in_fn,error_name, details): """ :param pos_ln: error line :param in_fn: input file :param error_name: error name :param details: Error details, description """ self.pos_ln = pos_ln self.in_fn = in_fn self.error_name = error_name = details def as_string(self): red_code = "\033[91m" reset_code = "\033[0m" result = f'{self.error_name}: {}\n' result += f'come from (a place) {self.in_fn}, line {self.pos_ln + 1}' return red_code+result+reset_code class IllegalCharError(HlangError): """ Illegal character error """ def __init__(self, pos_ln,in_fn, details): super().__init__(pos_ln, in_fn, 'Illegal characters', details)
grammatical analysis
After that, it's time to start parsing the syntax.
For this code, it's very simple, just add it to death
""" Syntax parser """ class Lexer: def __init__(self, in_fn, text): """ :param in_fn: where the text is input from (the file the text is in, standard input, output is also a file) It's actually the filename~~~ :param text: text to be parsed """ self.in_fn = in_fn = text = Position(-1, 0, -1) self.cur_char = None () # Basic symbol handling self.char_pro_base = { '+':TT_PLUS, '-':TT_MINUS, '*':TT_MUL, '/':TT_DIV, '^':TT_POWER, '(':TT_LPAREN, ')':TT_RPAREN } def advance(self): (self.cur_char) self.cur_char = [] if < len() else None def __char_process(self,tokens,TT): """ A way to handle basic characters that Add the Token and move the character pointer :return. """ (Token(TT)) () def make_tokens(self): """ the text of the characters added to the syntax parser, will meet the grammatical specification of the content, encapsulated as Token, (just like Spring will object information and then encapsulated as a Wapper to facilitate subsequent operations). (just like Spring will be the object information and then encapsulated as a Wapper, to facilitate subsequent operations.) :return. """ tokens = [] while self.cur_char != None: if self.cur_char in ' \t': # Tabs (spaces), meaningless, move forward () elif self.cur_char in DIGITS: # If it's a number, it automatically searches forward and adds the number and determines the type. #Numbers are special and are not involved character by character (the keywords to be defined later are similar) (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 () return [], IllegalCharError(,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 () if dot_count == 0: return Token(TT_INT, int(num_str)) else: return Token(TT_FLOAT, float(num_str))
After that, don't forget to ask for a run as an entry point to run up:
""" Language Parsing, Running Entry """ def run(fn, text): lexer = Lexer(fn, text) tokens, error = lexer.make_tokens() return tokens, error
each other
At the end of the day, it's all about our interactions:
""" Hlang is a Sample Language shell Just a sample example for learning by Huterox """ import basic while True: input_text = input("Interactive terminals:") result, error = ('<standard input>', input_text) if error: print(error.as_string()) else: print(result)
And then it's done. So easy.
Above is based on Python to write a syntax parser details, more information about Python syntax parser please pay attention to my other related articles!