You are on page 1of 62

Introduction

CFG - CFL
SVNIT, CoED
B. Tech - II

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

THEORETICAL COMPUTER SCIENCE


(CS-I)

L-3 T-1 P-0 C-4

(Total Contact Time: 42 Hours + 14 Hours = 56 Hours)

Introduction (02 Hours) by MAZ

Basic Mathematical Objects: Sets, Logic, Functions,


Relations, Strings, Alphabets, Languages;
Mathematical Induction: Inductive proofs, Principles;
Recursive Definitions; Set Notation.

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

Finite Automata and Regular Expressions (12 Hours)


By MAZ
Finite State systems,
Regular Languages & Regular Expressions,
Deterministic Finite Automata;
Nondeterministic Finite Automata,
Kleene Theorem;
Two-way Finite Automata,
Finite Automata with output,
Properties of Regular Sets: The Pumping Lemma for
Regular sets,
Closure properties,
Decision properties of regular languages,
Equivalence and minimization of Automata.

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

Context Free Grammars (14 Hours) by me

Definition,
Derivation trees & Ambiguity, Inherent ambiguity, Parse
tree,
Application of CFG,
Simplification of CFG,
Normal form of CFG,
Chomsky Normal form and Chomsky Hierarchy,
Unrestricted grammars,
Context-sensitive languages,
Relations between classes of languages,
Properties of Context Free Languages: The Pumping
Lemma, Closure properties, Decision properties of CFL.

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

Pushdown Automata (04 Hours) By SRG

2/9/2015

Definitions,
Languages of PDA,
Equivalence of PDA and CFG ,
Deterministic PDA

SVNIT, CoED, B. Tech II, TCS.

Turing Machines- (06 Hours) By SRG

2/9/2015

Turing Machine Model,


Language of a Turing Machine,
Programming techniques of the TM,
Variations of TM (Multiple TM, One-tape and
Multi-tape TM etc),
Deterministic and Non deterministic TM,
Universal TM,
Churche thesis,
Recursively Enumerable Languages.
SVNIT, CoED, B. Tech II, TCS.

Computational Complexity (04 Hours) By SRG


Time and Space Complexity,
Growth Rate,
Complexity classes,
Tractable and Non tractable Problems: P and NP,
Cookss theorem.

Tutorials will be based on the coverage of the


above topics separately (14 Hours)

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

BOOKS RECOMMENDED

1). John E. Hopcroft, Rajeev Motwani, Jeffrey Ullman:


Introduction to Automata theory, languages computation , 3/E,
Pearson India, Last Indian, Reprint 2005.

2). John C Martin : Introduction to Languages & the Theory of


Computation, McGraw-Hill International ed, 2002

3). Daniel I A Cohen : Introduction to Computer Theory, John


Wiley & Sons, 1990 Ed, Reprint 1994

4). A.M. Natrajan, A.Tamilarasi: Theory of computation, New


Age Publication, 1/E, 2003.

5). Sushil Kumar Azad : Theory of Computation, An introduction


to /automata, Formal Languages And Computability, Dhanpat
Ray & Co., New Delhi, 2005.

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

Mathematical Terms - Basic

Sats : collection of things called elements or members

Eg. N= {1,2,3} is a set of natural numbers

Functions: f : A B

Function from set A to set B is binary relation f from A to B such


that for every aA exactly one bB

Relations:

Languages:

2/9/2015

A subset of strings over an alphabet is a language

SVNIT, CoED, B. Tech II, TCS.

Basic

An alphabet is finite non empty set of symbols

A string is a finite sequence of symbols from the


alphabets

is used for an alphabet


{0,1}, the binary alphabet
{A,B,..,Z}, the set of uppercase letters

10110 is string from binary alphabet


STAMP string from roman alphabet

No symbol empty string - or

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

10

Basic

Natural language like English

2/9/2015

Hard to state all the rules


To understand it requires intelligence and ability
May carry multiple meanings
May depend on the context in which it is spoken

SVNIT, CoED, B. Tech II, TCS.

11

Basic

Formal language based on the rules which are


explicitly stated

2/9/2015

List of symbols
Rules for forming a string (word)
Rules for forming a sentence

Basic building block of FL is alphabet


Rules for forming string from alphabet are defined
without any ambiguity

SVNIT, CoED, B. Tech II, TCS.

12

Basic

A language class larger than the class of regular


language
Supports natural, recursive notation
Applications

Parse trees compilers


XML
Regular Languages

a *b *
2/9/2015

SVNIT, CoED, B. Tech II, TCS.

( a b) *
13

Basic
Context-Free Languages
(CFG-PDA)

Regular Languages
FA/RE

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

14

Context-Free Languages

Context-Free
Grammars

Pushdown
Automata

stack

automaton

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

15

Context-Free Grammar

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

16

CFG

Why named context-free?

Property that we can substitute strings for variables regardless of


context

A language is represented using a set of equations


