# 12.1 Languages and Grammars

date post

15-Jan-2016Category

## Documents

view

24download

0

Embed Size (px)

description

### Transcript of 12.1 Languages and Grammars

Slide 1

12.1 Languages and GrammarsDefineVocabulary V (alphabet)

Word (string)

Empty string

Free language V*

A language over V

an, for a in V, n in N.Phrase-Structure GrammarsA phrase-structure grammar is a 4-tuple (V,T,S,P)ExampleThe Language Generated by a GrammarThe language L(G) generated by a grammar G is the subset of T* consisting of all the strings of terminal symbols which can be generated from the start symbol S by applying a series of productions.ExampleGenerate the string aaabb from the start symbol of the grammar at right.SolutionNote that the language L generated by a grammar consists of all strings of terminal symbols that are derivable from the start symbol SIn the above example,

ExampleA grammar that generates the set

is given at rightShow how the strings 012 and 001122 can be derived from the start symbol S

ExampleFind a grammar that generates the set

Language TypesType 0 No restrictionsType 1 All productions are of the form w1w2, where length(w1) length(w2) and w1 contains a non-terminalType 2 All productions are of the form NwType 3 All productions are of the form Aa or AaBContext-Free GrammarsType 2 languages (where all productions are of the form Nw) are called context-free grammars.If a grammar is context-free, then all the words in the language have a parse tree, also called a derivation tree.ExampleConstruct a parse tree for aaabb in the grammar at right.ParsingTo parse is to discover the form of the derivation treeTop-down parsing starts at the root, with the start symbol, and constructs the tree from thereBottom-up parsing builds the tree up from the leaves, by grouping symbols into valid right-hand sides of productionsBackus-Naur Form (BNF)Backus-Naur Form, or BNF, is a notation for describing grammars, first used in the description of the syntax of the ALGOL 60 languageNon-terminal symbols are descriptive terms enclosed in angular brackets (e.g. instead of S)The symbol used for is ::=Alternative right-hand sides can be combined using a vertical slash (|) to separate themExample of a Portion of a Simple BNF Grammar ::= | | ::= | + ::= | * ::= | | ( )Parse Tree for 12*(2+x) ::= | | ::= | + ::= | * ::= | | ( )12.2 Finite State Machines with OutputA finite state machine with output is a 6-tuple (S, I, O, f, g, s0), where S is a finite set of states, I is a finite set of input symbols, O is a finite set of output symbols, f is a state transition function, g is an output function, and s0 is a designated state from the set S, called the start state.Example:Extending the Output FunctionRecall that the output function g maps a (state, input symbol) pair to a single output symbolWe can extend that function so that it maps an input string to a string of output symbols.For instance, in the above example g(01100100) =ExampleInput alphabet is {a, b}. Output a 1 if the input string began with bab, output 0 otherwise.More ExamplesMachine to reverse bits

Machine to output 1 for an odd number of 1s, 0 otherwiseVending Machine ExampleMachine takes quarters only. Outputs a coke if 75 cents has been entered and the vend button has been pressed (use output symbol c)Outputs a quarter if 75 cents has already been entered (use output symbol 25)No output otherwise (use output symbol n)12.3 Finite State Machines with No OutputConcatenation of sets of strings

Powers An of a set of symbols A

Kleene Closure A* of a set of strings AExamples: Deterministic Finite State AutomataA deterministic finite state automaton (DFSA) is a 5-tuple M = (S, I, f, s0 , F), where S is a finite set of states, I is an input alphabet, f:SIS is a state transition function, and F is a subset of S called the set of final states.The language accepted by M is the set of strings in I* which, if given as input to M, will leave the machine in a final state.ExamplesConstruct a machine with input alphabet {0, 1} which accepts any string with an even number of 1s

