Formal Languages - PowerPoint PPT Presentation

1 / 93
About This Presentation
Title:

Formal Languages

Description:

(a single variable in both sides) Removing Unit Productions ... (exercise 7.1.3, Hopcroft, Motwani, Ullman) Greibach Normal Form. All productions have form: ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 94
Provided by: imsUnist
Category:

less

Transcript and Presenter's Notes

Title: Formal Languages


1
Formal Languages Simplifications of Context-Free
Languages Slides based on RPI CSCI 2400 Thanks to
Petros Drineas
2
A Substitution Rule
equivalent grammar
Substitute
3
A Substitution Rule
Substitute
equivalent grammar
4
In general
Substitute
equivalent grammar
5
Nullable Variables
Nullable Variable
6
Removing Nullable Variables
example Grammar
Nullable variable
7
Final Grammar
Substitute
8
Unit-Productions
Unit Production
(a single variable in both sides)
9
Removing Unit Productions
Observation
Is removed immediately
10
example Grammar
11
Substitute
12
Remove
13
Substitute
14
Remove repeated productions
Final grammar
15
Useless Productions
16
Another grammar
Not reachable from S
17
contains only terminals
In general
if
then variable is useful
otherwise, variable is useless
18
A production is useless if any of
its variables is useless
19
Removing Useless Productions
example Grammar
20
First
find all variables that can produce strings with
only terminals
Round 1
Round 2
21
Keep only the variables that produce terminal
symbols
(other variables are useless)
Remove useless productions
22
Second
Find all variables reachable from
Use a Dependency Graph
not reachable
23
Keep only the variables reachable from S
(other variables are useless)
Final Grammar
Remove useless productions
24
Removing All
  • Step 1 Remove Nullable Variables
  • Step 2 Remove Unit-Productions
  • Step 3 Remove Useless Variables

25
Normal FormsforContext-free Grammars

26
Chomsky Normal Form
each productions has form
or
variable
variable
terminal
27
examples
Chomsky Normal Form
Not Chomsky Normal Form
28
Conversion to Chomsky Normal Form
  • example

Not Chomsky Normal Form
29
Introduce variables for terminals
30
Introduce intermediate variable

31
Introduce intermediate variable
32
Final grammar in Chomsky Normal Form
Initial grammar
33
In general
From any context-free grammar (which doesnt
produce ) not in Chomsky Normal Form
we can obtain An equivalent grammar
in Chomsky Normal Form
34
The Procedure
First remove Nullable variables Unit
productions
35
Then, for every symbol
Add production
In productions replace with
New variable
36
Replace any production
with
New intermediate variables
37
Theorem
For any context-free grammar (which doesnt
produce ) there is an equivalent grammar in
Chomsky Normal Form
38
Observations
  • Chomsky normal forms are good
  • for parsing and proving theorems
  • It is very easy to find the Chomsky normal
  • form for any context-free grammar

39
exercise
Find CNF for this grammar S -gt 0A0 1B1 BB A
-gt C B -gt S A C -gt S epsilon (exercise
7.1.3, Hopcroft, Motwani, Ullman)
40
Greibach Normal Form
All productions have form
symbol
variables
41
examples
Greibach Normal Form
Not Greibach Normal Form
42
Conversion to Greibach Normal Form
Greibach Normal Form
43
Theorem
For any context-free grammar (which doesnt
produce ) there is an equivalent grammar in
Greibach Normal Form
44
Observations
  • Greibach normal forms are very good
  • for parsing
  • For many context-free grammars,
  • it's hard to find the Greibach normal form.

45
  • Why?
  • Try to find Greibach normal form
  • for grammar in CNF exercise ...

46
Compilers