Equations are recursive in nature
CFG has set of variables, set of terminals and set of
productions.

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

17

More Applications

Parenthesis matching in code


Syntax checking
In scenarios where there is a general need for:

2/9/2015

Matching a symbol with another symbol, or


Matching a count of one symbol with that of another
symbol, or
Recursively substituting one symbol with a string of
other symbols

SVNIT, CoED, B. Tech II, TCS.

18

Example
A context-free grammar

S aSb
S

A derivation:

S aSb aaSbb aabb


2/9/2015

SVNIT, CoED, B. Tech II, TCS.

19

Example
A context-free grammar

S aSb
S

Another derivation:

S aSb aaSbb aaaSbbb aaabbb


2/9/2015

SVNIT, CoED, B. Tech II, TCS.

20

CFG Example

Language of palindromes

We can easily show using the pumping lemma that the


language L = { w | w = wR } is not regular.
However, we can describe this language by the
following context-free grammar over the alphabet
{0,1}:
A
A 0
A 1
A 0A0
A 1A1

2/9/2015

More compactly: A | 0 | 1 | 0A0 | 1A1


SVNIT, CoED, B. Tech II, TCS.

21

How does the CFG for


palindromes work ?

An input string belongs to the language (i.e., accepted)


iff it can be generated by the CFG
Example :w=01110
G can generate w as follows :
A | 0 | 1 | 0A0 | 1A1

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

22

Formal Definition of a CFG

There is a finite set of symbols that form the strings,

There is a finite set of variables,

i.e. there is a finite alphabet.


The alphabet symbols are called terminals

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 A

One of the variables is the start symbol. Other variables


may exist to help define the language

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

23

Formal Definition of a CFG

There is a finite set of productions or production rules


that represent the recursive definition of the language.
Each production

2/9/2015

Has a single variable that is being defined to the left of the


production
Has the production symbol
Has a string of zero or more terminals or variables, called the
body of the production
To form strings we can substitute each variables production in for
the body where it appears.

SVNIT, CoED, B. Tech II, TCS.

24

Definition: Context-Free Grammars


Grammar

Variables

G (V , T , S , P )
Terminal
symbols

Start
variable

Productions of the form:

A x
Variable

2/9/2015

String of variables
and terminals
SVNIT, CoED, B. Tech II, TCS.

25

CFG Notation

A CFG may then be represented by these four


components, denoted G=(V,T,P,S)

V : A finite set of variables or non-terminals


T : A finite set of terminals (V and T do not intersect)
P : A finite set of productions,
each of the form A > x, where A is in V and x is in (V U T)*
Note that x may be

S : A starting non-terminal (S is in V)
CFG for the language of binary palindromes:
G=({A},{0,1},P,A)
P: A ==> 0 A 0 | 1 A 1 | 0 | 1 |

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

26

Example
A context-free grammar

S aSa
S bSb
S
A derivation:

S aSa abSba abba


2/9/2015

SVNIT, CoED, B. Tech II, TCS.

27

Example
A context-free grammar

S aSa
S bSb
S

Another derivation:

S aSa abSba abaSaba abaaba


2/9/2015

SVNIT, CoED, B. Tech II, TCS.

28

Example

S aSa
S bSb
S

L(G ) {ww : w {a, b}*}


2/9/2015

SVNIT, CoED, B. Tech II, TCS.

29

Example
A context-free grammar

S aSb
S SS
S

A derivation:

S SS aSbS abS ab
2/9/2015

SVNIT, CoED, B. Tech II, TCS.

30

Example
A context-free grammar

S aSb
S SS
S
A derivation:

S SS aSbS abS abaSb abab


2/9/2015

SVNIT, CoED, B. Tech II, TCS.

31

Examples

Language of balanced parenthesis e.g.,


()(((())))((())).

G: S => (S) | SS |

A grammar for L = {0m 1n| mn}

G: S => 0S1 | A
A => 0A |

How would you interpret the string 00000111 using


this grammar?

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

32

Example CFG:
G = ({S}, {0, 1}, P, S)
P:
(1) S > 0S1
or just simply S > 0S1 |
(2) S >
Example Derivations:
S => 0S1
(1)
S =>
(2)
=> 01
(2)
S => 0S1
=> 00S11
=> 000S111
=> 000111

(1)
(1)
(1)
(2)

Note that G generates the language {0k1k | k>=0}

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

33

Example CFG:
G = ({A, B, C, S}, {a, b, c}, P, S)
P: (1) S > ABC
(2) A > aA
A > aA |
(3) A >
(4) B > bB
B > bB |
(5) B >
(6) C > cC
C > cC |
(7) C >
Example Derivations:
S => ABC
(1)
S => ABC
=> BC
(3)
=> aABC
=> C
(5)
=> aaABC
=>
(7)
=> aaBC
=> aabBC
=> aabC
=> aabcC
=> aabc
SVNIT, CoED, B. Tech II, TCS.
Note that G generates the
language a*b*c*

