Context-Free Languages - PowerPoint PPT Presentation

About This Presentation
Title:

Context-Free Languages

Description:

... cat runs': 10. Language of the grammar: L = { 'a cat runs', 'a cat ... two or more derivation trees. 61. In other words: A context-free grammar is ambiguous ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 75
Provided by: costas
Learn more at: http://www.cs.rpi.edu
Category:
Tags: cat | context | free | languages | trees

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
Grammars
  • Grammars express languages
  • Example the English language

7

8
  • A derivation of the dog walks

9
  • A derivation of a cat runs

10
  • Language of the grammar

L a cat runs, a cat walks,
the cat runs, the cat walks,
a dog runs, a dog walks, the
dog runs, the dog walks
11
Notation

Production Rules
Variable
Terminal
12
Another Example
  • Grammar
  • Derivation of sentence

13
  • Grammar
  • Derivation of sentence

14
  • Other derivations

15
  • Language of the grammar

16
More Notation
  • Grammar

Set of variables
Set of terminal symbols
Start variable
Set of Production rules
17
Example
  • Grammar

18
More Notation
  • Sentential Form
  • A sentence that contains
  • variables and terminals
  • Example

Sentential Forms
sentence
19
  • We write
  • Instead of

20
  • In general we write
  • If

21
  • By default

22
Example

Grammar
Derivations
23
Example
Grammar
Derivations
24
Another Grammar Example
  • Grammar

Derivations
25
More Derivations

26
Language of a Grammar
  • For a grammar
  • with start variable

String of terminals
27
Example
  • For grammar

Since
28
A Convenient Notation

29
Example

A context-free grammar
A derivation
30

A context-free grammar
Another derivation
31
(((( ))))
Describes parentheses
32
Example
A context-free grammar
A derivation
33
A context-free grammar
Another derivation
34
(No Transcript)
35
Example
A context-free grammar
A derivation
36
A context-free grammar
A derivation
37
Describes matched parentheses
() ((( ))) (( ))
38
Definition Context-Free Grammars
Grammar
Variables
Terminal symbols
Start variable
Productions of the form
String of variables and terminals
Variable
39
(No Transcript)
40
Definition Context-Free Languages
  • A language is context-free
  • if and only if
  • there is a context-free grammar
  • with

41
Derivation Order

42
(No Transcript)
43
Derivation Trees

44

45
(No Transcript)
46
(No Transcript)
47
(No Transcript)
48
Derivation Tree
49
Derivation Tree
yield
50
Partial Derivation Trees

Partial derivation tree
51

Partial derivation tree
52
sentential form

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

Rightmost
Same derivation tree
54
Ambiguity

55
leftmost derivation
56
leftmost derivation
57
(No Transcript)
58
The grammar
is ambiguous
string
has two derivation trees
59
The grammar
is ambiguous
string
has two leftmost derivations
60
Definition
A context-free grammar is ambiguous if
some string has two or
more derivation trees
61
In other words
A context-free grammar is ambiguous if
some string has two or
more leftmost derivations
(or rightmost)
62
Why do we care about ambiguity?
take
63
(No Transcript)
64
(No Transcript)
65
Correct result
66
  • Ambiguity is bad for programming languages
  • We want to remove ambiguity

67
We fix the ambiguous grammar
New non-ambiguous grammar
68
(No Transcript)
69
Unique derivation tree
70
The grammar
is non-ambiguous
Every string has a unique
derivation tree
71
Another Ambiguous Grammar
IF_STMT
if EXPR then STMT
if EXPR then STMT else STMT
72
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
73
Inherent Ambiguity
  • Some context free languages
  • have only ambiguous grammars

Example
74
The string
has two derivation trees
Write a Comment
User Comments (0)
About PowerShow.com