1 / 10

Context-free Grammars

Learn about context-free grammars, which are more powerful than regular languages. Originally developed by linguists, they play an important role in the compilation of programming languages.

Download Presentation

Context-free Grammars

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. [Section 2.1] Context-free Grammars • more powerful than regular languages • originally developed by linguists • important for compilation of programming languages

  2. [Section 2.1] Context-free Grammars • Example: A -> 0A1 • A -> B • B -> # • Terminology: • - substitution rules (productions) • variables (including the start variable) – typically upper-case • terminals – typically lower-case, other symbols • derivation, parse tree

  3. [Section 2.1] Context-free Grammars • Def 2.2: A context-free grammar is a 4-tuple (V,§,R,S), where • V is a finite set of variables • § is a finite set of terminals, V ŧ = ; • R is a finite set of rules, each rule is of the form A -> w where A 2 V and w 2 (V [§)* • S 2 V is the start variable • If A -> w 2 P, then we write uAv => uwv (read “uAv yields uwv”), and we write u =>* v (read “u derives v”) if u=v or if there exists a sequence u1,u2,…,uk such that • u => u1 => u2 => … uk => v • The language of the grammar is { w2§* | S=>*w }.

  4. [Section 2.1] Context-free Grammars • Examples: give context-free grammars for the following languages: • { aibjck | i=k, i,j,k ¸ 0 } • { aibjck | i=j, i,j,k ¸ 0 } • strings over { ( , ) } that are well-parenthesized • strings over { 0,1 } that contain equal number of 0’s and 1’s

  5. [Section 2.1] Ambiguity Example: [EXPR] -> [EXPR] + [EXPR] | [EXPR] x [EXPR] | ( [EXPR] ) | a Give a derivation (and parse trees) for the string a+axa. Notice: for every parse tree there is a unique left-most derivation.

  6. [Section 2.1] Ambiguity Def 2.7: A context-free grammar is called ambiguous if there exists a string that can be generated by two different left-most derivations. Note: Some context-free languages do not have unambigous grammars (e.g { aibjck | i=j or j=k } ). These are called inherently ambiguous. Example: give an unambigous CFG for the language of arithmetic expressions over { +, x, a }

  7. [Section 2.1] Closure properties of CFLs Thm: The class of context-free languages (CFLs) is closed under union. Thm: The class of CFLs is closed under concatenation. Thm: The class of CFLs is closed under star.

  8. [not in the book?] Closure properties of CFLs Thm: The class of CFLs is not closed under intersection. Thm: The class of CFLs is closed under intersection with regular languages. Thm: Every regular language is context-free.

  9. [Section 2.1] Chomsky Normal Form Def 2.8: A CFG is in Chomsky normal form if every rule is of the form A -> BC or A -> a, where B,C 2 V-{S}, and a 2§. Thm 2.9: Any CFL can be generated by a CFG in Chomsky normal form. Note: what about CFL that contains ε ? Why a normal form ?

  10. [Section 2.1] Chomsky Normal Form Thm 2.9: Any CFL can be generated by a CFG in Chomsky normal form. “Proof” by example: S -> ASA | aB Things to fix: 1. A -> B | S 2. B -> b | ε 3. 4.

More Related