47
Machine Code
Program
Add v,v,1 cmp v,5 jmplt eLSe THeN add x,
12,v eLSe WHILe cmp x,3 ...
v 1 if (vgt5) x 12 v while (x !3)
x x - 3 v 10 ......
Compiler
48
Compiler
Lexical analyzer
parser
input
output
machine code
program
49
A parser knows the grammar of the programming
language
50
Parser
PROGRAM STMT_LIST STMT_LIST STMT
STMT_LIST STMT STMT eXPR IF_STMT
WHILe_STMT
STMT_LIST eXPR eXPR eXPR eXPR - eXPR
ID IF_STMT if (eXPR) then STMT
if (eXPR) then STMT else STMT WHILe_STMT
while (eXPR) do STMT
51
The parser finds the derivation of a particular
input
derivation
Parser
input
e gt e e gt e e e gt 10 ee gt
10 2 e gt 10 2 5
e -gt e e e e INT
10 2 5
52
derivation tree
derivation
e
e gt e e gt e e e gt 10 ee gt
10 2 e gt 10 2 5

e
e
10
e
e

2
5
53
derivation tree
e
machine code

e
e
mult a, 2, 5 add b, 10, a
10
e
e

2
5
54
Parsing

55
Parser
input string
derivation
grammar
56
example
Parser
derivation
input
?
57
exhaustive Search
Phase 1
Find derivation of
All possible derivations of length 1
58
(No Transcript)
59
Phase 2
Phase 1
60
Phase 2
Phase 3
61
Final result of exhaustive search
(top-down parsing)
Parser
input
derivation
62
Time complexity of exhaustive search
Suppose there are no productions of the form
Number of phases for string
63
For grammar with rules
Time for phase 1
possible derivations
64
Time for phase 2
possible derivations
65
Time for phase
possible derivations
66
Total time needed for string
phase 1
phase 2w
phase 2
extremely bad!!!
67
There exist faster algorithms for specialized
grammars
S-grammar
symbol
string of variables
appears once
Pair
68
S-grammar example
each string has a unique derivation
69
For S-grammars
In the exhaustive search parsing there is only
one choice in each phase
Time for a phase
Total time for parsing string
70
For general context-free grammars
There exists a parsing algorithm that parses a
string in time
(we will show it in the next class)
71
The CYK Parser
72
The CYK Membership Algorithm
Input
  • Grammar in Chomsky Normal Form
  • String

Output
find if
73
The Algorithm
Input example
  • Grammar
  • String

74
(No Transcript)
75
(No Transcript)
76
(No Transcript)
77
(No Transcript)
78
Therefore
Time Complexity
Observation
The CYK algorithm can be easily converted to a
parser (bottom up parser)
79
The following slides are courtesy of Professor
Papp, University of Debrecen.
80
1,7


1,1
7,7
a a b b a b a
81
S
S B,C
S A,D S D
S,B C B S
S A,S D S
D A A B,C B,C A B,C
A
a a b b a b a
82
S
S B,C
S A,D S D
S,B C B S
S A,S D S
D A A B,C B,C A B,C
A
a a b b a b a
83
S
S B,C
S A,D S D
S,B C B S
S A,S D S
D A A B,C B,C A B,C
A
a a b b a b a
84
S
S B,C
S A,D S D
S,B C B S
S A,S D S
D A A B,C B,C A B,C
A
a a b b a b a
85
S
S B,C
S A,D S D
S,B C B S
S A,S D S
D A A B,C B,C A B,C
A
a a b b a b a
86
S
S B,C
S A,D S D
S,B C B S
S A,S D S
D A A B,C B,C A B,C
A
a a b b a b a
87
S
S B,C
S A,D S D
S,B C B S
S A,S D S
D A A B,C B,C A B,C
A
a a b b a b a
88
S
S B,C
S A,D S D
S,B C B S
S A,S D S
D A A B,C B,C A B,C
A
a a b b a b a
89
S
S B,C
S A,D S D
S,B C B S
S A,S D S
D A A B,C B,C A B,C
A
a a b b a b a
90
S
S B,C
S A,D S D
S,B C B S
S A,S D S
D A A B,C B,C A B,C
A
a a b b a b a
91
S
S B,C
S A,D S D
S,B C B S
S A,S D S
D A A B,C B,C A B,C
A
a a b b a b a
92
Word is in language if S appears here
S
S B,C
S A,D S D
S,B C B S
S A,S D S
D A A B,C B,C A B,C
A
a a b b a b a
93
exercise
Parse baaba for this grammar S -gt AB BC A -gt
BA a B -gt CC b C -gt AB a (Hopcroft,
Motwani, Ullman, p301)
Write a Comment
User Comments (0)
About PowerShow.com