Context free grammars
This presentation is the property of its rightful owner.
Sponsored Links
1 / 13

Context-Free Grammars PowerPoint PPT Presentation


  • 37 Views
  • Uploaded on
  • Presentation posted in: General

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.

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

Presentation Transcript


Context free grammars

CSC 4170

Theory of Computation

Context-Free Grammars

Section 2.1


What is a cfg

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


Our grammar simplified

2.1.b

Our grammar simplified

A  0A1

A  

 0A1

 00A11

 000A111

 0000A1111

 00001111

A

What language does this grammar produce?


A more complex cfg

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


A more complex cfg1

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


Formal definitions

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.


Ambiguity an informal example

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


An example of an ambiguous cfg

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


An equivalent but unambiguous grammar

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


A more complex unambiguous grammar

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


Designing context free grammars

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


Converting a dfa into a cfg

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.


Testing in work

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


  • Login