ContextFree Languages - PowerPoint PPT Presentation

1 / 66
About This Presentation
Title:

ContextFree Languages

Description:

We want to remove ambiguity. Ambiguity is bad for programming languages. 63 ... Inherent Ambiguity. Some context free languages. have only ambiguous grammars. Example: ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 67
Provided by: costas
Category:

less

Transcript and Presenter's Notes

Title: ContextFree 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
Ambiguity

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

63
Another Ambiguous Grammar
IF_STMT
if EXPR then STMT
if EXPR then STMT else STMT
64
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
65
Inherent Ambiguity
  • Some context free languages
  • have only ambiguous grammars

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