Guided Worksheet Formal Definition of Limit: An Instrument ...
CFGs: Formal Definition
-
Upload
erimentha-arianna -
Category
Documents
-
view
34 -
download
1
description
Transcript of CFGs: Formal Definition
11
CFGs: Formal DefinitionCFGs: Formal Definition
G = (V, G = (V, , P, S), P, S)
VV = variables= variables a finite seta finite set
= alphabet or terminals= alphabet or terminals a finite seta finite set
PP = productions= productions a finite seta finite set
SS = start variable= start variable SSVV
Productions’ form, where AProductions’ form, where AV, V, (V(V))**:: A A
22
CFGs: DerivationsCFGs: Derivations
Derivations in one step:Derivations in one step:AA GG A A P P x x**, , ,,,,((VV))**
Can choose Can choose anyany variable for use for derivation step. variable for use for derivation step.
Derivations in zero-or-more steps:Derivations in zero-or-more steps:GG
** is the reflexive and transitive closure of is the reflexive and transitive closure of G G ..
Language of a grammar:Language of a grammar:L(G) = {xL(G) = {x** | S | S GG
** x} x}
33
Parse TreesParse TreesSample derivations:
S AB AAB aAB aaB aabB aabbS AB AbB Abb AAbb Aabb aabb
S A | A BA | a | A b | A AB b | b c | B c | b B
These two derivations use same productions, but in different orders.
Root label = start node.
Each interior label = variable.
Each parent/child relation = derivation step.
Each leaf label = terminal or .
All leaf labels together = derived string = yield.
S
A B
AA Bb
a a b
44
Left- & Rightmost Left- & Rightmost DerivationsDerivations
Sample derivations:S AB AAB aAB aaB aabB aabb
S AB AbB Abb AAbb Aabb aabb
S A | A BA | a | A b | A AB b | b c | B c | b B
S
A B
AA Bb
a a b
These two derivations are special.
1st derivation is leftmost.Always picks leftmost variable.
2nd derivation is rightmost.Always picks rightmost variable.
55
Left / Rightmost DerivationsLeft / Rightmost Derivations
In proofs…In proofs… Restrict attention to left- or rightmost derivations.Restrict attention to left- or rightmost derivations.
In parsing algorithms…In parsing algorithms… Restrict attention to left- or rightmost derivations.Restrict attention to left- or rightmost derivations. E.g., recursive descent uses leftmost; E.g., recursive descent uses leftmost; yaccyacc uses uses
rightmost.rightmost.
66
Derivation TreesDerivation Trees
Infinitely many others
possible.
S
A B
AA b
a
a
bA
S
A
A A
AA bA
a
a
bA
S
A B
AA Bb
a a b
S A | A BA | a | A b | A AB b | b c | B c | b B
w = aabbOther derivation
trees for this string?
77
AmbiguityAmbiguity
CFG CFG ambiguousambiguous any of following equivalent any of following equivalent statements:statements: string w with multiple derivation trees.string w with multiple derivation trees. string w with multiple leftmost derivations.string w with multiple leftmost derivations. string w with multiple rightmost derivations.string w with multiple rightmost derivations.
Defining ambiguity of Defining ambiguity of grammargrammar, not language., not language.
88
Ambiguity & DisambiguationAmbiguity & Disambiguation
Given ambiguous grammar, would like an Given ambiguous grammar, would like an equivalent unambiguous grammar.equivalent unambiguous grammar. Allows more knowledge about structure of derivation.Allows more knowledge about structure of derivation. Simplifies inductive proofs on derivations.Simplifies inductive proofs on derivations. Can lead to more efficient parsing algorithms.Can lead to more efficient parsing algorithms. In programming languages, want to impose a In programming languages, want to impose a
canonical structure on derivations. E.g., for canonical structure on derivations. E.g., for 1+21+233..
Strategy: Force an ordering on all derivations.Strategy: Force an ordering on all derivations.
99
Disambiguation Example Disambiguation Example
ExpExp nn
| Exp | Exp ++ Exp Exp
| Exp | Exp Exp Exp
What is an equivalent What is an equivalent unambiguous unambiguous
grammar?grammar?
ExpExp Term Term
| Term | Term ++ Exp Exp
TermTerm nn
| | nn Term Term
UsesUses operator precedenceoperator precedence left-associativityleft-associativity
1010
Parsing DesignationsParsing Designations
Major parsing algorithm classes are LL and Major parsing algorithm classes are LL and LRLR
The first letter indicates what order the input is The first letter indicates what order the input is read – L means left to rightread – L means left to right
Second letter is direction in the “parsing tree” Second letter is direction in the “parsing tree” the derivation goes, L = top down, R = bottom upthe derivation goes, L = top down, R = bottom up
K of LL(k) or LR(k) is number of symbols K of LL(k) or LR(k) is number of symbols lookahead in input during parsinglookahead in input during parsing
Power of parsing techniquesPower of parsing techniques LL(k) < LR(k)LL(k) < LR(k) LL(n) < LL(n+1), LR(n) < LR(n+1)LL(n) < LL(n+1), LR(n) < LR(n+1)
Choice of LL or LR largely religiousChoice of LL or LR largely religious