1 / 10

CFGs: Formal Definition

CFGs: Formal Definition. G = (V, S , P, S) V = variables a finite set S = alphabet or terminals a finite set P = productions a finite set S = start variable S  V Productions’ form, where A  V, a (V S ) * : A  a. CFGs: Derivations. Derivations in one step:

libby
Download Presentation

CFGs: Formal Definition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CFGs: Formal Definition G = (V, S, P, S) V = variables a finite set S = alphabet or terminals a finite set P = productions a finite set S = start variable SV Productions’ form, where AV, a(VS)*: • A  a

  2. CFGs: Derivations Derivations in one step: bAgGbag Aa P  xS*, a,b,g(VS)* Can choose any variable for use for derivation step. Derivations in zero-or-more steps: G* is the reflexive and transitive closure of G . Language of a grammar: L(G) = {xS* | S G* x}

  3. S A B Root label = start node. A A b B Each interior label = variable. a a b Each parent/child relation = derivation step. Each leaf label = terminal or e. All leaf labels together = derived string = yield. Parse Trees S  A | A B A e | a | A b | A A B b | bc | B c | b B • Sample derivations: • S  AB  AAB aAB aaB aabB aabb • S  AB  AbB  Abb AAbb Aabbaabb These two derivations use same productions, but in different orders.

  4. S A B A A b B a a b Left- & Rightmost Derivations S  A | A B A e | a | A b | A A B b | bc | B c | b B • Sample derivations: • S  AB  AAB aAB aaB aabB aabb • S  AB  AbB  Abb AAbb Aabbaabb • These two derivations are special. • 1st derivation is leftmost. • Always picks leftmost variable. • 2nd derivation is rightmost. • Always picks rightmost variable.

  5. Left / Rightmost Derivations • In proofs… • Restrict attention to left- or rightmost derivations. • In parsing algorithms… • Restrict attention to left- or rightmost derivations. • E.g., recursive descent uses leftmost; yacc uses rightmost.

  6. S  A | A B A e | a | A b | A A B b | bc | B c | b B Other derivation trees for this string? w = aabb S S S A B A B A A A b B A A b A A a a b a A b A A A b a a A b e a Derivation Trees Infinitely many others possible.

  7. Ambiguity CFG ambiguous any of following equivalent statements: •  string w with multiple derivation trees. •  string w with multiple leftmost derivations. •  string w with multiple rightmost derivations. Defining ambiguity of grammar, not language.

  8. Ambiguity & Disambiguation Given ambiguous grammar, would like an equivalent unambiguous grammar. • Allows more knowledge about structure of derivation. • Simplifies inductive proofs on derivations. • Can lead to more efficient parsing algorithms. • In programming languages, want to impose a canonical structure on derivations. E.g., for 1+23. Strategy: Force an ordering on all derivations.

  9. Exp  n | Exp + Exp | Exp  Exp What is an equivalent unambiguous grammar? Exp  Term | Term + Exp Term  n | n Term Uses operator precedence left-associativity Disambiguation Example

  10. Parsing Designations • Major parsing algorithm classes are LL and LR • The first letter indicates what order the input is read – L means left to right • Second letter is direction in the “parsing tree” the derivation goes, L = top down, R = bottom up • K of LL(k) or LR(k) is number of symbols lookahead in input during parsing • Power of parsing techniques • LL(k) < LR(k) • LL(n) < LL(n+1), LR(n) < LR(n+1) • Choice of LL or LR largely religious

More Related