"Long book Notes" implements a simple mathematical expression in Python using a translator (recursive descent analysis method) from the prefix to the suffix syntax

Source: Internet
Author: User

The previous note introduced some basic concepts related to grammar analysis, and this note is based on the Book of Dragons section 2.5the content implements a suffix syntax translator demo for simple expressions.
Note: The demo in the original book is a Java instance, and I will give the implementation of a logically consistent version of Python.
There are a few basic concepts that need to be introduced before the simple suffix Translator code can be implemented.
1. Top-down analysis Method (Top-down parsing)
as the name implies, the idea of top-down analysis is to derive the production type, to create the beginning of the symbol as the root node, from top to bottom in order to construct its child nodes, the final structure of the syntax analysisTree. When implemented, it scans the input string sequentially from left to right, building the parse tree during the scan.
Suppose you have the following set of grammar-generation:

then assume that the input string to be deduced now is:
for (; expr; expr;) Other
The derivation of the Top-down analysis method is as follows:

The algorithm steps used are:
1) as the root node with the production start symbol (that is, non-terminating symbol "stmt")
2) on Node n labeled non-Terminator A, select a production of a (in the above example, the input is a for statement, so select the For statement corresponding to the production), and the resultingeach symbol in the body constructs a child node of n
3) Look for the next node to construct the subtree, usually selected as the leftmost non-terminator of the parsing tree
4) Repeat 第2-3 step until the input string is scanned
in the implementation of the algorithm, there is an important contract term called lookahead symbol, which refers to the input string is currently being scanned Terminator , we often in the syntax of the implementation of the scanThe code sees the lookahead variable, so it is necessary to know its origin.
The previous derivation of the for statement is briefly explained by the above algorithm flow:
1) Select the production start symbol stmt as the root node
2) because the input is for the statement, so choose the production "for (optexpr; optexpr; optexpr) stmt "for derivation, at which point the lookahead symbol points to the terminating symbol" for ",as shown in (a) in the top section. Constructs a child node of the root node with the symbol in the for-generation, as shown in (b) in the middle of the constructed parse tree.
3) The child nodes of the root node have been constructed, and according to the algorithm flow, the sub-tree of the child node is now constructed, although the "for" node is the left-most node of the current analysis tree and has not yet been expanded.But it is Terminator cannot expand subtree, at this point, if the currently considered terminator symbol matches the lookahead symbol (in this case point to "for", exactly matches), then the syntax Analysis Tree in the figureArrows and arrows of the input string move forward one step, that is, the node of the parse tree points to the (node, the lookahead symbol points to the "(" in the input string. Because "(" is also a terminator andmatches the lookahead symbol, so one step further forward, at which point the parse tree arrow points to a non-terminator node labeled optexpr, and the lookahead symbol points to the inputA string of Terminator ";", because the lookahead symbol points to the ";" With the optexpr beginning of the production type can not match, so the use of empty production to expand the OPTEXPR node child nodes.
and so on, the resulting parse tree looks like this:

2. Recursive descent analysis Method (Recursion-descent parsing)
Recursive descent analysis is a top-down parsing method that uses a set of recursive processes to process input strings. each non-terminator in a grammar production has a procedure or function associated with it .
The Predictive Analysis Method (predictive parsing) is a simple recursive descent analysis method in which the control flow of each non-terminating symbol corresponding to a process or function can be determined by the lookahead symbol without ambiguity, that is, when using the predictive analysis method, The process of scanning the input string does not require backtracking (backtracking). The procedure call sequence that occurs when parsing an input string implicitly defines a parse tree for that input string.
The suffix syntax translator for simple expressions given later in this paper is implemented by using predictive analysis method.

3. Left/Right recursion (left/right recursion)
can occur when a non-terminator on the left side of a grammar production is at the same time as the non-terminator phase at the beginning of the producing body on the right.
Because of the production, Non-Terminator A also appears at the leftmost end of the production body, and when deduced from the recursive descent analysis method, it may lead to an infinite loop invocation, as shown in
Similarly, there is a right recursion problem with the following production:
Span style= "font-size:14px" >
when using recursive descent analysis to derive the above-mentioned production, a right recursive infinite loop may occur:

so before using the recursive descent analysis method, It is often necessary to modify the original grammar generation to eliminate the left/right recursion problem . Wikipedia's left recursion entry

is to translate the infix form of a simple mathematical expression into a suffix form, assuming that the given syntax guidance is defined as follows (where the left is a grammar-producing type, and the right is an additional semantic rule that defines the semantic rules from infix to postfix conversions):

The grammar guidance definition corresponding to the syntax guidance translation plan is as follows:

due to the existence of a left recursion problem (caused by non-terminator expr ) in the translation plan above, adjustments are required to eliminate left recursion, and the revised syntax-guided translation plan is as follows:

for the above translation plan, using the predictive analysis method, according to the algorithm flow described in Dragon Book section 2.5, the Python version of the simple mathematical expression from the prefix to the suffix syntax of the translator, the complete code is as follows.
#!/bin/env Python "This demo was inspired by sections 2.5 of the ' Dragon book ': <compilers:principles, Techniques, and Tools>it implements a syntax-directed translator for simple expressions like ' 1+2-3 ' It translate infix expression into Postfix form ' class Parser (object): lookahead = ' def __init__ (self): print ' "Input an expression wit        H infix form (one Character Per line): ' Parser.lookahead = raw_input () self.infix_list = [Parser.lookahead] Self.postfix_list = [] def expr (self): Self.term () while true:if parser.lookahead = = '  + ': self.match (' + ') self.term () self.postfix_list.append (' + ') elif Parser.lookahead = = '-': Self.match ('-') self.term () Self.postfix_list.appen                D ('-') else:print ' raw input is (infix form): ' Print '. Join (Self.infix_list) print ' PostFix form of the input is: ' print '. Join (self.postfix_list) return def term (self): If Self._isdigits (parser.lookahead): Self.postfix_list.append (Parser.lookahead) Self.match (parser.l            Ookahead) else:print ' term:syntax error ' def match (self, T): if Parser.lookahead = = T:  Parser.lookahead = Raw_input () self.infix_list.append (parser.lookahead) else:print ' Match:syntax error ' def _isdigits (self, s): Try:int (s) return True except Excep        tion, E:return falseclass Postfix (object): def main (self): parser = parser () parser.expr () print ' \ n ' if ' __main__ ' = = __name__: Postfix = Postfix () postfix.main ()
Run the above script and enter a valid simple math operation ( from the production, only 0-9 of the mathematical addition and subtraction is currently supportedInfix expression, the script translates it to the suffix syntax. Examples of interactions are:

Note: The script is intended to be an example of how to use predictive analysis to translate input strings, so the implementation is relatively simple and rude (such as input infix expression can only enter one character per line, otherwise error-_-).

Resources
1. Dragon Book section 2.3-2.5
2. Wikipedia:recursive Descent Parser
3. Wikipedia:left recursion

========================= EOF ========================


The

Dragon book note uses Python to implement a simple mathematical expression from the prefix to the suffix syntax (using recursive descent analysis method)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.