CSC 3130: Automata theory and formal languages Tutorial 4

1 / 26
About This Presentation
Title:

CSC 3130: Automata theory and formal languages Tutorial 4

Description:

... CFG is more general than regular expression. NFA Regular Expression DFA. S 0S1 ... Adding 1 'a' and 1 'b', the difference between the numbers of 'a' and 'b' keep constant ... –

Number of Views:118
Avg rating:3.0/5.0
Slides: 27
Provided by: chh87
Category:

less

Transcript and Presenter's Notes

Title: CSC 3130: Automata theory and formal languages Tutorial 4


1
CSC 3130 Automata theory and formal
languagesTutorial 4
  • KN Hung
  • Office SHB 1026

Department of Computer Science Engineering
2
Agenda
  • Context Free Grammar (CFG)
  • Design
  • Parse Tree
  • Cocke-Younger-Kasami (CYK) algorithm
  • Parsing CFG in normal form
  • Pushdown Automata (PDA)
  • Design

3
Context-Free Grammar (Recap)
  • A context free grammar is consisted of

4) Start Variable
3) Production Rule
S ? AB ba A ? aA a B ? b
Another Production Rule
1) Variable
2) Terminal
4
Context-Free Grammar (Recap)
  • A string is said to belong to the language (of
    the CFG) if it can be derived from the start
    variable

Apply Production Rule
CFG Example
Derivation
S ? AB ba A ? aA a B ? b
  • AB
  • aAB
  • aaB
  • aab

S
Therefore, aab belongs to the language
5
Why CFG?
  • L w 0n1n n is an positive integer
  • L is not a regular language
  • Proved by Pumping Lemma
  • A Context-Free Grammar can describe it
  • Thus, CFG is more general than regular expression
  • NFA ? Regular Expression ? DFA

S ? 0S1 S ? 01
6
CFG Design
  • Given a context-free language, design the CFG
  • L ab-string, w Number of as lt Number of
    bs
  • Some time for you to get into think 1 min

S ? ?
7
CFG Design (Cont)
  • Trial Bottom-up
  • Shortest string in L b
  • Given a string in L, we can expand it, s.t. it is
    still in L
  • i.e., Add terminals, while not violating the
    constraints

8
CFG Design (Cont)
  • One Wrong Trial
  • S ? b
  • S ? bS Sb
  • S ? abS baS bSa aSb

However, cannot parse strings like aabbbbbaa
9
CFG Design (Cont)
  • Approach 1
  • S ? b
  • S ? SS
  • S ? SaS aSS SSa

But, is it sufficient to say the grammar is
correct?
10
CFG Design (Cont)
  • Approach 2
  • Start with the grammar for ab-strings with same
    number of as and bs
  • Call the start symbol of this grammar E
  • Now, we generate all strings of type
  • EbE EbEbE EbEbEbE
  • Thus, we have the grammar

11
CFG Design (Cont)
  • Approach 2 (Cont)
  • S ? EbET
  • T ? bET e
  • E ?

For the pattern EbE EbEbE
E generates ab-strings with same number of as
and bs (c.f. 09L7.pdf Slide 32)
12
CFG Design (Cont)
  • After designing the grammar, G, you may have to
    prove (if required) that the language of this
    grammar is equivalent to the given language
  • i.e., Prove that L(G) L
  • Proof
  • Part 1) L(G) ? L
  • Part 2) L ? L(G)
  • Due to time limit, I will not do this part

13
Parse Tree
  • How to parse aab in this grammar? (Previous
    example)

CFG Example
S ? AB ba A ? aA a B ? b
S
14
Parse Tree (Cont)
  • Idea Production Rule Node Children
  • Should be very intuitive to understand

Derivation
  • AB
  • aAB
  • aaB
  • aab

S
15
Parse Tree (Cont)
  • Ambiguity

3 - 1 - 2
String
CFG
3 1 2
3 (1 2)
16
Parse Tree (Cont)
  • Useful in programming language
  • CSC3180
  • Useful in compiler
  • CSC3120

17
Cocke-Younger-Kasami Algorithm
  • Used to parse context-free grammar in Chomsky
    normal form (or simply normal form)

18
CYK Algorithm - Idea
  • Algorithm 2 in Lecture Note (09L8.pdf)
  • Idea Bottom Up Parsing
  • Algorithm
  • Given a string s of length N
  • For k 1 to N
  • For every substring of length k
  • Determine what variable(s) can derive it
  • sub(x,y) starts at index x, ends at index y

19
CYK Algorithm - Init
  • Base Case k 1
  • The possible choices of variable(s) can be known
    by scanning through each production

B
A,C
A,C
B
A,C
a
b
a
b
a
We want to parse this string
20
CYK Algorithm Table
  • Each cell Variables deriving the substring

a
b
a
b
a
21
CYK Algorithm Loop (kgt1)
  • When k 2
  • Example
  • sub(1,2) ba
  • ba b a sub(1,1) sub(2,2)
  • Possible
  • BA BC
  • ? Variable A,S
  • Since A?BA, S?BC

S,A
B
A,C
A,C
B
A,C
a
b
a
b
a
22
CYK Algorithm Loop (kgt1)
  • For each substring
  • Decompose into two substrings
  • Example
  • sub(2,4) aab
  • Possible
  • AS, AC, CS, CC

, BB
a
b
a
b
a
23
CYK Algorithm Loop (kgt1)
  • How about sub(3,5) ?
  • Give you 1 min

S,A
B
S,C
S,A
B
A,C
A,C
B
A,C
a
b
a
b
a
24
CYK Algorithm Parse Tree
  • Parse Tree is known from the table
  • See 09L8.pdf - Slide 21

Length of Substring
a
b
a
b
a
Start Index of Substring
25
CYK Algorithm (Conclusion)
  • Start from shortest substring to the longest
  • i.e., from single-character-string to the
    whole string
  • For Context-free grammar, G
  • 1) Convert G into normal form
  • Remove e-productions
  • Remove unit-productions
  • 2) Apply CYK algorithm
  • Con Loss in intuition

26
End
  • Thanks for coming!
  • Any questions?
Write a Comment
User Comments (0)
About PowerShow.com