G. Pullaiah College of Engineering and Technology Formal Languages & Automata Theory Department of Computer Science & Engineering
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
Midterm 1 Solution http://www.idt.mdh.se/kurser/cd5560/12_11/examination/Duggor/MIDTERM1-20121127-Solution.pdf
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. http://www2.mat.ua.pt/rosalia/cadeiras/TC/pump.pdf
Time to take the next step: beyond Regular Languages Non-regular languages Context-Free Languages Regular Languages
Automata theory: formal languages and formal grammars
Context-Free Languages Based on C Busch, RPI, Models of Computation
Context-Free Languages Grammars Pushdown Automata
Context-Free Grammars
Formal Definition Grammar Set of variables Set of terminal symbols Start variable Set of production rules
Repetition: Regular Grammars Variables Terminal symbols Start variables Right or Left Linear Grammars. Productions of the form: or is string of terminals
Definition: Context-Free Grammars Variables Terminal symbols Start variables Productions of the form: is string of variables and terminals
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).
Example 1: A context-free grammar A derivation
A context-free grammar Another derivation
Example 2: A context-free grammar A derivation
A context-free grammar Another derivation
Example 3: A context-free grammar A derivation
A context-free grammar A derivation
Example 4:
Definition: Context-Free Grammars Variables Terminal symbols Start variables Productions of the form: is string of variables and terminals
Definition: Context-Free Languages A language is context-free if and only if there is a grammar with
Derivation Order Leftmost derivation
Derivation Order Rightmost derivation
Leftmost derivation
Rightmost derivation
Derivation Trees
Derivation can be represented in a tree form
Derivation Tree
Derivation Tree yield
Partial Derivation Trees
Partial derivation tree
sentential form Partial derivation tree yield
Sometimes, derivation order doesn’t matter Leftmost: Rightmost: The same derivation tree
Ambiguity
derivation (* denotes multiplication) leftmost derivation
derivation leftmost derivation
Two derivation trees
The grammar is ambiguous! String has two derivation trees
The grammar is ambiguous as the string has two leftmost derivations:
Definition A context-free grammar is ambiguous if some string has two or more derivation trees (two or more leftmost/rightmost derivations).
Why do we care about ambiguity?
Why do we care about ambiguity?
Why do we care about ambiguity?
Correct result:
for programming languages We want to remove ambiguity! Ambiguity is bad for programming languages We want to remove ambiguity!
We fix the ambiguous grammar… …by introducing parentheses () to indicate grouping, (precedence) Non-ambiguous grammar
Unique derivation tree
The grammar : is non-ambiguous. Every string has a unique derivation tree.
Inherent Ambiguity Example: Some context free languages have only ambiguous grammars! Example:
The string has two derivation trees
Non-regular languages Context-Free Languages Regular Languages
Applications: Compilers
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
Compiler Lexical analyzer parser input output machine code program
A parser “knows” the grammar of the programming language
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
The parser finds the derivation of a particular input derivation Parser input E E + E E + E * E 10 + E*E 10 + 2 * E 10 + 2 * 5 E E + E | E * E | INT 10 + 2 * 5
derivation derivation tree 10 E 2 5 + * E E + E E + E * E 10 + E*E 10 + 2 * E 10 + 2 * 5
derivation tree 10 E 2 5 + * machine code mult a, 2, 5 add b, 10, a
Parsing examples
Parser input string derivation grammar
Example: Parser derivation input ?
Exhaustive Search Phase 1: Find derivation of All possible derivations of length 1
Phase 2 Phase 1
Phase 2 Phase 3
Final result of exhaustive search (top-down parsing) Parser input derivation
Another use of context free grammars: Context Free Art http://www
Context Free Art
Context-Free Languages Grammars Pushdown Automata stack automaton
Pushdown Automata PDAs
Pushdown Automaton - PDA Input String Stack States
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.
The States Pop old reading Push new Input stack symbol writing symbol
input stack top Replace (An alternative is to start and finish with empty stack)
input Push top stack
input Pop top stack
input No Change top stack
Formal Definition Pushdown Automaton is defined as 7-tuple Final States Input alphabet Stack Transition function Final states start symbol start state
Example 3.7 Salling: Input Time 0 A PDA for simple nested parenthesis strings Time 0 Input Stack
Example 3.7 Time 1 Input Stack
Example 3.7 Time 2 Input Stack
Example 3.7 Time 3 Input Stack
Example 3.7 Time 4 Input Stack
Example 3.7 Time 5 Input Stack
Example 3.7 Time 6 Input Stack
Example 3.7 Time 7 Input Stack