Documente Academic
Documente Profesional
Documente Cultură
Technique
A A1 | A2 | A3 | . Am | 1| 2| 3| n Replace A productions by A 1 A| 2 A|..| nA A 1 A| 2 A|..| m A|
Example
S Aa | b A Ac | Sd | A Ac | Aad | bd |
S Aa | b A bdA | A A cA | adA |
TOP-DOWN Parsing
Can be viewed as an attempt to find a leftmost derivation for an input string Is an attempt to construct a parse tree for the input string from the root and creating the nodes of the parse tree in preorder.
S cAd A ab | a
Example
(a)
a
(b)
a
(c)
Left Factoring
Is a grammar transformation Useful for producing a grammar suitable for predictive parsing. Example: For a production: A A A 1 | 2
X Y Z $
Output
Parsing Table M
Set ip to point ot the first symbol of w$; Repeat let X be top stack symbol and a the symbol pointed to by ip; if X is a terminal or $ then if X = a then pop X from the stack and advance ip else error() else /* X is a nonterminal */ if M[X,a] = X Y1 Y2Y3. Yk then begin pop X from the stack; push Yk Yk-1YK-2. Y1 onto the stack, with Y1 on top output the production X Y1 Y2Y3. Yk end else error() Until X = $ /* stack is empty */
Program
STACK
INPUT
$E
$ET $ETF $ETid
id + id * id$
id + id * id$ id + id * id$ Id + id * id$
$ET
$E $ET+ $ET $ETF $ETid $ET
+ id * id$
+ id * id$ + id * id$ id * id$ id * id$ id * id$ * id$
$ETF*
$ETF $ETid $ET $E $
* id$
id$ id$ $ $ $
T->*FT
F->id T->e E->e
FIRST and FOLLOW: Rules to compute FIRST(X); 1. If X is terminal, then FIRST(X) is {X} 2. If X e is a production, then add e to FIRST(X).
3. If X is nonterminal and X Y1 Y2 ..Yk is a production then place a in FIRST(X) if for some i, a is in FIRST(Yi), and e is in all of FIRST(Y1) . FIRST(Yi-1); that is Y1. Yi-1 * e.
If e is in FIRST(Yj) for all j -> 1 to k then add e to FIRST(X). Computing FIRST(X) for any string X1 X2 X nas follows add to FIRST(X1 X2 .. Xn) all the non-e symbols of FIRST(X1). also add the non-e symbols of FIRST(X2) if e is in FIRST(X1). and so on
FOLLOW(S)
Rules 1. Place $ on FOLLOW(S), where S is the start symbol and $ is the input right endmarker. 2. If there is a production A B, then everything in FIRST() except for e is placed in FOLLOW(B) 3. If there is a production A B, or a production A B where FIRST() contains e then everything in FOLLOW(A) is in FOLLOW(B)
Example
E TE E+TE | e T FT T *FT | e F ( E ) | id