1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2010
2 Content Midterm solutions Regular vs. Nonregular Languages Context-Free Languages Context-Free Grammars Derivation Trees. Ambiguity Push-Down Automata, PDA Applications
3 A Nonregular Language DFA must have infinite number of states. Statesare distinct for each
4 Regular Languages Context-Free Languages Non-regular languages
5 Automata theory: formal languages and formal grammars
6 Context-Free Languages
7 Pushdown Automata Context-Free Grammars
8 Context-Free Grammars
9 A context-free grammar A derivation Example
10 A context-free grammar Another derivation
11
12 A context-free grammar A derivation Example
13 A context-free grammar Another derivation
14
15 A context-free grammar A derivation Example
16 A context-free grammar A derivation
17
18 Definition: Context-Free Grammars Grammar Productions of the form: is string of variables and terminals VariablesTerminal symbols Start variables
19 Definition: Context-Free Languages A language is context-free if and only if there is a grammar with
20 Derivation Order Leftmost derivation
21 Derivation Order Rightmost derivation
22 Leftmost derivation
23 Rightmost derivation
24 Derivation Trees
25
26
27
28
29 Derivation Tree
30 yield Derivation Tree
31 Partial Derivation Trees Partial derivation tree
32 Partial derivation tree
33 Partial derivation tree sentential form yield
34 Same derivation tree Sometimes, derivation order doesn’t matter Leftmost: Rightmost:
35 Ambiguity
36 leftmost derivation derivation (* denotes multiplication)
37 derivation leftmost derivation
38
39 Two derivation trees
40 The grammar is ambiguous! Stringhas two derivation trees
41 stringhas two leftmost derivations The grammar is ambiguous:
42 Definition A context-free grammar is ambiguous if some string has two or more derivation trees (two or more leftmost/rightmost derivations)
43 Why do we care about ambiguity?
44 Why do we care about ambiguity?
45 Why do we care about ambiguity?
46 Correct result:
47 Ambiguity is bad for programming languages We want to remove ambiguity!
48 We fix the ambiguous grammar… …by introducing parentheses () to indicate grouping, (precedence) Non-ambiguous grammar
49
50 Unique derivation tree
51 The grammar : is non-ambiguous Every string has a unique derivation tree
52 Inherent Ambiguity Some context free languages have only ambiguous grammars! Example:
53 The string has two derivation trees
54 Definition: Context-Free Grammars Grammar Productions of the form: is string of variables and terminals VariablesTerminal symbols Start variables
55 Definition: Regular Grammars Grammar Right or Left Linear Grammars. Productions of the form: is string of terminals VariablesTerminal symbols Start variables or
56 Regular Languages Context-Free Languages Non-regular languages
57 Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton
58 Pushdown Automata PDAs
59 Pushdown Automaton - PDA Input String Stack States
60 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 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
61 The States Input symbol Pop old reading stack symbol Push new writing stack symbol
62 top input stack Replace (An alternative is to start and finish with empty stack)
63 input Push top stack
64 input Pop top stack
65 input No Change top stack
66 Input Stack Example 3.7 Salling: A PDA for simple nested parenthesis strings Time 0
67 Input Stack Example 3.7 Time 1
68 Input Stack Example 3.7 Time 2
69 Input Stack Example 3.7 Time 3
70 Input Stack Example 3.7 Time 4
71 Input Stack Example 3.7 Time 5
72 Input Stack Example 3.7 Time 6
73 Input Example 3.7 Time 7 Stack
74 Applications: Compilers
75 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
76 Lexical analyzer parser Compiler program machine code input output
77 A parser “knows” the grammar of the programming language
78 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
79 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
80 derivation derivation tree E E + E E + E * E 10 + E*E * E * 5 10 E 2 E 5 E E + E *
81 derivation tree mult a, 2, 5 add b, 10, a machine code 10 E 2 E 5 E E + E *
82 Parsing
83 grammar Parser input string derivation
84 Example: Parser derivation input ?
85 Exhaustive Search Phase 1: All possible derivations of length 1 Find derivation of
86
87 Phase 2 Phase 1
88 Phase 2 Phase 3
89 Final result of exhaustive search Parser derivation input (top-down parsing)
90 Context Free Art
91 Context Free Art