Experimental purpose
Understand the basic methods and contents of mastering operator Priority analysis, learn to think scientifically and solve problems, improve the ability of program design.
Experimental content and requirements
An analytic interpreter is designed with operator precedence analysis method, lexical analysis, parsing, and expression evaluation of input assignment statement, output statement and clear statement, and stored in the specified variable; if there is an error, the information about the error is indicated.
Grammar representation
S-V=e | E? | Clear
E-E+t | e–t | T
T-T*f | t/f | F
F-(E) | V | C
Problem analysis
Because of the requirement of the operator precedence analysis method to parse the program, we should design the precedence relation table according to the grammar given. For each non-terminator, find out:
FIRSTVT (s) = {V,?, clear} LASTVT (s) = {?, clear}
FIRSTVT (f) = {(, V, c} lastvt (f) = {), V, c}
FIRSTVT (t) = {*,/, (, V, c} lastvt (t) = {*,/,), V, c}
FIRSTVT (E) = {+,-, *,/, (, V, c} lastvt (E) = {+,-, *,/,}, V, c}
The species code of the symbol is shown in the following table:
The Priority relationship table is as follows:
Let's briefly analyze the general process of parsing:
When there is a sentence to be processed, first of all, its lexical analysis to decompose each symbol in the sentence, and then the sentence according to the operator precedence algorithm into the stack, if it can be successfully normalized, it is a legitimate sentence, otherwise the sentence is illegal.
Here's one thing to consider, which is how to make a return. Since the grammar is given, we consider designing a grammar table in which the contents of the grammar table are the order of the species codes that can be attributed to the string, such as v=e can be represented as 9,1,13. In this case, when we are going to have a time-out, we can only use the species code that stores the symbol in the leftmost phrase in order, and then match the species code sequence with the grammar table to know what the current attribution needs to be done.
Another point to note is how to evaluate an expression. Here we need to design a two-tuple variable name table, which can return the variable's data based on the name of the variable. The detailed design of the variable name table is shown in the Detail design section.
Because it is a simplified analysis, this program only considers the processing of integers.
with the above analysis, you can construct a flowchart for the operator precedence analysis algorithm, as shown in.
Detailed design
(1) Lexical analysis part
Because the content of lexical analysis has been introduced in course design 1, and this time the state transition diagram is very similar to the course design 1, so there is not much to introduce.
(2) Priority relation table
In the program we use a two-dimensional array pritable[][] to store the precedence relationship between the operators. Pritable[a][b]=1 represents a>b;. Pritable[a][b]=0 represents a=b;. Pritable[a][b]=-1 says a<b;
(3) Design of the return stack
Because it's a bit of a hassle to really design a stack to handle the problem, you can use an array of s[] instead of the stack, 2.1 shows that I represent the stack top pointer, and S[i] represents the element at the top of the stack. The node in the stack is constructed with a data structure node that has three elements, the operator's word name str, the operator's species code type, and the operator's value Num. When our lexical analysis is used to scan a word, the word name, species code, and value of node nodes can be populated.
(4) The process of attribution
In the problem analysis, we introduce the grammar table for the normalization, the grammar table gratable is a two-dimensional array, the species code that holds the operator in the grammar. At the time of attribution, as long as the species code of the leftmost phrase is matched to an item in the grammar table, it can be normalized by that attribution operation. The steps of the regression refer to the operation in the flowchart, set the top pointer of the stack, set the species code of the top node of the stack to be non-terminator, and save the result of this one-time attribution. For example, for example, e->e+t this time, e+t produces a result, which is ultimately stored in the numerical field of the top node of the stack. It is important to note that the primary function of the numeric fields in node nodes is to save the results in the attribution.
(5) Variable name table
The function of the variable name table vartable is to return the value of the variable according to the variable name, which is a sequence of two tuples, so that it can be implemented by using the map in the STL template. The main use of the variable name table is in the evaluation of the assignment statement and expression, for example, when an assignment statement a=5 need to be reduced, check whether the variable a is present in the variable name table, if it does not exist, add a and 5 to the variable name table, if a exists then you can update the value of a in the Variable name table. And in a f->v like this, if you find V is not in the variable name table, it means that V is a variable that is not assigned, which results in an exception.
Input and output
The output of the program is: Determine whether the syntax is correct, the result of the output expression evaluation, and the output error message.
sample input (input from file In.txt ):
A = 5
b = A + 10
B?
B + A * a?
A = (A + b) * (B-A) + 5 + 4/2
A?
Clear
AB =
sample output (output to file OUT.txt ):
A = 5 is a valid sentence, execution succeeds
b = A + 10 is a valid sentence, execution succeeds
B? For legitimate sentences, execution succeeds
The value of B is 15
B + A * a? For legitimate sentences, execution succeeds
The value of B + A * A is 40
A = (A + b) * (B-A) + 5 + 4/2 for valid sentences, successful execution
A? For legitimate sentences, execution succeeds
A has a value of 207
Clear is a valid sentence, execution succeeds
AB = statement syntax error occurred!
Conclusion
Winter vacation at home want to try to do a C language compiler, these days in the home to study the compiling principle of the Dragon book, incidentally, the school-time compiling principles of the course design to the blog.
To tell the truth, the contents of Dragon book is very difficult, I learned the principle of compiling, but also in the second chapter on the card, so that our curriculum knowledge is very shallow. This is the time to put the foundation firmly, study the Dragon book for some time, and then consider how to do this compiler. In a few days should start to do, then the development of the document and learn some new knowledge should be updated to the blog, but also a summary of their own it.
Course design of compiling principle--Parser