context-free grammars
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Context-Free Grammars](https://reader036.vdocuments.us/reader036/viewer/2022062809/568159df550346895dc72c29/html5/thumbnails/1.jpg)
Context-Free GrammarsContext-Free Grammars
Section 2.1
CSC 4170
Theory of Computation
![Page 2: Context-Free Grammars](https://reader036.vdocuments.us/reader036/viewer/2022062809/568159df550346895dc72c29/html5/thumbnails/2.jpg)
What is a CFG2.1.a
A BA B 0A1
Terminals: 0,1
Variables: A,B
Start variable: A
Productions:
Derivation: A B 0A1 0B1 00A11 0011
Parse tree:
A
B
A
B
A
0 1
0 1
![Page 3: Context-Free Grammars](https://reader036.vdocuments.us/reader036/viewer/2022062809/568159df550346895dc72c29/html5/thumbnails/3.jpg)
Our grammar simplified2.1.b
A 0A1 A
A 0A1 00A11 000A111 0000A1111 00001111
What language does this grammar produce?
![Page 4: Context-Free Grammars](https://reader036.vdocuments.us/reader036/viewer/2022062809/568159df550346895dc72c29/html5/thumbnails/4.jpg)
A more complex CFG2.1.c1
S N’_V_N’ N’ N | N_who_V_N’N men | women | childrenV like | hate | respect
SN’_V_N’N’_like_N’N_like_N’N_like_Nwomen_like_Nwomen_like_children
S
N’ _ V _ N’
N like N
women children
![Page 5: Context-Free Grammars](https://reader036.vdocuments.us/reader036/viewer/2022062809/568159df550346895dc72c29/html5/thumbnails/5.jpg)
A more complex CFG2.1.c2
S N’_V_N’ N’ N | N_who_V_N’N men | women | childrenV like | hate | respect
S
N’ _ V _ N’
N respect N _ who _ V _ N’
children men hate N
women
![Page 6: Context-Free Grammars](https://reader036.vdocuments.us/reader036/viewer/2022062809/568159df550346895dc72c29/html5/thumbnails/6.jpg)
Formal definitions2.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.
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.
x * y means that x=y, or x y, or there are z1,…,zn such that x z1 … zn y.
![Page 7: Context-Free Grammars](https://reader036.vdocuments.us/reader036/viewer/2022062809/568159df550346895dc72c29/html5/thumbnails/7.jpg)
Ambiguity: An informal example 2.1.e
the girl touches the boy with the flower
the girl touches
the boy with the flower
with the flower
the girl touches the boy
Does this mean
the girl touches (the boy with the flower)
or
(the girl touches the boy) with the flower
?
![Page 8: Context-Free Grammars](https://reader036.vdocuments.us/reader036/viewer/2022062809/568159df550346895dc72c29/html5/thumbnails/8.jpg)
An example of an ambiguous CFG 2.1.f
<EXPR> <EXPR> + <EXPR> | <EXPR> <EXPR> | a
<EXPR>
<EXPR> + <EXPR>
a <EXPR> <EXPR>
a a
<EXPR>
<EXPR> <EXPR>
<EXPR> + <EXPR>
a a
a
a + a a
A grammar is ambiguous iff it has two different parse trees for the same string
![Page 9: Context-Free Grammars](https://reader036.vdocuments.us/reader036/viewer/2022062809/568159df550346895dc72c29/html5/thumbnails/9.jpg)
An equivalent but unambiguous grammar2.1.g
<EXPR> <EXPR> + <TERM> | <TERM><TERM> <TERM> a | a
<EXPR>
<EXPR> + <TERM>
<TERM>
a
<TERM> a
a
a + a a
![Page 10: Context-Free Grammars](https://reader036.vdocuments.us/reader036/viewer/2022062809/568159df550346895dc72c29/html5/thumbnails/10.jpg)
A more complex unambiguous grammar2.1.h
<EXPR> <EXPR> + <TERM> | <TERM><TERM> <TERM> <FACTOR> | <FACTOR><FACTOR> (<EXPR>) | a
<EXPR>
<EXPR> + <TERM>
<TERM>
<FACTOR>
a
<TERM> <FACTOR>
<FACTOR>
a
a
<EXPR>
<TERM>
<TERM> <FACTOR>
<FACTOR>
( <EXPR> )
<EXPR> + <TERM>
<TERM>
<FACTOR>
a
<FACTOR>
a
a
a + a a (a + a) a
![Page 11: Context-Free Grammars](https://reader036.vdocuments.us/reader036/viewer/2022062809/568159df550346895dc72c29/html5/thumbnails/11.jpg)
Designing context-free grammars2.1.i
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
![Page 12: Context-Free Grammars](https://reader036.vdocuments.us/reader036/viewer/2022062809/568159df550346895dc72c29/html5/thumbnails/12.jpg)
Converting a DFA into a CFG2.1.j
Q1
0
Q2
0
1
1
Variables: The states of the DFA
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.
![Page 13: Context-Free Grammars](https://reader036.vdocuments.us/reader036/viewer/2022062809/568159df550346895dc72c29/html5/thumbnails/13.jpg)
Testing in work2.1.j
Q1
0
Q2
0
1
1
Q1 0 Q1
Q1 1 Q2
Q2 0 Q2
Q2 1 Q1
Q2
011001Q1
0Q1
01Q2
011Q1
0110Q1
01100Q1
011001Q2
011001