Class 08 B

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

www.th-deg.

de

Syntax analysis
1. Introduction
2. Parsing techniques and derivations
3. Verifying the language generated by a grammar
4. Preprocessing grammars
5. Problems with grammars
6. Top-down parsing
7. Automatic top-down parsing
8. Table-driven parser
9. Bottom-up parsing
10. Operator precedence
11. LR(0) parsing
12. From SLR to LR(k)
13. LR(1) parsers
14. LALR parsers
TECHNISCHE HOCHSCHULE DEGGENDORF
2
www.th-deg.de

Verifying the language generated by a


grammar
• To proof that a given grammar G produces some language L, i.e.,
L=L(G)
– Proof L(G)⊆LL : Every string produced by G is in L (induction
over length of derivations)
– Proof L⊆LL(G) : Every string of L can be produced by G
(induction over string length)
• Example:
– S→ε|(S)S
generates all strings of balanced parenthesis – and only those

TECHNISCHE HOCHSCHULE DEGGENDORF


3
www.th-deg.de

Verifying the language generated by a


grammar
• Example:
S→ε|(S)S
generates all strings of balanced parenthesis – and only those
• L(G)⊆LL (every produced string is balanced)
– n=1: Length of derivation is 1 => empty string => qed
– n>1: All derivations of length n'<n produce balanced
parenthesis; any derivation of length n has the form
S => (S)S =>* (x)S => (x)y
where both, x and y can be derived in less than n steps and are
therefore balanced; therefore, (x)y is also balanced => qed

TECHNISCHE HOCHSCHULE DEGGENDORF


4
www.th-deg.de

Verifying the language generated by a


grammar
• Example:
S→ε|(S)S
generates all strings of balanced parentheses – and only those
• L⊆LL(G) (every balanced string is produced)
– Observe: Every string of balanced parentheses has even length
– n=0: Length of string is 0 => empty string => qed
– n≥1: Every balanced string of length l<2n is derivable from S.
• Let w be a string of balanced parentheses of length l<2n, n≥1
• Let (x) be the shortest nonempty balanced prefix of w => w=(x)y
• Length of both, x and y, is less than 2n => x, y derivable from S
• => there are derivations S =>* x; S =>* y, and therefore
S => (S)S =>* (x)y, and thus S =>* w
TECHNISCHE HOCHSCHULE DEGGENDORF
5
www.th-deg.de

Syntax analysis
1. Introduction
2. Parsing techniques and derivations
3. Verifying the language generated by a grammar
4. Preprocessing grammars
5. Problems with grammars
6. Top-down parsing
7. Automatic top-down parsing
8. Table-driven parser
9. Bottom-up parsing
10. Operator precedence
11. LR(0) parsing
12. From SLR to LR(k)
13. LR(1) parsers
14. LALR parsers
TECHNISCHE HOCHSCHULE DEGGENDORF
6
www.th-deg.de

Elimination of left recursion

• Grammar G is left recursive iff


∗ +
∃ A ∈ N ∃α∈( N ∪T ) : A ⇒ A α
• Left recursion cannot be handled by top-down parsers
• Immediate left recursion already covered
• Problem: Indirect left recursion, e.g.:
– S → Aa | b
A → Ac | Sd | ε

TECHNISCHE HOCHSCHULE DEGGENDORF


7
www.th-deg.de

Elimination of left recursion


• Algorithm ElimLeftRec (=4.19)
• Input: Grammar G w/o cycles or ε-productions (there are elimination strategies)
• Output: Equivalent grammar G' (i.e., L(G)=L(G')) w/o left recursion
1. Order nonterminals A1, A2, …, An
2. For each i = 1 … n:
1. For each j = 1 … i-1:
1. Replace each production Ai → Aj γ by the productions
Ai → δ1 γ | … | δk γ, where
Aj → δ1 | … | δk are all current Aj-productions
2. Eliminate immediate left recursions among Ai productions:
1. Order Ai → Ai α1 | … | Ai αm| β1 | … | βq
2. Replace these by
Ai → β1 Ai' | … | βq Ai'
Ai' → α1 Ai' | … | αm Ai' | ε
TECHNISCHE HOCHSCHULE DEGGENDORF
8
www.th-deg.de

Elimination of left recursion


• Example:
S → Aa | b
A → Ac | Sd | ε
– Order: S, A
– i=1, j=?: NOP
– i=2, j=1:
• Replace A → Sd by
A → Aad | bd
to obtain A → Ac | Aad | bd | ε
• Eliminate direct recursion:
A → bd A' | A'
A' → c A' | adA' | ε
plus:
S → Aa | b
TECHNISCHE HOCHSCHULE DEGGENDORF
9
www.th-deg.de

Left factoring
• Useful for creating grammars for top-down parsers:
– Choice between alternative productions may be unclear
– rewrite grammar so that decision is postponed to a point
where enough input has been gathered
• Example:
– Original grammar:
A → αβ1 | αβ2
(which β to use? β1 or β2?)
– Left factored:
A → α A'
A' → β1 | β2

TECHNISCHE HOCHSCHULE DEGGENDORF


10
www.th-deg.de

Left factoring
• Algorithm LeftFact (=4.21)
• Input: Grammar G
• Output: Equivalent left-factored grammar
1.While ∃ nonterminal A with several productions using the same prefix:
1. Determine longest common prefix α in productions
A → α β1 | … | α βn | γ1 | … | γm
2.If α≠ε:
1.Replace
A → α β1 | … | α βn | γ1 | … | γm
by
A → α A' | γ1 | … | γm
A' → β1 | … | βn

TECHNISCHE HOCHSCHULE DEGGENDORF


11
www.th-deg.de

Left factoring
• Example:
A → if E then S | if E then S else S | a
E→b
Here, it is not immediately clear on reading the input (up to S),
which production to use – should we read on or produce the "if"?
– α=if E then S
– Replace
A → if E then S | if E then S else S | a
by
A → if E then S A' | a
A' → ε | else S
– Result:
A → if E then S A' | a
A' → ε | else S
E→b
TECHNISCHE HOCHSCHULE DEGGENDORF
12

You might also like