1 / 8

Context-free grammars, derivation trees, and ambiguity

Context-free grammars, derivation trees, and ambiguity. Module 06.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez. Context-free Grammars Derivation Trees Ambiguity. Topics. Definition : A context-free grammar (CFG)

lpringle
Download Presentation

Context-free grammars, derivation trees, and ambiguity

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. Context-free grammars, derivation trees, and ambiguity Module 06.1COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez

  2. Context-free Grammars Derivation Trees Ambiguity Topics

  3. Definition: A context-free grammar (CFG) is a quadrupleG = (, , P, S),where all productions are of the formA →, for A ∊ and  ∊ (∪ )*. Re-writing using grammar rules: βAγ⇒βγif A → (derivation). Left-most derivation: At each step, the left-most nonterminal is re-written. Right-most derivation: At each step, the right-most nonterminal is re-written. Context-free grammars

  4. Sample grammar and derivations

  5. Derivation trees • Describe re-writes, independently of the order (left-most or right-most). • Each tree branch matches a production rule in the grammar. • Leaves are terminals. • Bottom contour is the sentence. • Left recursion causes left branching. • Right recursion causes right branching.

  6. Goal of parsing: Examine input string, determine whether it's legal. Same as: try to build derivation tree. Therefore, tree should be unique. Definition: A CFG is ambiguous if there exist two different right-most (or left-most, but not both) derivations for some sentence z. (Equivalent) Definition: A CFG is ambiguous if there exist two different derivation trees for some sentence z. Ambiguous grammars

  7. Classic ambiguities • Simultaneous left/right recursion: E → E + E → i • Dangling else problem: S → if E then S → if E then S else S → … Ambiguity is undecidable: no algorithm exists.

  8. summary • Context-free Grammars • Derivation Trees • Ambiguity

More Related