1 / 13

Context-Free Grammars

CSC 4170 Theory of Computation. Context-Free Grammars. Section 2.1. 2.1.a. What is a CFG. A  B A   B  0A1. Terminals: 0,1. Variables: A,B. Productions:. Start variable: A. A  B  0A1  0B1  00A11  0011. Derivation:. A. B. Parse tree:. 0 1. A. B.

phyre
Télécharger la présentation

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. CSC 4170 Theory of Computation Context-Free Grammars Section 2.1

  2. 2.1.a What is a CFG A  B A   B  0A1 Terminals: 0,1 Variables: A,B Productions: Start variable: A A  B  0A1  0B1  00A11  0011 Derivation: A B Parse tree: 0 1 A B 0 1 A 

  3. 2.1.b Our grammar simplified A  0A1 A    0A1  00A11  000A111  0000A1111  00001111 A What language does this grammar produce?

  4. 2.1.c1 A more complex CFG S  N’_V_N’ N’  N |N_who_V_N’ N  men | women | children V  like | hate | respect SN’_V_N’N’_like_N’N_like_N’N_like_Nwomen_like_Nwomen_like_children S N’ _ V _ N’ N like N women children

  5. 2.1.c2 A more complex CFG S  N’_V_N’ N’  N |N_who_V_N’ N  men | women | children V  like | hate | respect S N’ _ V _ N’ N respect N _ who _ V _ N’ children men hate N women

  6. 2.1.d Formal definitions Acontext-free grammaris a 4-tuple (V,,R,S), where 1. V is a finite set called thevariables; 2. is a finite set, disjoint from V, called theterminals; 3. R is a finite set ofrules, with each rule being a pair of a variable and a string of variables and terminals; 4. S is an element of V called thestart variable. If u,v, and w are strings of variables and terminals and A w is a rule, we say that uAv yields uwv, written uAv  uwv. x * y means that x=y, or x y, or there are z1,…,zn such that x z1 … zn y. Thelanguage produced(defined, described) by the grammar is {w | S * w and w is a string of (only) terminals}. Acontext-free languageis a language produced by some CFG.

  7. 2.1.e Ambiguity: An informal example the girl touches the boy with the flower Does this mean the girl touches (the boy with the flower) or the girl touches the boy with the flower (the girl touches the boy) with the flower ? with the flower the girl touches the boy

  8. 2.1.f An example of an ambiguous CFG <EXPR>  <EXPR> + <EXPR> | <EXPR>  <EXPR> | a a + a  a <EXPR> <EXPR> <EXPR> + <EXPR> <EXPR>  <EXPR> a <EXPR>  <EXPR> <EXPR> + <EXPR> a a a a a A grammar isambiguous iff it has two different parse trees for the same string

  9. 2.1.g An equivalent but unambiguous grammar <EXPR>  <EXPR> + <TERM> | <TERM> <TERM>  <TERM>  a | a <EXPR> <EXPR> + <TERM> <TERM> <TERM>  a a a a + a  a

  10. 2.1.h A more complex unambiguous grammar <EXPR>  <EXPR> + <TERM> | <TERM> <TERM>  <TERM>  <FACTOR> | <FACTOR> <FACTOR>  (<EXPR>) | a <EXPR> <TERM> <EXPR> <TERM>  <FACTOR> <EXPR> + <TERM> <FACTOR> a <TERM> <TERM>  <FACTOR> ( <EXPR> ) <FACTOR> <FACTOR> a <EXPR> + <TERM> <TERM> <FACTOR> a a <FACTOR> a a + a  a a (a + a)  a

  11. 2.1.i Designing context-free grammars Design a CFG that produces all regular expressions over the alphabet {0,1}: <RE>  Design a CFG G that produces the union of the languages produced by two given CFGs G1 and G2. G1: A1 w1 … An  wn G2: B1 u1 … Bm  um

  12. 2.1.j Converting a DFA into a CFG 0 0 Variables:The states of the DFA 1 Q1 Q2 1 Start variable:The start state of the DFA Productions: 1. Qi  a Qj, whenever there is an a-arrow from Qi to Qj; 2. Qi  , whenever Qi is an accept state.

  13. 2.1.j Testing in work 0 0 Q1  0 Q1 Q1  1 Q2 Q2  0 Q2 Q2  1 Q1 Q2   1 Q1 Q2 1 Q1 0Q1 01Q2 011Q1 0110Q1 01100Q1 011001Q2 011001 011001

More Related