In the following article, from the perspective of C ++ lexical analysis, a language is a set of sentences, and a sentence is a non-linear structure composed of the marks returned by the lexical analyzer, the task of C ++ syntax analysis is to divide the lexical symbols into various types of syntax units based on the syntax rules of the language.
The best way to reflect the sentence structure is tree, which is commonly used in the analysis tree and syntax tree. There are two basic methods to analyze the syntax structure: top-down analysis and bottom-up analysis. Top-down analysis builds an analysis tree from the root to the leaves, while bottom-up analysis is the opposite. In both cases, the analyzer scans input from left to right and reads a mark each time. Similar to C ++ lexical analysis, syntax analysis also has two meanings:
① The rule for sentence formation is also known as the syntax rule. Most of the syntax rules of the programming language can be described using context-independent syntax ContextFreeGrammar (CFG.
② Identify the review structure in the mark stream based on the syntax rules, also known as syntax analysis. The most effective top-down and bottom-up analysis methods can only process sub-classes of contextual grammar, such as LL grammar and LR method, but they are sufficient to cope with the vast majority of syntax phenomena of program design reviews.
I. Task and purpose
1. Use the C/C ++ programming language and recursive descent subroutine to compile the C ++ lexical analyzer of the function drawing language. It is also required to design a syntax analyzer test applet to call a self-compiled syntax analyzer to test various inputs.
2. The task of syntax analysis is to divide the lexical symbols into various types of syntax units based on the syntax rules of the language based on the C ++ lexical analysis. Syntax analysis is based on the syntax rules of the language. The syntax rules are generally described in context-independent grammar.
- Analysis of components of Visual C ++ 6.0
- Detailed description of function objects in C ++
- How to use C ++ function objects?
- Comparison between C ++ function parameters and Java Transmission
- Analysis of C ++ function parameter reference
By writing a syntax analyzer by yourself, you can understand the regular syntax and the context-independent syntax CFG) basic concept derivation, analysis tree and syntax tree, elimination of ambiguity and ambiguity), top-down analysis of recursive descent subprogram method, prediction analysis table method, LL1) grammar) and bottom-up analysis. Understand how to link theory with practice and understand the differences between theory and practice.
Ii. Analysis and Design
Syntax analysis programs generally have the following functions: Perform syntax analysis on word and symbol strings and derive and normalize them according to the semantic rules) to identify various syntax units in the program, finally, determine whether the input string is a syntactic correct "program ".
Here we use the recursive descent Analysis Method: directly simulate the process of language generation in the form of a program. Its basic design philosophy is: To construct a sub-Program for each non-Terminator, each sub-program's process body is split according to the candidate item of the generated formula, and directly matches the Terminator, when a non-Terminator is encountered, the corresponding non-terminator subroutine is called.
This analysis starts from the subprogram that calls the grammar and starts the symbol until all non-terminologies are expanded as terminologies and matched. If this step is performed during the analysis, the analysis is successful. Otherwise, the input has a syntax error. The limit of recursive descent Analysis on grammar is that there must be no common left factor or left recursion. Because the syntax is defined recursively, subprograms are also recursive.
For a relatively small language, the recursive descent subroutine method is very effective. It is simple, flexible, and easy to construct. Its disadvantage is that the program is directly related to grammar, any change to grammar requires corresponding modifications to the program.
The following describes the general design of the C ++ lexical analysis program:
1. Write the context-independent grammar G for syntax analysis as required;
2. Eliminate the ambiguity of context-independent grammar G;
3. Remove the direct context-independent grammar G) left recursion and extract the left factor;
4. Construct and simplify the state conversion graph of grammar;
5. Convert the conversion graph into an EBNF representation;
6. Construct recursive descent subprograms from EBNF;
The following is a detailed design:
Overall structure and module division
Syntax Test Module(Parsermain. cpp)
Syntax Analyzer Module(Parser. h & parser. cpp) Graph language interpreter Portal Recursive subroutine set Traverse and print the expression syntax tree in sequence Error Handling Module
Lexical analyzer Module(Example. h & example. cpp) Initial lexical analyzer Identifies minimum syntax units with independent meanings Auxiliary Module |
|
|