Then there is the recursive Descent analysis algorithm (also considered a subclass of the top-down analysis algorithm (also improved)) ...
And then give the pseudo-code ...
However the imagination is beautiful, the reality is cruel:
If, for example, the real thing is to be realized, it is that when the first token is read (it is obviously num), it is possible to find that both E + T and T are feasible, so there is a problem here ... But this situation can sometimes be avoided by using the particularity of the grammar:
In this example, E is ultimately nothing more than T + T + t + ... Format, and T is f * f * f * ... format, it is possible to call parse_t () after reading in 3, then read into a token, if +, call parse_t (), if not, stop calling ...
Next is a small assignment:
Recursive descent analysis algorithm
Given the following grammar g:
A-a B
| A
B-B
| C
Suppose you are writing a recursive descent analysis algorithm for this grammar, give a recursive descent analysis algorithm for the algorithm. What difficulties have you encountered? How do you solve these difficulties?
This is very simple, according to gourd painting scoop, pseudo code as follows
1 parse_a () {2 token = tokens[i++]; 3 if (token = = A) {4 token = tokens[i++]; 5 if (token = = EOF) {6 ret Urn 7 }else{8 parse_b () 9 }10 }else{11 error (...); }13}14 Parse_b () {+ token = tokens[i++];17 if (token = = B) { ... }else if (token = = c) {... }else{22 error (...); }24}
Reading dragon Book compiling Principles of Grammar Analysis (5) ...