Construct a machine which accepts {0,1}*.ExampleWhat language is accepted by the following machine?ExampleWhat language is accepted by the following machine?Non-Deterministic Finite-State AutomataA non-deterministic finite-state automaton (NFSA) is defined the same as a dfsa, except that the state transition function maps each (state, input symbol) pair to a set of states; i.e. f:SIP(S).If f(s,a) is {s',s"}, then the machine in state s could, if its input is a, either transition to state s' or to state s".If f(s,a) is {} (the empty set), then the machine in state s would consider a an invalid input symbol.The language accepted by an NFSA is the set of all input strings which could put the machine in a final state if the right choices were made at each step in the execution of the machine.

ExampleWhat language is accepted by the following NFSA?Constructing a DFSA from an NFSAAny NFSA can be transformed into a DFSA by using sets of states as the states in the new machine.The start state of the new machine is {s0}.The transition function g of the new machine can be defined in terms of the original function f and sets of states.A set representing a state in the new machine is considered final if any state in the set is final.Example: Transform the machine on the previous slide into a DFSA.Note that the language accepted by the original NFSA is the same as that accepted by the DFSA constructed from it.This can be expressed as a theorem about languages12.4 Language Recognition Let I be a finite set of input symbols. The regular expressions over I are the symbols in I along with the empty string , the empty set , and any expression that can be formed from those elements using concatenation, union, and Kleene closureRegular expressions are used to generate the category of languages called regular languagesRegular LanguagesRegular expressions always describe sets, and when a symbol, say a, is used in a regular expression it stands for {a}.Thus (a ab)* is the Kleene closure of the set {a, ab}.A language generated in this way by a regular expression is called a regular language.Examples: Give a regular expression describing The set { | is or is all zeros or is all ones}

the set { | is or is an alternating sequence of 0s and 1s}

What are the Regular Languages? Kleenes Theorem: The regular languages are precisely those languages which can be recognized by FSAs.Draw a DFSA which recognizes the last example of a regular language.Regular GrammarsRecall that in a regular grammar, all the productions are of the form Aa or AaB, where A and B are non-terminals and a is a terminal symbol.A regular language has the property that all of its non-empty strings can be generated by the productions of a regular grammarExample:Find a regular grammar capable of generating the nonempty strings recognized by the FSA shown aboveWhich Languages are not Regular?Example: {0n1n | n 0} is not regular12.5 Turing Machines

A Turing Machine (TM) is a quadruple (S, I, f, s0), where S is a set of states, I is a set of input/output symbols, f is a transition function, and s0 is an initial state. A special symbol B, called the blank symbol, is always an element of I.The transition function is actually a partial function, since its domain is a subset of SI, not the complete set. But for every pair (s,i) for which f(s,i) is defined, f(s,i) will be a triple (s,i,D), where s is a state, i is an ouput symbol, and D is either the symbol R (for right) or the symbol L (for left).PictureA Turing Machine is conceptualized as taking its input from and writing its output to a discrete tape (actually a sequence of storage cells) that is infinite in both directions. All but a finite number of cells contain the blank symbol B.

By convention, the initial position of a TM is with the read head of the machine positioned over the leftmost non-blank symbol.

At each step of the machines operation, it reads the input symbol from the tape, changes its state, writes a symbol back into the same cell, then moves either left or right.ExampleStates:s0, start state moving right, looking for 1ss1, moving right, looking for 1s, found 1s2, moving right, looking for 1s, found 2s3, moving right, looking for 1s, found 3s4, found four 1s.ExampleStates:s0, start state moving right, looking for 1ss1, moving right, looking for 1s, found 1s2, moving right, looking for 1s, found 2s3, moving right, looking for 1s, found 3s4, found four 1s.ExampleStates:s0, start state moving right, looking for 1ss1, moving right, looking for 1s, found 1s2, moving right, looking for 1s, found 2s3, moving right, looking for 1s, found 3s4, found four 1s.ExampleStates:s0, start state moving right, looking for 1ss1, moving right, looking for 1s, found 1s2, moving right, looking for 1s, found 2s3, moving right, looking for 1s, found 3s4, found four 1s.ExampleStates:s0, start state moving right, looking for 1ss1, moving right, looking for 1s, found 1s2, moving right, looking for 1s, found 2s3, moving right, looking for 1s, found 3s4, found four 1s.ExampleStates:s0, start state moving right, looking for 1ss1, moving right, looking for 1s, found 1s2, moving right, looking for 1s, found 2s3, moving right, looking for 1s, found 3s4, found four 1s.ExampleStates:s0, start state moving right, looking for 1ss1, moving right, looking for 1s, found 1s2, moving right, looking for 1s, found 2s3, moving right, looking for 1s, found 3s4, found four 1s.ExampleStates:s0, start state moving right, looking for 1ss1, moving right, looking for 1s, found 1s2, moving right, looking for 1s, found 2s3, moving right, looking for 1s, found 3s4, found four 1s.Transition FunctionThe transition function for this TM is given below:Final StatesA state si is said to be a final state if there are no actions defined for that state, i.e. if there are no pairs of the form (si, x) for which the transition function is defined. The language recognized by a Turing Machine T is the set of all strings of input symbols which, if placed on the tape with the read head at the initial position, will cause the machine to eventually transition into a final state.ExampleA TM that recognizes {0n1n| n 1}.001MR1010R1111R1M2ML1_2_L213ML3M5MR3040L3141L4040L4141L4M0MRExample: Construct a Turing machine with tape symbols 0, 1 and B that, when given a bit string as input, replaces the first 0 with a 1 and does not change any of the other symbols on the tape. Example: Construct a Turing machine that recognizes the set of all bit strings that end with a 0.TheoremThe languages that can be recognized by a TM are precisely the type 0 languages (languages for which a grammar exists)Computing Functions with a TMUnary Representation1 for 011 for 1111 for 2Etc.Example: f(n) = n mod 3. One way is to write an * after n, then place f(n) to the right of that.Use the following 5-tuples:(s0,1,s1,1,R), (s1,B,s5,*,R), (s1,1,s2,1,R), (s2,B,s6,*,R), (s5,B,s4,1,R),(s2,1,s3,1,R), (s3,B,s7,*,R), (s7,B,s6,1,R), (s6,B,s5,1,R), (s3,1,s1,1,R)(s0,1,s1,1,R), (s1,B,s5,*,R), (s1,1,s2,1,R), (s2,B,s6,*,R), (s5,B,s4,1,R)(s2,1,s3,1,R), (s3,B,s7,*,R), (s7,B,s6,1,R), (s6,B,s5,1,R), (s3,1,s1,1,R)

Computable FunctionsA function is computable if and only ifExample of a non-computable function:The Church-Turing ThesisDecision ProblemsA decision problem asks

The Halting problem is an unsolvable decision problem.

A decision problem is P if

A decision problem is NP if