Download presentation
Presentation is loading. Please wait.
Published byMarjorie Wells Modified over 6 years ago
1
G. Pullaiah College of Engineering and Technology
Formal Languages & Automata Theory Department of Computer Science & Engineering
2
Content Midterm results Regular vs
Content Midterm results Regular vs. Non-regular Languages Context-Free Languages Context-Free Grammars Derivation Trees. Ambiguity Applications Push-Down Automata, PDA
3
Midterm 1 Solution
4
A comment on the MIDTERM 1
The Pumping Lemma for Regular Languages Pumping Lemma cannot be used to prove that a language is regular! An example: If something is a square it always has four edges (a property of square) But: having proved that something has four edges does not necessarily mean that the object is a square.
5
Time to take the next step: beyond Regular Languages
Non-regular languages Context-Free Languages Regular Languages
6
Automata theory: formal languages and formal grammars
7
Context-Free Languages
Based on C Busch, RPI, Models of Computation
8
Context-Free Languages
Grammars Pushdown Automata
9
Context-Free Grammars
10
Formal Definition Grammar Set of variables Set of terminal symbols
Start variable Set of production rules
11
Repetition: Regular Grammars
Variables Terminal symbols Start variables Right or Left Linear Grammars. Productions of the form: or is string of terminals
12
Definition: Context-Free Grammars
Variables Terminal symbols Start variables Productions of the form: is string of variables and terminals
13
Regular vs. Context-free Grammar
A regular grammar is either right or left linear, whereas context free* grammar is any combination of terminals and non-terminals. Hence regular grammars are a subset of context-free grammars. Grammar generating palindromes is not regular: *The name context-free grammar is explained by the property of productions that are independent of the surrounding symbols. There are also context-sensitive grammars where productions depend on the context (symbols that surround variables).
14
Example 1: A context-free grammar A derivation
15
A context-free grammar
Another derivation
17
Example 2: A context-free grammar A derivation
18
A context-free grammar
Another derivation
20
Example 3: A context-free grammar A derivation
21
A context-free grammar
A derivation
23
Example 4:
25
Definition: Context-Free Grammars
Variables Terminal symbols Start variables Productions of the form: is string of variables and terminals
26
Definition: Context-Free Languages
A language is context-free if and only if there is a grammar with
27
Derivation Order Leftmost derivation
28
Derivation Order Rightmost derivation
29
Leftmost derivation
30
Rightmost derivation
31
Derivation Trees
32
Derivation can be represented in a tree form
36
Derivation Tree
37
Derivation Tree yield
38
Partial Derivation Trees
39
Partial derivation tree
40
sentential form Partial derivation tree yield
41
Sometimes, derivation order doesn’t matter
Leftmost: Rightmost: The same derivation tree
42
Ambiguity
43
derivation (* denotes multiplication) leftmost derivation
44
derivation leftmost derivation
46
Two derivation trees
47
The grammar is ambiguous! String has two derivation trees
48
The grammar is ambiguous as the string has two leftmost derivations:
49
Definition A context-free grammar is ambiguous if some string has two or more derivation trees (two or more leftmost/rightmost derivations).
50
Why do we care about ambiguity?
51
Why do we care about ambiguity?
52
Why do we care about ambiguity?
53
Correct result:
54
for programming languages We want to remove ambiguity!
Ambiguity is bad for programming languages We want to remove ambiguity!
55
We fix the ambiguous grammar…
…by introducing parentheses () to indicate grouping, (precedence) Non-ambiguous grammar
57
Unique derivation tree
58
The grammar : is non-ambiguous. Every string has a unique derivation tree.
59
Inherent Ambiguity Example: Some context free languages
have only ambiguous grammars! Example:
60
The string has two derivation trees
61
Non-regular languages
Context-Free Languages Regular Languages
62
Applications: Compilers
63
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 (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } ...... Compiler
64
Compiler Lexical analyzer parser input output machine code program
65
A parser “knows” the grammar
of the programming language
66
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
67
The parser finds the derivation
of a particular input derivation Parser input E E + E E + E * E 10 + E*E * E * 5 E E + E | E * E | INT * 5
68
derivation derivation tree 10 E 2 5 + * E E + E E + E * E 10 + E*E * E * 5
69
derivation tree 10 E 2 5 + * machine code mult a, 2, 5 add b, 10, a
70
Parsing examples
71
Parser input string derivation grammar
72
Example: Parser derivation input ?
73
Exhaustive Search Phase 1: Find derivation of
All possible derivations of length 1
75
Phase 2 Phase 1
76
Phase 2 Phase 3
77
Final result of exhaustive search
(top-down parsing) Parser input derivation
78
Another use of context free grammars: Context Free Art http://www
79
Context Free Art
80
Context-Free Languages
Grammars Pushdown Automata stack automaton
81
Pushdown Automata PDAs
82
Pushdown Automaton - PDA
Input String Stack States
83
A PDA can write symbols on a stack and read them later on.
The Stack A PDA can write symbols on a 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.
84
The States Pop old reading Push new Input stack symbol writing symbol
85
input stack top Replace
(An alternative is to start and finish with empty stack)
86
input Push top stack
87
input Pop top stack
88
input No Change top stack
89
Formal Definition Pushdown Automaton is defined as 7-tuple Final
States Input alphabet Stack Transition function Final states start symbol start state
90
Example 3.7 Salling: Input Time 0
A PDA for simple nested parenthesis strings Time 0 Input Stack
91
Example 3.7 Time 1 Input Stack
92
Example 3.7 Time 2 Input Stack
93
Example 3.7 Time 3 Input Stack
94
Example 3.7 Time 4 Input Stack
95
Example 3.7 Time 5 Input Stack
96
Example 3.7 Time 6 Input Stack
97
Example 3.7 Time 7 Input Stack
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.