CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University

download CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

If you can't read please download the document

description

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. - PowerPoint PPT Presentation

Transcript of CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University

  • *

    CDT314 FABERFormal Languages, Automata and Models of ComputationLecture 8Mlardalen University2012

  • *Content

    Context-Free LanguagesPush-Down Automata, PDANPDA: Non-Deterministic PDAFormal Definitions for NPDAs NPDAs Accept Context-Free LanguagesConverting NPDA to Context-Free Grammar

  • *Regular LanguagesContext-Free LanguagesNon-regular languages

  • *Context-Free Languages Based on C Busch, RPI, Models of Computation

  • *Context-Free LanguagesPushdownAutomataContext-FreeGrammars(CF grammars are defined as generalized Regular Grammars)

  • *Definition: Context-Free GrammarsGrammarProductions of the form:VariablesTerminalsymbolsStartvariables

  • *Pushdown AutomataPDAs

  • *Pushdown Automaton - PDAInput StringStackStates

  • *The Stack The stack allows pushdown automata to recognize some non-regular languages.All access to the stack is 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 (but it is not random access!).POP reading symbol PUSH writing symbol

  • *The StatesInputsymbolPop old - readingstack symbolPush new- writingstack symbol

  • *topinputstackReplace(An alternative is to either start and finish with empty stack or with a stack bottom symbol such as $)

  • *input

  • *input

  • *

  • *NPDAsNon-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)

  • *is the language accepted by the NPDA:Example NPDA

  • *NPDA M (Even-length palindromes)Example NPDAExample : aabaaabbblbbbaaabaa

  • *Pushing StringsInputsymbolPopsymbolPushstring

  • *topinputstackPushpushedstringExample

  • *Another NPDA exampleNPDA M

  • *Time 0InputStackCurrent stateExecution Example

  • *InputStackTime 1

  • *InputStackTime 2

  • *InputStackTime 3

  • *InputStackTime 4

  • *InputStackTime 5

  • *InputStackTime 6

  • *InputStackacceptTime 7

  • *Formal Definitions for NPDAs

  • *Transition function

  • *Transition functioncurrent statecurrent input symbolcurrent stack topnew statenew stack topAn unspecified transition function is to the null set and represents a dead configuration for the NPDA.

  • *Formal DefinitionNon-Deterministic Pushdown Automaton NPDA

  • *Instantaneous DescriptionCurrentstateRemaininginputCurrentstackcontents

  • *InputStackTime 4:ExampleInstantaneous Description

  • *InputStackTime 5:ExampleInstantaneous Description

  • *We writeTime 4Time 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 writeA computation example

  • *Formal DefinitionLanguage of NPDA MInitial stateFinal state

  • *ExampleNPDA M

  • *NPDA M

  • *Therefore:NPDA M

  • *NPDAs Accept Context-Free Languages

  • *Theorem

  • *Context-FreeLanguages(Grammars)LanguagesAccepted byNPDAsProof - Step 1:Convert any context-free grammar G to a NPDA M with L(G) = L(M)

  • *Context-FreeLanguages(Grammars)LanguagesAccepted byNPDAsProof - Step 2:Convert any NPDA M to a context-free grammar G with L(M) = L(G)

  • *Converting Context-Free Grammarsto NPDAs

  • *An example grammar:What is the equivalent NPDA?

  • *GrammarNPDAFor each terminaladd transition:For each productionadd transition:

  • *The NPDA simulates the leftmost derivations of the grammarL(Grammar) = L(NPDA)

  • *Grammar:A leftmost derivation:

  • *NPDA execution:InputStackTime 0Start

  • *InputStackTime 1

  • *InputStackTime 2

  • *InputStackTime 3

  • *InputStackTime 4

  • *InputStackTime 5

  • *InputStackTime 6

  • *InputStackTime 7

  • *InputStackTime 8

  • *InputStackTime 9

  • *InputStackTime 10accept

  • *In generalGiven any grammar Gwe can construct a NPDA M with

  • *For any productionFor any terminalConstructing NPDA M from grammar GTop-down parser

  • *Grammar G generates string w if and only ifNPDA M accepts w

  • *For any context-free languagethere is an NPDAthat accepts the same language

  • *Which means

  • *Converting NPDAstoContext-Free Grammars

  • *For any NPDA Mwe will construct

    a context-free grammar G with

  • *in NPDA MInput processedStack contentsterminalsvariablesA derivation in GrammarThe grammar simulates the machine

  • *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 NPDAEmpty Stack

  • *Second we modify the NPDA transitions.All transitions will have form:orwhich 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 propertiesi.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 ConstructionIn grammar G

  • *For each transition:we add production:

  • *For each transition:we add production:for all states qk , ql

  • *Start VariableStack bottom symbolStart 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 NPDAthere is an context-free grammarthat 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:

  • *We have already shown that for any context-free languagethere is an NPDAthat accepts the same language. That is:

  • *Therefore:END OF PROOF

  • *An example of a NPDA in an appropriate form

  • *ExampleGrammar production:

  • *Grammar productions:

  • *Grammar production:

  • *Resulting Grammar

  • *Resulting Grammar, cont.

  • *Resulting Grammar, cont.

  • *Derivation of string

  • *In general, in grammar:if and only ifis accepted by the NPDA

  • *ExplanationBy construction of Grammar:if and only ifin the NPDA going from qi to qjthe stack doesnt change belowand A is removed from stack

  • *Example (Sudkamp 8.1.2)Language consisting solely of as or an equal number of as and bs.

  • *JFLAP demoConcerning examination in the course:Exercises are voluntaryLabs are voluntaryMidterms are voluntaryLectures are voluntaryAll of them are recommended!http://www.cs.duke.edu/csed/jflap/movies

    *