1 / 9

Context-Free Grammars & Languages

Context-Free Grammars & Languages. Example. <program> -> <stmt> <program> |

dillian
Download Presentation

Context-Free Grammars & Languages

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 & Languages

  2. Example <program> -> <stmt> <program> | 𝜆 <stmt> -> <assign>|<if>|<while> |… <assign> -> x = 5 | x = 10 <if> -> if (<cond>) <block-stmt> | if (<cond>) <block-stmt> else <block-stmt> | … <cond> -> x == 10 | flag | … <block-stmt> -> <stmt><block-stmt> | 𝜆 …

  3. Definition A context-free grammar G = (V, , S, P), where each rule in P has the form A  x • A is a variable (A  V) • x is a string of variables and terminals (x  (V  )* )

  4. Language of CFG • Given a CFG G = (V, , S, P), w ∊ L(G) iff w ∊ * and S =>* w • L(G) = { w ∊ * | S =>* w }

  5. Context Free Languages A language L is context-free iff There exists a CFG G such that L(G) = L

  6. Parsing • Given a grammar G and string w find derivation of w in G (if it exists.)

  7. Derivations and Parse Trees • Left-derivation: Always replace the left-most variable • Right-derivation: Always replace the right-most variable • Parse tree: Display derivation in a tree • Root is start variable • Node’s children are symbols of rule used in derivation

  8. Ambiguity • A CFG G is ambiguous if • There exists w ∊ L(G) such that • There is more than 1 parse tree for 2 in G. • A language L is inherently ambiguous if • For all CFG G, where L(G) = L, • G is an ambiguous grammar

  9. Tasks • Remove ambiguity from grammars where possible • Efficient creation of parse trees

More Related