FL 3

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

Chapter 3

Context Free Languages


3.1 Context Free Grammars
3.2 Parsing and Ambiguity
Formal Definition of a CFG
• There is a finite set of symbols that form the strings,
i.e. there is a finite alphabet. The alphabet symbols
are called terminals.
• There is a finite set of variables, sometimes called
non-terminals or syntactic categories. Each variable
represents a language (i.e. a set of strings).
– In the palindrome example, the only variable is P.
• One of the variables is the start symbol. Other
variables may exist to help define the language.
• There is a finite set of productions or production
rules that represent the recursive definition of the
language. Each production is defined:
1. Has a single variable that is being defined to the left of the
production
2. Has the production symbol 
3. Has a string of zero or more terminals or variables, called the
body of the production. To form strings we can substitute
each variable’s production in for the body where it appears.
– V is the set of variables
– T is the set of terminals
– P is the set of production rules
– S is the start symbol.
• CFG drive their name from the fact that the substitution
of the variable on the left of the production can be made
any time such a variable appears in a sentential form. It
does not depend on the symbol in the rest of the
sentential form (the context). This feature is the
consequence of following only a single variable on the
left side of the production.
Context-Free Grammar: G  (V , T , S , P )

All productions in Pare of the form

A s
Variable String of
variables and
terminals

4
Example of Context-Free Grammar
S  aSb | 
productions
P  {S  aSb, S  }

G  V , T , S , P 

V  {S }
T  {a, b} start variable
variables
terminals
5
Another Example

Context-free grammar G:
S  aSa | bSb | 
Example derivations:
S  aSa  abSba  abba
S  aSa  abSba  abaSaba  abaaba

R
L(G )  {ww : w {a, b}*}
Palindromes of even length
9
Another Example
Context-free grammar G:
S  aSb | SS | 
Example derivations:
S  SS  aSbS  abS  ab
S  SS  aSbS  abS  abaSb  abab

L(G )  {w : na ( w)  nb ( w),
and na (v)  nb (v)
Describes
in any prefix v}
matched
parentheses: () ((( ))) (( )) a  (, b )
10
Derivation Order

Consider the following example grammar


with 5 productions:

1. S  AB 2. A  aaA 4. B  Bb
3. A   5. B  

11
1. S  AB 2. A  aaA 4. B  Bb
3. A   5. B  

eftmost derivation order of string aab


:

1 2 3 4 5
S  AB  aaAB  aaB  aaBb  aab

At each step, we substitute the


leftmost variable
12
1. S  AB 2. A  aaA 4. B  Bb
3. A   5. B  

ightmost derivation order of string aab


:

1 4 5 2 3
S  AB  ABb  Ab  aaAb  aab
At each step, we substitute the
rightmost variable
13
1. S  AB 2. A  aaA 4. B  Bb
3. A   5. B  

Leftmost derivation of aab:


1 2 3 4 5
S  AB  aaAB  aaB  aaBb  aab

Rightmost derivation of aab:


1 4 5 2 3
S  AB  ABb  Ab  aaAb  aab
14
Derivation Trees/ parse tree
Consider the same example grammar:
S  AB A  aaA |  B  Bb | 
aab:
And a derivation of
S  AB  aaAB  aaABb  aaBb  aab
Definition:
• A parse tree for a context-free grammar G = (V,P,R, S) is a

tree whose nodes are labeled by elements of V u and that
satisfies the following conditions.
• The root is labeled by the start symbol S.
• Each interior node is labeled by a non-terminal.

• Each leaf is labeled by a terminal symbol or by .

15
S  AB A  aaA |  B  Bb | 

S  AB
S

A B

yield AB

16
S  AB A  aaA |  B  Bb | 

S  AB  aaAB
S

A B

yield aaAB
a a A

17
S  AB A  aaA |  B  Bb | 

S  AB  aaAB  aaABb
S

A B

a a A B b

yield aaABb
18
S  AB A  aaA |  B  Bb | 
S  AB  aaAB  aaABb  aaBb
S

A B

a a A B b

yield
 aaBb  aaBb
19
S  AB A  aaA |  B  Bb | 
S  AB  aaAB  aaABb  aaBb  aab
Derivation Tree S
(parse tree)
A B

a a A B b
yield
  aab  aab
20
Sometimes, derivation order doesn’t matter
Leftmost derivation:
S  AB  aaAB  aaB  aaBb  aab
Rightmost derivation:
S  AB  ABb  Ab  aaAb  aab
S

Give same A B
derivation tree
a a A B b

  21
Ambiguity

22
Grammar for mathematical expressions

E  E  E | E  E | (E) | a

Example strings:
(a  a )  a  (a  a  (a  a ))

Denotes any number

23
E  E  E | E  E | (E) | a

E  E  E  a E  a EE
E
 a  a E  a  a*a
E  E
A leftmost derivation
for a  a  a
a E  E

a a
24
E  E  E | E  E | (E) | a

E  EE  E  EE  a EE


E
 a  aE  a  aa
E  E
Another
leftmost derivation
for a  a  a E  E a

a a
25
E  E  E | E  E | (E) | a

Two derivation trees


for a  a  a
E E

E  E E  E

a E  E E  E a

a a a a
26
take a2

a  a a  2  22
E E

E  E E  E

2 E  E E  E 2

2 2 2 2
27
Good Tree Bad Tree
2  22  6 2  22  8
6 Compute expression result 8
E using the tree E
2 4 4 2
E  E E  E
2 2 2 2
2 E  E E  E 2

2 2 2 2
28
Ambiguous Grammar:
A context-free grammar G is ambiguous
if there is a string w L(G )which has:

two different derivation trees


or
two leftmost derivations

(Two different derivation trees give two


different leftmost derivations and vice-versa)
29
Example: E  E  E | E  E | (E) | a

this grammar is ambiguous since


string a  a  a has two derivation trees
E E

E  E E  E

a E  E E  E a

a a a a
30
E  E  E | E  E | (E) | a
this grammar is ambiguous also because
string a  a  a has two leftmost derivations
E  E  E  a E  a EE
 a  a E  a  a*a

E  EE  E  EE  a EE


 a  aE  a  aa
31
In general, ambiguity is bad
and we want to remove it

Sometimes it is possible to find


a non-ambiguous grammar for a language

But, in general ιt is difficult to achieve this

32

You might also like