Formal Languages, Automata and Models of Computation CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2012
Content Context-Free Languages Push-Down Automata, PDA NPDA: Non-Deterministic PDA Formal Definitions for NPDAs NPDAs Accept Context-Free Languages Converting NPDA to Context-Free Grammar
Non-regular languages Context-Free Languages Regular Languages
Context-Free Languages Based on C Busch, RPI, Models of Computation
Context-Free Languages Grammars Pushdown Automata stack automaton (CF grammars are defined as generalized Regular Grammars)
Definition: Context-Free Grammars Variables Terminal symbols Start variables Productions of the form: is string of variables and terminals
Pushdown Automata PDAs
Pushdown Automaton - PDA Input String Stack States
The Stack A PDA can write symbols on stack and read them later on. POP reading symbol PUSH writing symbol All access to the stack is 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 (but it is not random access!). The stack allows pushdown automata to recognize some non-regular languages.
The States Pop old - reading Push new Input symbol stack symbol - writing stack symbol Input symbol
input stack top Replace (An alternative is to either start and finish with empty stack or with a stack bottom symbol such as $)
input Push top stack
input Pop top stack
input No Change top stack
NPDAs Non-deterministic Push-Down Automata
Non-Determinism
A string is accepted if: All the input is consumed The last state is a final state Stack is in the initial condition (either: empty (when we started with empty stack), or: bottom symbol reached, depending on convention)
Example NPDA is the language accepted by the NPDA:
Example NPDA NPDA M (Even-length palindromes) Example : aabaaabbblbbbaaabaa
Pushing Strings Pop symbol Input symbol Push string
Example input pushed string stack top Push
Another NPDA example NPDA M
Execution Example Time 0 Input Stack Current state
Time 1 Input Stack
Time 2 Input Stack
Time 3 Input Stack
Time 4 Input Stack
Time 5 Input Stack
Time 6 Input Stack
Time 7 Input Stack accept
Formal Definitions for NPDAs
Transition function
Transition function new state current state current stack top new stack top current input symbol An unspecified transition function is to the null set and represents a dead configuration for the NPDA.
Formal Definition Non-Deterministic Pushdown Automaton NPDA Final States Input alphabet Stack Transition function Final states start symbol
Instantaneous Description Current stack contents Current state Remaining input
Instantaneous Description Example Instantaneous Description Input Time 4: Stack
Instantaneous Description Example Instantaneous Description Input Time 5: Stack
We write Time 4 Time 5
A computation example
A computation example
A computation example
A computation example
A computation example
A computation example
A computation example
A computation example
For convenience we write A computation example For convenience we write
Formal Definition Language of NPDA M Initial state Final state
Example NPDA M
NPDA M
Therefore: NPDA M
NPDAs Accept Context-Free Languages
Theorem Context-Free Languages (Grammars) Accepted by NPDAs
Proof - Step 1: Context-Free Languages Languages Accepted by (Grammars) Languages Accepted by NPDAs Convert any context-free grammar G to a NPDA M with L(G) = L(M)
Proof - Step 2: Context-Free Languages Languages Accepted by (Grammars) Languages Accepted by NPDAs Convert any NPDA M to a context-free grammar G with L(M) = L(G)
Converting Context-Free Grammars to NPDAs
An example grammar: What is the equivalent NPDA?
Grammar NPDA For each production add transition: For each terminal
L(Grammar) = L(NPDA) The NPDA simulates the leftmost derivations of the grammar L(Grammar) = L(NPDA)
Grammar: A leftmost derivation:
NPDA execution: Time 0 Input Stack Start
Time 1 Input Stack
Time 2 Input Stack
Time 3 Input Stack
Time 4 Input Stack
Time 5 Input Stack
Time 6 Input Stack
Time 7 Input Stack
Time 8 Input Stack
Time 9 Input Stack
Time 10 Input Stack accept
In general Given any grammar G we can construct a NPDA M with
Constructing NPDA M from grammar G Top-down parser For any production For any terminal
Grammar G generates string w if and only if NPDA M accepts w
For any context-free language there is an NPDA that accepts the same language
Context-Free Languages (Grammars) Which means Languages Accepted by NPDAs Context-Free Languages (Grammars)
Converting NPDAs to Context-Free Grammars
For any NPDA M we will construct a context-free grammar G with
The grammar simulates the machine A derivation in Grammar terminals variables Input processed Stack contents in NPDA M
Some Simplifications First we modify the NPDA so that It has a single final state qf and It empties the stack when it accepts the input. Original NPDA Empty Stack
Second we modify the NPDA transitions. All transitions will have form: which means that each move increases/decreases stack by a single symbol.
Those simplifications do not affect generality of our argument. It can be shown that for any NPDA there exists an equivalent one having the above two properties i.e. the equivalent NPDA with a single final state which empties its stack when it accepts the input, and which for each move increases/decreases stack by a single symbol.
The Grammar Construction In grammar G Terminals: Input symbols of NPDA states Stack symbol Variables:
For each transition: we add production:
For each transition: we add production: for all states qk , ql
Stack bottom symbol Start Variable Start state (Single) Final state
From NPDA to CFG, in short: When we write a grammar, we can use any variable names we choose. As in programming languages, we like to use "meaningful" variable names. Translating an NPDA into a CFG, we will use variable names that encode information about both the state of the NPDA and the stack contents. Variable names will have the form [qiAqj], where qi and qj are states and A is a variable. The "meaning" of the variable [qiAqj] is that the NPDA can go from state qi with Ax on the stack to state qj with x on the stack. Each transition of the form (qi, a, A) = (qj,l ) results in a single grammar rule.
From NPDA to CFG Each transition of the form (qi, a, A) = (qj, BC) results in a multitude of grammar rules, one for each pair of states qx and qy in the NPDA. This algorithm results in a lot of useless (unreachable) productions, but the useful productions define the context-free grammar recognized by the NPDA. http://www.seas.upenn.edu/~cit596/notes/dave/npda-cfg6.html http://www.cs.duke.edu/csed/jflap/tutorial/pda/cfg/index.html using JFLAP
For any NPDA there is an context-free grammar that generates the same language.
We have the procedure to convert any NPDA M to a context-free grammar G with L(M) = L(G) which means: Context-Free Languages (Grammars) Accepted by NPDAs
Context-Free Languages (Grammars) We have already shown that for any context-free language there is an NPDA that accepts the same language. That is: Languages Accepted by NPDAs Context-Free Languages (Grammars)
Therefore: Context-Free Languages (Grammars) Accepted by NPDAs END OF PROOF
An example of a NPDA in an appropriate form
Example Grammar production:
Grammar productions:
Grammar production:
Resulting Grammar
Resulting Grammar, cont.
Resulting Grammar, cont.
Derivation of string
In general, in grammar: if and only if is accepted by the NPDA
Explanation By construction of Grammar: if and only if in the NPDA going from qi to qj the stack doesn’t change below and A is removed from stack
Example (Sudkamp 8.1.2) Language consisting solely of a’s or an equal number of a´s and b´s.
Concerning examination in the course: Exercises are voluntary Labs are voluntary Midterms are voluntary Lectures are voluntary… All of them are recommended! JFLAP demo http://www.cs.duke.edu/csed/jflap/movies