- 50 Views
- Uploaded on
- Presentation posted in: General

Context-Free Grammars

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

CSC 4170

Theory of Computation

Section 2.1

2.1.a

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

2.1.b

A 0A1

A

0A1

00A11

000A111

0000A1111

00001111

A

What language does this grammar produce?

2.1.c1

S N’_V_N’

N’ N |N_who_V_N’

N men | women | children

V 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

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

2.1.d

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.

2.1.e

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

2.1.f

<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

2.1.g

<EXPR> <EXPR> + <TERM> | <TERM>

<TERM> <TERM> a | a

<EXPR>

<EXPR> + <TERM>

<TERM>

<TERM> a

a

a

a + a a

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

2.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

2.1.j

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.

2.1.j

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