Frequently Asked Questions 1: Elimination left recursive, mention common factor
-
Elimination of left recursion:
P → P [alpha] | beta] (beta] does not begin P)
can rewrite rule:
P → [beta] p '
P' → [alpha] P '| [epsilon]
Example: S → Sa | b
eliminate left recursion:
S → the bS'
S ' → aS '| ε -
Mention common factor
S → aS | aa | ab
mention common factor:
S → aA;
A → S | A | B; -
Elimination of all the left recursive grammar
① the grammar nonterminals all arranged in certain order A1, A2 ......
② directly from A1 to eliminate left recursion start of A1 (if present), A1 A2 Alternatively right portion of the right portion A1, if there is the direct left recursion elimination of.
③ eliminate useless productions, starting production rules nonterminals never reach that is, from the start symbol.
Is simply generated Substituting other production, eliminating direct left recursive
Example G: S → Qc of | C
Q → Rb | B
R & lt → Sa | A
first S substituted into R: R → Qca | ca | a ( no left recursively continued)
of Q is substituted into R: R → Rbca | bca | ca | a ( a direct left recursive, eliminating)
finally obtained:
S → Qc of | C
Q → Rb | B
R & lt → BCAR '| CAR' | aR '
R & lt '→ bcaR' | ε
Common mold structure prediction analysis table 2, it is judged LL (1) grammar
Ideas: first requires grammar first set, follow set
again in accordance with first set, follow set predictive analysis table configured
Finally, according to Rule 3 LL (1) grammar is determined whether LL (1) grammar
For a production:
S → ABCC | C | BA
A → A | [epsilon]
B B →
C → C
-
FIRST set
(1) S → ... for the production of this, if the first candidate for the right-hand side terminator, the terminator was added directly to first (S), this embodiment belonging to {c} First (S)
(2) for S → ... of the production, if the right-hand side of the first candidate non-terminal symbol, the first set of non-terminal elements into non-ε first (S), a first embodiment of the set {a} was added first ( S)
(. 3) S → ... for this production, if the right side of the first non-terminal symbol and a symbol followed by a number of non-terminals, to be noted that the first possibility is nonterminal ε.
If the presence of a nonterminal first set of candidate formula ε, then after a nonterminal also added first (S), and so on.
If all nonterminal can derive ε, {ε} was added first (S)
Example {first (A) - ε} + {first (B) - ε} to be added first (S) -
FOLLOW set
(1) to the start symbol S, the # added to Follow (S)
(2) Find grammar all productions candidate formula nonterminal find nonterminals required
(2.1) If nonterminal as the terminator, the terminator was added follow current
embodiment {c} was added follow (C)
(2.2) if a non-terminal symbol nonterminal symbol, the first set of nonterminals added follow current
embodiment first (C) was added follow (B)
(2.3) If a nonterminal symbol candidate tailpipes, the production of the follow set added follow set nonterminals
in follow Example (S) was added follow (a)
Note: follow (B) also comprises follow (S), a may be ε, B are also likely candidates at the end of the formula -
Structure prediction analysis table (can find select set may be directly determined)
Example:
grammar G: E → the TE '
E' → + the TE '| [epsilon]
T → the FT'
T '→ * the FT' | [epsilon]
F. → (E) | I
FIRST set
FIRST (E) = {(, the I}
FIRST (E ') = {+, [epsilon]}
FIRST (T) = {(, the I}
FIRST (T') = {*, [epsilon]}
FIRST (F.) = {(, the I}
FOLLOW set
FOLLOW (E) = {), #}
FOLLOW (E ') = {), #}
FOLLOW (T) = {+,), #}
FOLLOW (T') = {+,) , #}
FOLLOW (F.) = {*, +,), #}
structure prediction analysis table
(1) for the production F → (E) | i, a first candidate character type as the terminator, select (F → ( E)) = {(}, select (F → i) = {i}
prediction analysis table within the direct recording, as shown in
(2) for the generation of formula E → TE ', the first character candidate of formula non-terminator, SELECT (E → the TE ') = First (T) = {I, (}
(. 3) for generating the formula E' → + TE '| ε , the presence of the candidate of formula E' → ε, select (E '→ ε) = follow (E ') = {), #}
-
Analyzing LL (1) grammar
(1) left free grammar recursively
for each (2) A grammar nonterminals first candidate character set for each type of production mutually disjoint
i | F → (E): Example
first ( (E)) ∩first (i) is empty
(3) each grammar nonterminal a, if the first character candidate set exists that contains ε, then fIRST (a) ∩FOLLOW (a) is empty
Example: E '→ TE + '| [epsilon]
FIRST (E') ∩FOLLOW (E ') is empty
if the grammar rules are in line three, for the LL (1) grammar
above this grammar to comply with three rules, is LL (1) grammar