11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012
2 Content Midterm results Regular vs. Non-regular Languages Context-Free Languages Context-Free Grammars Derivation Trees. Ambiguity Applications Push-Down Automata, PDA
Midterm 1 Solution 3 Duggor/MIDTERM Solution.pdf
4 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. A comment on the MIDTERM 1
5 Regular Languages Context-Free Languages Non-regular languages Time to take the next step: beyond 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 Pushdown Automata Context-Free Grammars
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 Grammar Right or Left Linear Grammars. Productions of the form: is string of terminals VariablesTerminal symbols Start variables or
12 Definition: Context-Free Grammars Grammar Productions of the form: is string of variables and terminals VariablesTerminal symbols Start variables
13 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: Regular vs. Context-free Grammar *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 A context-free grammar A derivation Example 1:
15 A context-free grammar Another derivation
16
17 A context-free grammar A derivation Example 2:
18 A context-free grammar Another derivation
19
20 A context-free grammar A derivation Example 3:
21 A context-free grammar A derivation
22
Example 4:
25 Definition: Context-Free Grammars Grammar Productions of the form: is string of variables and terminals VariablesTerminal symbols Start variables
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
33
34
35
36 Derivation Tree
37 yield Derivation Tree
38 Partial Derivation Trees Partial derivation tree
39 Partial derivation tree
40 Partial derivation tree sentential form yield
41 The same derivation tree Sometimes, derivation order doesn’t matter Leftmost: Rightmost:
42 Ambiguity
43 leftmost derivation derivation (* denotes multiplication)
44 derivation leftmost derivation
45
46 Two derivation trees
47 The grammar is ambiguous! Stringhas two derivation trees
48 has two leftmost derivations: The grammar is ambiguous as the string
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 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
56
57 Unique derivation tree
58 The grammar : is non-ambiguous. Every string has a unique derivation tree.
59 Inherent Ambiguity Some context free languages have only ambiguous grammars! Example:
60 The string has two derivation trees
61 Regular Languages Context-Free Languages Non-regular languages
62 Applications: Compilers
63 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v ELSE: WHILE: cmp x,3... Machine Code
64 Lexical analyzer parser Compiler program machine code input output
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 * 5 Parser E E + E | E * E | INT E E + E E + E * E 10 + E*E * E * 5 input derivation
68 derivation derivation tree E E + E E + E * E 10 + E*E * E * 5 10 E 2 E 5 E E + E *
69 derivation tree mult a, 2, 5 add b, 10, a machine code 10 E 2 E 5 E E + E *
70 Parsing examples
71 grammar Parser input string derivation
72 Example: Parser derivation input ?
73 Exhaustive Search Phase 1: All possible derivations of length 1 Find derivation of
74
75 Phase 2 Phase 1
76 Phase 2 Phase 3
77 Final result of exhaustive search Parser derivation input (top-down parsing)
78 Another use of context free grammars: Context Free Art
79 Context Free Art
80 Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton
81 Pushdown Automata PDAs
82 Pushdown Automaton - PDA Input String Stack States
83 The Stack The stack allows pushdown automata to recognize some non-regular languages. All access to the stack only on the top! (Stack top is written leftmost in the string, e.g. yxz) A PDA can write symbols on a stack and read them later on. A stack is valuable as it can hold an unlimited amount of information. POP reading symbol PUSH writing symbol
84 The States Input symbol Pop old reading stack symbol Push new writing stack symbol
85 top input stack 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 States Input alphabet Stack alphabet Transition function Final states Stack start symbol start state
90 Input Stack Example 3.7 Salling: A PDA for simple nested parenthesis strings Time 0
91 Input Stack Example 3.7 Time 1
92 Input Stack Example 3.7 Time 2
93 Input Stack Example 3.7 Time 3
94 Input Stack Example 3.7 Time 4
95 Input Stack Example 3.7 Time 5
96 Input Stack Example 3.7 Time 6
97 Input Example 3.7 Time 7 Stack