Formal Languages - PowerPoint PPT Presentation

1 / 76
About This Presentation
Title:

Formal Languages

Description:

has two leftmost derivations. The grammar. is ambiguous: Definition: ... two or more leftmost derivations (or rightmost) Why do we care about ambiguity? take ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 77
Provided by: imsUnist
Category:

less

Transcript and Presenter's Notes

Title: Formal Languages


1
Formal Languages Context-Free Languages Slides
based on RPI CSCI 2400 Thanks to Petros Drineas
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
Language?
34
A context-free grammar
Another derivation
35
(No Transcript)
36
Example
A context-free grammar
A derivation
37
A context-free grammar
A derivation
38
Describes matched parentheses
() ((( ))) (( ))
39
Definition Context-Free Grammars
Grammar
Variables
Terminal symbols
Start variable
Productions of the form
String of variables and terminals
Variable
40
(No Transcript)
41
Definition Context-Free Languages
  • A language is context-free
  • if and only if
  • there is a context-free grammar
  • with

42
Derivation Order

43
(No Transcript)
44
Derivation Trees

45

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

Partial derivation tree
52

Partial derivation tree
53
sentential form

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

Rightmost
Same derivation tree
55
Ambiguity

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

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

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