# CSI 3104 Winter 2006 Introduction to Formal Languages Chapter - Download as PowerPoint

Document Sample

```					CSI 3104 /Winter 2006: Introduction to Formal Languages
Chapter 12: Context-Free Grammars

Chapter 12: Context-Free Grammar
I. Theory of Automata
 II. Theory of Formal Languages
III. Theory of Turing Machines …

Zaguia/Stojmenovic               1
Chapter 12: Context-Free Grammars

   programming languages
   compiling a program: an operation that
generates an equivalent program in
machine or assembler language.
   2 phases:
1.   parsing 
2.   translation to machine language

Zaguia/Stojmenovic    2
Chapter 12: Context-Free Grammars

Example: AE (Arithmetic Expressions)
 Rule 1: Any number is in AE

 Rule 2: If x and y are in AE, then so are:

(x) – (x) (x+y) (x–y) (x*y)

A different way for defining the set AE is to use a set
of substitutions rules similar to the grammatical
rules:

Zaguia/Stojmenovic            3
Chapter 12: Context-Free Grammars

Substitution rules that define the AE’s:
S  AE
AE  (AE + AE)
AE  (AE–AE)
AE  (AE*AE)
AE  (AE)
AE  –(AE)
AE  NUMBER

NUMBERS??
NUMBER  FIRST-DIGIT
FIRST-DIGIT  FIRST-DIGIT OTHER-DIGIT
FIRST-DIGIT  1 2 3 4 5 6 7 8 9
OTHER-DIGIT  0 1 2 3 4 5 6 7 8 9

Zaguia/Stojmenovic   4
Chapter 12: Context-Free Grammars

S  AE  (AE*AE)  ((AE+AE)*AE)  ((AE+AE)*(AE+AE))
…  ((3+4)*(6+7))                         AE:
S  AE
AE  (AE + AE)
How to generate the number 1066?                           AE  (AE–AE)
NUMBER  FIRST-DIGIT
 FIRST-DIGIT OTHER-DIGIT                           AE  (AE*AE)
 FIRST-DIGIT OTHER-DIGIT OTHER-DIGIT               AE  (AE)
 FIRST-DIGIT OTHER-DIGIT OTHER-DIGIT OTHER-DIGIT   AE  –(AE)
1066
AE  NUMBER

NUMBERS
NUMBER         FIRST-DIGIT
FIRST-DIGIT    FIRST-DIGIT OTHER-DIGIT
FIRST-DIGIT    123456789
OTHER-DIGIT   0123456789

Zaguia/Stojmenovic                     5
Chapter 12: Context-Free Grammars

Definition: A context free grammar (CFG) is:
1.  an alphabet S of letters, called terminals.
2.  a set of symbols, called nonterminals or variables.
One symbol S is called the start symbol.
3.  a finite set of productions of the form:
Aa
where A is a nonterminal and a is a finite
sequence (word) of nonterminals and terminals.

Zaguia/Stojmenovic           6
Chapter 12: Context-Free Grammars

EA:
   Examples:                                 S  AE
AE  (AE + AE)
Terminals: (, ), +, -, *, numbers               AE  (AE–AE)
Nonterminals: S, AE                             AE  (AE*AE)
AE  (AE)
AE  –(AE)
NUMBERS                                          AE  NUMBER
NUMBER           FIRST-DIGIT
FIRST-DIGIT      FIRST-DIGIT OTHER-DIGIT
FIRST-DIGIT      123456789
OTHER-DIGIT     0123456789

Terminals : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Nonterminals : S, FIRST-DIGIT, OTHER-DIGIT
Zaguia/Stojmenovic                    7
Chapter 12: Context-Free Grammars

Definition: A sequence of applications of productions starting
with the start symbol and ending in a sequence of terminals is
called a derivation.

Definition: The language generated by a CFG is the set of all
sequences of terminals produced by derivations. We also say
language defined by, language derived from, or language
produced by the CFG.

Definition: A language generated by a CFG is called a context-
free language.
Zaguia/Stojmenovic                    8
Chapter 12: Context-Free Grammars

Examples:      S  aS          SΛ
S  aS
 aaS           Generated Language:
 aaaS {Λ, a, aa, aaa, …} = language(a*).
 aaaaS
 aaaaaS
 aaaaaaS
 aaaaaaΛ = aaaaaa
Zaguia/Stojmenovic     9
Chapter 12: Context-Free Grammars

S  SS          Sa                SΛ
S  SS
 SSS
 SaS
Generated Language:
 SaSS
 ΛaSS       {Λ, a, aa, aaa, …} = language(a*).
 ΛaaS
 ΛaaΛ=aa
(An infinite number of derivations for the word aa.)

Zaguia/Stojmenovic           10
Chapter 12: Context-Free Grammars

In general: variables: Upper case letters
terminals: Lower case letters

The empty word:
is it a nonterminal?    L  ...
no
a terminal?       LaaL = aa
not exactly, because it is erased.

NL                 N can simply be deleted.

Zaguia/Stojmenovic      11
Chapter 12: Context-Free Grammars

   S  aS   S  bS  Sa     Sb
S  aS  abS  abbS  abba

   SX       SY            XΛ           Y  aY
Y bY     Ya            Yb

   S  aS   S  bS   Sa     Sb     SΛ
S  aS  abS  abbS  abbaS  abba

   S  aS     S  bS         SΛ

Zaguia/Stojmenovic             12
Chapter 12: Context-Free Grammars

S  XaaX X  aX X  bX X  Λ
S  XaaX  aXaaX  abXaaX  abXaabX  abaab
How many derivations are possible for the word baabaab?
S  XY
X  aX                                   S  XY
X  bX       Abbreviation:               X  aX | bX | a
Xa                                      Y  Ya | Yb | a
Y  Ya
Y Yb
Ya
Zaguia/Stojmenovic                     13
Chapter 12: Context-Free Grammars

S  SS | ES | SE | Λ | DSD
E  aa | bb
D  ab | ba

EVEN-EVEN=language([aa+bb+(ab + ba)(aa+bb)*(ab+ba)]*)

S  aSb | Λ
S  aSb  aaSbb  aaaSbbb  aaaaSbbbb
 aaaaaSbbbbb  aaaaabbbbb

S  aSa | bSb | Λ ?

Zaguia/Stojmenovic      14
Chapter 12: Context-Free Grammars

S  AA
A  AAA | bA | Ab | a                                  S
Parse Trees for the word bbaaaab
A        A
S
S
S                                                   b A    A A A
A        A
A       A
A        A                     b A                      b A a a         A b
A A A
b A   A A A
a             a
b A a a            A b
bbaaaab

Zaguia/Stojmenovic                             15
Chapter 12: Context-Free Grammars

Parse trees are also called syntax trees, generation trees,
production trees, or derivation trees.

Remark: In a parse tree every internal nodes is labelled
with a variable (nonterminal) and every leaf is labelled
with a terminal.

Zaguia/Stojmenovic            16
Chapter 12: Context-Free Grammars

Example:
S  (S+S) | (S*S) | NUMBER
NUMBER  …
S  (S+S)  (S+(S*S))  …  (3+(4*5))
S  (S*S)  ((S+S)*S)  …  ((3+4)*5)
S
S

S   * S
S +     S

S +     S   5
3   S *     S

3       4
4       5         Zaguia/Stojmenovic                    17
Chapter 12: Context-Free Grammars

S
S
S
                           
S +     S
3 +      S
3 + 20
   23
3   S *     S
4 *       5
4       5
S                 S                              S

S   *   S
       S   *   5
             7                  35
*       5

S +     S   5      3 +    4

3       4
Zaguia/Stojmenovic                            18
Chapter 12: Context-Free Grammars

Lukasiewicz (Prefix) Notation                      +3*45

S                        S                         S

S +     S                    +                         +

3       *                 3       *
3   S *     S

4       5                4        5                4       5

Zaguia/Stojmenovic                   19
Chapter 12: Context-Free Grammars

*+345

S                             S
S

*                             *
S   * S
+                            +         5
S +     S   5                         5

4                   3        4
3       4               3

Zaguia/Stojmenovic                     20
Chapter 12: Context-Free Grammars

S           *+*+12+3456          +12

*           *+* 3 +3456          +34

+                6   *+* 3     7 56       *37

*            5           *+    21    56       + 21 5

*      26    6       * 26 6
+                +

156
1       2       3            4
Zaguia/Stojmenovic            21
Chapter 12: Context-Free Grammars

S
Example: S  AB A  a                 Bb
S  AB  aB  ab                                A               B
S  AB  Ab  ab
● A CFG is ambiguous if there is at least one word a               b
in the language that has at least two derivation     S
trees. It is called unambiguous otherwise.
a S          a
● Example: S  aSa S  bSb
Sa Sb SL
a S          a
S  aSa  aaSaa  aabaa
b
Zaguia/Stojmenovic                         22
Chapter 12: Context-Free Grammars

Language(a+)
Example: S  aS | Sa | a
S      S            S             S

a S   a S             S   a         S    a
S

a S      S     a   a S              S    a
a S

a     a             a             a
a S
Example: S  aS | a
Zaguia/Stojmenovic
a   23
Chapter 12: Context-Free Grammars

Total Language Trees
S  aa | bX | aXX
X  ab | b        S

aa         bX              aXX

bab    bb        aabX    abX      aXab   aXb

aabab aabb        abab    abb aabab abab aabb abb

Zaguia/Stojmenovic                        24
Chapter 12: Context-Free Grammars

Total language tree is infinite
S  aSb | bS | a          S
since S can call itself recursively
aSb         bS              a

aaSbb     abSb         aab      baSb      bbS          ba

aaaSbbb   aabSbb   aaabb                                  aaaSbbb aaaSbbb   bba
.        .                                                 .        .
.        .                                                 .        .
Zaguia/Stojmenovic                           25
Chapter 12: Context-Free Grammars

S
SX|b                           X             b
X  aX                         aX

aaX

aaaX
.
.
Total language tree is infinite but language is finite

Zaguia/Stojmenovic             26

```
DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 19 posted: 2/16/2010 language: English pages: 26
How are you planning on using Docstoc?