110 likes | 196 Views
Are all languages regular? Is the TM a FSM? Consider a mc/prog to recognise the language {ab, aabb, aaabbbb, …} i.e. a n b n . Is this a regular language? If so, then produce the FSA. If not, why not? The Pumping Lemma will help us with this. Pumping Lemma – Idea
E N D
Are all languages regular? • Is the TM a FSM? Consider a mc/prog to recognise the language {ab, aabb, aaabbbb, …} i.e. anbn . Is this a regular language? If so, then produce the FSA. If not, why not? The Pumping Lemma will help us with this. Pumping Lemma – Idea Every string/word in a regular language has form: x yi z A non-empty word can be repeatedly inserted into a word with violating acceptability.
Pumping Lemma Let L be a regular language. There exists an integer n>0 s.t. any string w e L with length |w|>=n can be represented by xyz s.t. - y is nonempty - |xy|<=n - xyiz e L for all i>=0. Proof (in book) Example The language an is regular because… Let n=1. Then any string ak, k>=1 can be represented by e ake x y z and e (ak )ie is in L. Example The language an bn is not regular because… If it is regular, then there exists n s.t. any |w|>=n contains a non-empty substring y that can be “pumped in” or “cut out” of w. Pick an bn as word, with |an bn| = 2n. Since |xy|>=n, y must be am for 0<m<=n. i.e. an-m ambn x y z This implies that an-m (am )ibn is in L, i.e. xyiz. But it isn’t!So this language is not regular.
So… • Not all languages are regular. • FSMs are not TMs. Chomsky hierarchy level 3 regular languages Noam Chomsky, 1956,59
Context free languages How can we recognise languages like an bn ? (n>=1) Grammar G S = aSb | ab (phrase structure grammar) Derive string aaabbb: S => aSb => aaSbb => aaabbb This is an example of a context-free grammar. Other examples include programming language syntax. Example S -> S while (C) {S};S | I = E | e C -> E > E E -> a | b S => S while (C) {S};S => while (C) {S};S => while (E>E) {S};S => while (a>b) {S};S (syntax charts, BNF) => while (a>b) {I=E};S => while (a>b) {I=E};
How do your recognise a CF language?Not with a FSA! an bn We need some form of memory, e.g. a stack. Idea: as you read a’s, push them onto a stack, then pop the stack as you read b’s. So, the stack works like a counter. b a No! a >
Pushdown automata Finite set of States Q with initial state Input alphabet S Stack symbols G Transition relation (Q x Su{e} x G*) -> (Q x G*) Accepting states A configuration is a (state, input tape, stack). A computation is a sequence of configurations. A transition describes how to “move” between configurations. A transition ((s,a,r),(q,g)) describes how to move from configuration state tape stack (s,u,a) to (q,n,b) when u = an (n nu, q theta, r rho,g gamma) a = rq (old stack) b = gq (new stack) stack transitions a top b > tape a a b a a head
A program to recognise {anbn|n e N}* is (s0,a,e),(s,a) push a on to stack (s,a,e),(s,a) push more a’s on to stack (s,b,a), (f,e) see b and so pop a off stack (f,b,a),(f,e) see more b’s and pop more a’s Finish when input has been read and stack is empty. Examples If you try to recognise aaabb, you are left with an empty tape, but a on stack. If you try to recognise aabbb, you are left with b on the tape, and an empty stack. If you try to recognise aabb, you are left an empty tape and an empty stack.
Pushdown automata Context Free Languages (machine) (language) • There are algorithms to convert one to the other. • CF langs are closed under U,concat, * (but not intersection) Are all languages regular or context-free? For example, what about L= {akbkck|k e N} L= {tt |t e {ab}* } These languages are not context-free – they cannot be generated by context-free grammars which have only a single nonterminal on the lhs of rules. They are generated by context-sensitive grammars which can have any number of nonterminals on the lhs, such that |lhs| <= |rhs| (rules increase size of word) Context-sensitive grammars are typically used to specify type rules for programming languages.
Example of context sensitive grammar L= {akbkck|k e N} Grammar S -> aSBC | aBC CB -> BC aB -> ab bB -> bb bC -> bc cC -> cc A derivation of a2b2c2: S => aSBC => aaBCBC => aaBBCC => aabBCC => aabbCC => aabbcC => aabbcc The rules in a context-sensitive grammar tell you how to replace a nonterminal in a given context.
Finally, Not all languages are context-sensitive! It may be the case that not all rules increase the size of the word, i.e. we can remove the restriction |lhs| <= |rhs|. This most powerful class of language is called recursively enumerable (RE). What does such a language look like? Example: L = {ww| w e {a,b}*} (Need a queue to implement!)
Summary/conclusions level 0 recursively enumerable level 1 context-sensitive level 2 context-free level 3 regular Noam Chomsky, 1956,59