Context-Free Languages - PowerPoint PPT Presentation

1 / 76
About This Presentation
Title:

Context-Free Languages

Description:

Title: Languages and Finite Automata Author: Costas Busch Last modified by: ENCS Created Date: 8/31/2000 1:12:33 AM Document presentation format: On-screen Show – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 77
Provided by: Cost52
Learn more at: http://web.cs.wpi.edu
Category:

less

Transcript and Presenter's Notes

Title: Context-Free Languages


1
Context-Free Languages

2

Regular Languages
3

Context-Free Languages
Regular Languages
4
Context-Free Languages
Context-Free Grammars
Pushdown Automata
stack
automaton
5
Context-Free Grammars

6
Example

A context-free grammar
A derivation
7

A context-free grammar
Another derivation
8
(((( ))))
Describes parentheses
9
Example
A context-free grammar
A derivation
10
A context-free grammar
Another derivation
11
(No Transcript)
12
Example
A context-free grammar
A derivation
13
A context-free grammar
A derivation
14
Describes matched parentheses
() ((( ))) (( ))
15
Definition Context-Free Grammars
Grammar
Variables
Terminal symbols
Start variable
Productions of the form
String of variables and terminals
Variable
16
(No Transcript)
17
Definition Context-Free Languages
  • A language is context-free
  • if and only if
  • there is a context-free grammar
  • with

18
Derivation Order

19
(No Transcript)
20
Derivation Trees

21

22
(No Transcript)
23
(No Transcript)
24
(No Transcript)
25
Derivation Tree
26
Derivation Tree
yield
27
Partial Derivation Trees

Partial derivation tree
28

Partial derivation tree
29
sentential form

Partial derivation tree
yield
30
Sometimes, derivation order doesnt matter
Leftmost

Rightmost
Same derivation tree
31
So far, we concentrated on generative aspect of
grammars.How about analytical aspect?Parsing..

32
Parser
input string
derivation
grammar
33
Example
Parser
derivation
input
?
34
Exhaustive Search
Phase 1
Find derivation of
All possible derivations of length 1
35
(No Transcript)
36
Phase 2
Phase 1
37
Phase 2
Phase 3
38
Final result of exhaustive search
(top-down parsing)
Parser
input
derivation
39
Time complexity of exhaustive search
Suppose there are no productions of the form
Number of phases for string is Why?
40
For grammar with rules
Time for phase 1
possible derivations
41
Time for phase 2
possible derivations
42
Time for phase
possible derivations which is exponential in the
length of w
43
Total time needed for parsing
phase 1
phase 2w
phase 2
Extremely bad!!!
44
There exist faster algorithms for specialized
grammars
S-grammar
symbol
string of variables
appears once
Pair
45
S-grammar example
Each string has a unique derivation
46
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
47
For general context-free grammars
There exists a parsing algorithm that parses a
string in time
(we will show it in the next class)
48
Ambiguity

49
leftmost derivation
50
leftmost derivation
51
(No Transcript)
52
The grammar
is ambiguous
since there is a string, namely
, which has two derivation trees
53
The grammar
is ambiguous
since string
has two leftmost derivations
54
Definition
A context-free grammar is ambiguous if
some string has two or more
distinct derivation trees
55
Alternatively we may say
Ambiguity of a grammar implies the existence
of two or more leftmost (or rightmost)
derivations
56
Why do we care about ambiguity?
take
57
(No Transcript)
58
(No Transcript)
59
Correct result
60
  • Ambiguity is bad for programming languages
  • We want to remove ambiguity

61
We may be able to fix the ambiguity
New non-ambiguous grammar
62
(No Transcript)
63
Unique derivation tree
64
The grammar
is non-ambiguous
Every string has a unique
derivation tree
65
Another Ambiguous Grammar
IF_STMT
if EXPR then STMT
if EXPR then STMT else STMT
66
if expr1 then if expr2 then stmt1 else stmt2
IF_STMT
if
expr1
then
STMT
else
if
expr2
then
stmt1
stmt2
IF_STMT
if
expr1
then
else
STMT
stmt2
if
expr2
then
stmt1
67
Inherent Ambiguity
  • Some context-free languages
  • have only ambiguous grammars

Example
68
The string
has two derivation trees
69
Compilers

70
Machine Code
Program
Add v,v,0 cmp v,5 jmplt ELSE THEN add x,
12,v ELSE WHILE cmp x,3 ...
v 5 if (vgt5) x 12 v while (x !3)
x x - 3 v 10 ......
Compiler
71
Compiler
Lexical analyzer
parser
input
output
machine code
program
72
A parser knows the grammar of the programming
language
73
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
74
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
75
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
76
derivation tree
E
machine code

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

2
5
Write a Comment
User Comments (0)
About PowerShow.com