2/9/2015

(1)
(2)
(2)
(3)
(4)
(5)
(6)
(7)
34

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

35

Definition: Context-Free Grammars


Grammar

Variables

G (V , T , S , P )
Terminal
symbols

Start
variable

Productions of the form:

A x
Variable

2/9/2015

String of variables
and terminals
SVNIT, CoED, B. Tech II, TCS.

36

G (V , T , S , P )

L(G ) {w : S w, w T *}

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

37

Definition: Context-Free Languages

A language
if and only if

is context-free

there is a context-free grammar


with
L L(G )

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

38

Derivation Order
1. S AB

2. A aaA

4. B Bb

3. A

5. B

Leftmost derivation:

S AB aaAB aaB aaBb aab


Rightmost derivation:

S AB ABb Ab aaAb aab


2/9/2015

SVNIT, CoED, B. Tech II, TCS.

39

S aAB
A bBb
B A|
Leftmost derivation:

S aAB abBbB abAbB abbBbbB


abbbbB abbbb
Rightmost derivation:

S aAB aA abBb abAb


abbBbb abbbb
2/9/2015

SVNIT, CoED, B. Tech II, TCS.

40

Derivation Trees

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

41

A aaA |

S AB

B Bb |

S AB
S

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

42

A aaA |

S AB

B Bb |

S AB aaAB
S

2/9/2015

B
A

SVNIT, CoED, B. Tech II, TCS.

43

A aaA |

S AB

B Bb |

S AB aaAB aaABb
S

2/9/2015

B
A

SVNIT, CoED, B. Tech II, TCS.

44

A aaA |

S AB

B Bb |

S AB aaAB aaABb aaBb


S

2/9/2015

B
A

SVNIT, CoED, B. Tech II, TCS.

45

A aaA |

S AB

B Bb |

S AB aaAB aaABb aaBb aab


S

Derivation Tree

2/9/2015

B
A

SVNIT, CoED, B. Tech II, TCS.

46

A aaA |

S AB

B Bb |

S AB aaAB aaABb aaBb aab


S

Derivation Tree

B
A

yield

aab
aab

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

47

Partial Derivation Trees


S AB
A aaA |

B Bb |

S AB
S

Partial derivation tree

A
2/9/2015

SVNIT, CoED, B. Tech II, TCS.

B
48

S AB aaAB

Partial derivation tree

a
2/9/2015

B
A

SVNIT, CoED, B. Tech II, TCS.

49

S AB aaAB

Partial derivation tree

sentential
form

B
yield

a
2/9/2015

SVNIT, CoED, B. Tech II, TCS.

aaAB
50

Sometimes, derivation order doesnt matter


Leftmost:

S AB aaAB aaB aaBb aab


Rightmost:

S AB ABb Ab aaAb aab


S

Same derivation tree

a
2/9/2015

B
A

SVNIT, CoED, B. Tech II, TCS.

b
51

So far, we concentrated on generative


aspect of grammars.
How about analytical aspect?
Parsing..

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

52

Parser
input
string

2/9/2015

grammar

SVNIT, CoED, B. Tech II, TCS.

derivation

53

Example:

Parser

input

aabb

S SS
S aSb
S bSa

derivation

S
2/9/2015

SVNIT, CoED, B. Tech II, TCS.

54

Exhaustive Search

S SS | aSb | bSa |
Phase 1:

S SS

Find derivation of

S aSb
S bSa

aabb

S
All possible derivations of length 1

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

55

S SS

aabb

S aSb
S bSa
S

2/9/2015

SVNIT, CoED, B. Tech II, TCS.

56

Phase 2

Phase 1

S SS
S aSb

2/9/2015

S SS | aSb | bSa |

S SS SSS
S SS aSbS
S SS bSaS

aabb

S SS S
S aSb aSSb
S aSb aaSbb
S aSb abSab
S aSb ab
SVNIT, CoED, B. Tech II, TCS.

57

S SS | aSb | bSa |

Phase 2

S SS SSS
S SS aSbS
S SS S

aabb

S aSb aSSb
S aSb aaSbb
Phase 3

S aSb aaSbb aabb


2/9/2015

SVNIT, CoED, B. Tech II, TCS.

58

Final result of exhaustive search


(top-down parsing)
Parser

input

aabb

S SS
S aSb
S bSa
S
derivation

S aSb aaSbb aabb


2/9/2015

SVNIT, CoED, B. Tech II, TCS.

59

Time complexity of exhaustive search

Suppose there are no productions of the form

A
A B
Number of phases for string
Why?

2/9/2015

is

SVNIT, CoED, B. Tech II, TCS.

2| w|

60

For grammar with

Time for phase 1:

rules

2/9/2015

possible derivations

SVNIT, CoED, B. Tech II, TCS.

61

Time for phase 2:

2/9/2015

possible derivations

SVNIT, CoED, B. Tech II, TCS.

62

You might also like