Title: FABER F10
1 CDT314 FABER Formal Languages, Automata and
Models of Computation Lecture 5 Mälardalen
University 2010
2ContentMidterm solutionsRegular vs. Nonregular
Languages Context-Free LanguagesContext-Free
GrammarsDerivation Trees. AmbiguityPush-Down
Automata, PDAApplications
3A Nonregular Language
DFA must have infinite number of states.
States
are distinct for each
4Non-regular languages
Context-Free Languages
Regular Languages
5Automata theory formal languages and formal
grammars
6Context-Free Languages
7Context-Free Languages
Context-Free Grammars
Pushdown Automata
8Context-Free Grammars
9Example
A context-free grammar
A derivation
10 A context-free grammar
Another derivation
11(No Transcript)
12Example
A context-free grammar
A derivation
13A context-free grammar
Another derivation
14(No Transcript)
15Example
A context-free grammar
A derivation
16A context-free grammar
A derivation
17(No Transcript)
18Definition Context-Free Grammars
Grammar
Variables
Terminal symbols
Start variables
Productions of the form
19Definition Context-Free Languages
- A language is context-free
- if and only if there is a grammar with
-
20Derivation Order
Leftmost derivation
21Derivation Order
22Leftmost derivation
23Rightmost derivation
24Derivation Trees
25(No Transcript)
26(No Transcript)
27(No Transcript)
28(No Transcript)
29Derivation Tree
30Derivation Tree
yield
31Partial Derivation Trees
Partial derivation tree
32Partial derivation tree
33sentential form
Partial derivation tree
yield
34Sometimes, derivation order doesnt matter
Leftmost
Rightmost
Same derivation tree
35Ambiguity
36derivation
( denotes multiplication)
leftmost derivation
37derivation
leftmost derivation
38(No Transcript)
39Two derivation trees
40The grammar
is ambiguous!
41is ambiguous
42Definition
A context-free grammar is ambiguous if
some string has two or more
derivation trees (two or more leftmost/rightmost
derivations)
43Why do we care about ambiguity?
44Why do we care about ambiguity?
45Why do we care about ambiguity?
46(No Transcript)
47 Ambiguity is bad for programming languages We
want to remove ambiguity!
48We fix the ambiguous grammar
by introducing parentheses () to indicate
grouping, (precedence)
Non-ambiguous grammar
49(No Transcript)
50Unique derivation tree
51The grammar
is non-ambiguous
Every string has a unique
derivation tree
52Inherent Ambiguity
- Some context free languages
- have only ambiguous grammars!
53The string
has two derivation trees
54Definition Context-Free Grammars
Grammar
Variables
Terminal symbols
Start variables
Productions of the form
55Definition Regular Grammars
Grammar
Variables
Terminal symbols
Start variables
Right or Left Linear Grammars. Productions of the
form
or
56Non-regular languages
Context-Free Languages
Regular Languages
57Context-Free Languages
Pushdown Automata
Context-Free Grammars
58Pushdown AutomataPDAs
59Pushdown Automaton - PDA
Input String
Stack
States
60The Stack
A PDA can write symbols on stack and read them
later on.
POP reading symbol PUSH writing symbol
All access to the stack only on the top! (Stack
top is written leftmost in the string, e.g. yxz)
A stack is valuable as it can hold an unlimited
amount of information.
The stack allows pushdown automata to recognize
some non-regular languages.
61The States
Pop oldreading stack symbol
Push new writing stack symbol
Input symbol
62input
stack
top
Replace
(An alternative is to start and finish with empty
stack)
63input
64input
65(No Transcript)
66Example 3.7 Salling A PDA for simple nested
parenthesis strings
Time 0
Input
Stack
67Example 3.7
Time 1
Input
Stack
68Example 3.7
Time 2
Input
Stack
69Example 3.7
Time 3
Input
70Example 3.7
Time 4
Input
71Example 3.7
Time 5
Input
72Example 3.7
Time 6
Input
Stack
73Example 3.7
Time 7
Input
Stack
74ApplicationsCompilers
75Machine 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
76Compiler
Lexical analyzer
parser
input
output
machine code
program
77A parser knows the grammar of the programming
language
78Parser
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
79The parser finds the derivation of a particular
input
derivation
Parser
input
E ? E E ? E E E ? 10 EE ? 10
2 E ? 10 2 5
E ? E E E E INT
10 2 5
80derivation
derivation tree
E ? E E ? E E E ? 10 EE ? 10
2 E ? 10 2 5
81derivation tree
machine code
mult a, 2, 5 add b, 10, a
82Parsing
83Parser
input string
derivation
grammar
84Example
Parser
derivation
input
?
85Exhaustive Search
Phase 1
Find derivation of
All possible derivations of length 1
86(No Transcript)
87Phase 2
Phase 1
88Phase 2
Phase 3
89Final result of exhaustive search
(top-down parsing)
Parser
input
derivation
90Context Free Arthttp//www.contextfreeart.org/
91Context Free Arthttp//www.contextfreeart.org/