Presentation is loading. Please wait.

Presentation is loading. Please wait.

Context-Free Grammars

Similar presentations


Presentation on theme: "Context-Free Grammars"— Presentation transcript:

1 Context-Free Grammars
CSC 4170 Theory of Computation Context-Free Grammars Section 2.1

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

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

4 N  men | women | children V  like | hate | respect
A more complex CFG 2.1.c1 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 N  men | women | children V  like | hate | respect
A more complex CFG 2.1.c2 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 A context-free grammar is a 4-tuple (V,,R,S), where
Formal definitions 2.1.d A context-free grammar is a 4-tuple (V,,R,S), where 1. V is a finite set called the variables; 2.  is a finite set, disjoint from V, called the terminals; 3. R is a finite set of rules, with each rule being a pair of a variable and a string of variables and terminals; 4. S is an element of V called the start 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. The language produced (defined, described) by the grammar is {w | S * w and w is a string of (only) terminals}. A context-free language is a language produced by some CFG.

7 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 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 is ambiguous iff it has two different parse trees for the same string

9 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 A more complex unambiguous grammar
2.1.h <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 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 Converting a DFA into a CFG
2.1.j 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 011001 Testing in work Q1  0 Q1 Q1  1 Q2 Q2  0 Q2 Q2  1 Q1 Q2  
2.1.j 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


Download ppt "Context-Free Grammars"

Similar presentations


Ads by Google