MA/CSSE 474 Theory of Computation

Slides:



Advertisements
Similar presentations
1 Pushdown Automata (PDA) Informally: –A PDA is an NFA-ε with a stack. –Transitions are modified to accommodate stack operations. Questions: –What is a.
Advertisements

Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
Pushdown Automata Chapter 12. Recognizing Context-Free Languages We need a device similar to an FSM except that it needs more power. The insight: Precisely.
The Big Picture Chapter 3. We want to examine a given computational problem and see how difficult it is. Then we need to compare problems Problems appear.
Pushdown Automata Part II: PDAs and CFG Chapter 12.
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
Introduction to Computability Theory
CS5371 Theory of Computation
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
Chapter 3: Formal Translation Models
CS5371 Theory of Computation Lecture 12: Computability III (Decidable Languages relating to DFA, NFA, and CFG)
Problem of the DAY Create a regular context-free grammar that generates L= {w  {a,b}* : the number of a’s in w is not divisible by 3} Hint: start by designing.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
CMPS 3223 Theory of Computation Automata, Computability, & Complexity by Elaine Rich ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Slides provided.
Finite State Machines Chapter 5. Languages and Machines.
CMPS 3223 Theory of Computation
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
Pushdown Automata (PDA) Intro
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
Context-Free Grammars Normal Forms Chapter 11. Normal Forms A normal form F for a set C of data objects is a form, i.e., a set of syntactically valid.
TM Design Universal TM MA/CSSE 474 Theory of Computation.
Context-Free Grammars Chapter 11. Languages and Machines.
MA/CSSE 474 Theory of Computation DFSM Canonical Form Proof of NDFSM  DFSM ALGORITHM (as much as we have time for) This version includes the "answers"
MA/CSSE 474 Theory of Computation More Reduction Examples Non-SD Reductions.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Chapter 5 Context-Free Grammars
Grammars CPSC 5135.
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
Context-Free Grammars Chapter 11. Languages and Machines.
Pushdown Automata Part I: PDAs Chapter Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)
MA/CSSE 474 Theory of Computation Enumerability Reduction.
Poetry The Pumping Lemma Any regular language L has a magic number p And any long-enough word in L has the following property: Amongst its first p symbols.
MA/CSSE 474 Theory of Computation Decision Problems DFSMs.
Turing Machines Chapter 17. Languages and Machines SD D Context-Free Languages Regular Languages reg exps FSMs cfgs PDAs unrestricted grammars Turing.
CS 321 Programming Languages and Compilers Lectures 16 & 17 Introduction to Formal Languages Regular Languages Lexical Analysis.
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
Context-Free Grammars Chapter Languages and Machines 2.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
TM Design Macro Language D and SD MA/CSSE 474 Theory of Computation.
Context-Free Languages
MA/CSSE 474 Theory of Computation Minimizing DFSMs.
Context-Free Grammars Chapter 11. Languages and Machines.
Mathematical Foundations of Computer Science Chapter 3: Regular Languages and Regular Grammars.
The Big Picture Chapter 3.
Unrestricted Grammars
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
Nondeterministic Finite State Machines Chapter 5.
Finite State Machines Chapter 5. Languages and Machines.
Context-Free Grammars Chapter 11. Languages and Machines.
Algorithms and Decision Procedures for Regular Languages Chapter 9.
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
MA/CSSE 474 Theory of Computation How many regular/non-regular languages are there? Closure properties of Regular Languages (if there is time) Pumping.
MA/CSSE 474 Theory of Computation Decision Problems, Continued DFSMs.
MA/CSSE 474 Theory of Computation Regular Expressions Intro.
Normal Forms (Chomsky and Greibach) Pushdown Automata (PDA) Intro PDA examples MA/CSSE 474 Theory of Computation.
MA/CSSE 474 Decision Problems about Regular Languages
MA/CSSE 474 Theory of Computation
MA/CSSE 474 Theory of Computation Minimizing DFSMs.
MA/CSSE 474 Theory of Computation
MA/CSSE 474 Theory of Computation
MA/CSSE 474 Theory of Computation
MA/CSSE 474 Theory of Computation
MA/CSSE 474 Theory of Computation Nondeterminism NFSMs.
More on showing L non-regular
MA/CSSE 474 Theory of Computation
Minimize # states in a DFSM
MA/CSSE 474 Theory of Computation
More About Nondeterminism
Answer Questions about Exam2 problems
Presentation transcript:

MA/CSSE 474 Theory of Computation Algorithms and Decision Procedures for Regular Languages Intro to Context-free Grammars

Your parents will probably concur!… Your Questions? Your parents will probably concur!… Previous class days' material Reading Assignments HW 7 or 8 problems Exam 2 Anything else

Summary of Algorithms The next few slides are here for reference. I do not expect to spend class time on them. You should know how to do all of them, but during today's exercises you may simply "call" any of them as part of your decision procedures. ● Operate on FSMs without altering the language that is accepted: ● ndfsmtodfsm(M: NDFSM) ● minDFSM (M:DFSM) ● buildFSMcanonicalform(M:FSM)

Summary of Algorithms ● Compute functions of languages defined as FSMs: ● Given FSMs M1 and M2, construct a FSM M3 such that L(M3) = L(M2)  L(M1). ● Given FSMs M1 and M2, construct a new FSM M3 such that L(M3) = L(M2) L(M1). ● Given FSM M, construct an FSM M* such that L(M*) = (L(M))*. ● Given a DFSM M, construct an FSM M* such that L(M*) = L(M). ● Given two FSMs M1 and M2, construct an FSM M3 such that L(M3) = L(M2)  L(M1). L(M3) = L(M2) - L(M1). ● Given an FSM M, construct an FSM M* such that L(M*) = (L(M))R. ● Given an FSM M, construct an FSM M* that accepts letsub(L(M)).

Algorithms, Continued ● Converting between FSMs and regular expressions: ● Given a regular expression , construct an FSM M such that: L() = L(M) ● Given an FSM M, construct a regular expression  ● Algorithms that implement operations on languages defined by regular expressions: any operation that can be performed on languages defined by FSMs can be implemented by converting all regular expressions to equivalent FSMs and then executing the appropriate FSM algorithm.

Algorithms, Continued ● Converting between FSMs and regular grammars: ● Given a regular grammar G, construct an FSM M such that: L(G) = L(M) ● Given an FSM M, construct a regular grammar G L(G) = L(M). We have not yet discussed regular grammars. They are in the reading assignment for tomorrow. This is here for completeness.

Decision Procedures A decision procedure is an algorithm whose result is a Boolean value (in this course's context, sometimes we will write "yes" or "no" instead of "true" or "false"). It must: ● Halt, no matter what (correctly typed) input it is given. ● Always give the correct answer Usually a decision procedure has one or more parameters Zero-parameter decision procedures are not very interesting!

Decision Procedure Example Given two regular expressions 1 and 2, is: (L(1)  L(2)) – {}  ? 1. From 1, construct an FSM M1 such that L(1) = L(M1). 2. From 2, construct an FSM M2 such that L(2) = L(M2). 3. Construct M such that L(M ) = L(M1)  L(M2). 4. Construct M such that L(M) = {}. 5. Construct M such that L(M ) = L(M ) - L(M). 6. If L(M ) is empty return False; else return True. For practice later: Given two regular expressions 1 and 2, are there at least 3 strings that are generated by both of them?

Decision Procedure Practice Find a group of three students. Work together to write decision procedures for as many of the problems on the following slide as you can do in the allotted time.

Decision Procedures for Regular Languages ● Given a FSM M and a string w, does M accept w? ● Given a regular expression  and a string w, does  generate w? ● Given a FSM M, is L(M) empty? ● Given a FSM M, does L(M) = M*? ● Given a FSM M, is L(M) finite? ● Given a FSM M, is L(M) infinite? ● Given two FSMs M1 and M2, are they equivalent? ● Given an DFSM M, is M minimal?

Membership We can answer the membership question by running an FSM. But we must be careful if it's an NDFSM: These slide are all hidden, and the contents are on my Qiuiz18 key document

Membership decideFSM(M: FSM, w: string) = If ndfsmsimulate(M, w) accepts then return True else return False. decideregex(: regular expression, w: string) = From , use regextofsm to construct an FSM M such that L() = L(M). Return decideFSM(M, w). Recall that ndfsmsimulate takes epsilon-closure at every stage, so there is no danger of getting into an infinite loop.

Emptiness Given an FSM M, is L(M) empty? The graph analysis approach: 1. Mark all states that are reachable via some path from the start state of M. 2. If at least one marked state is an accepting state, return False. Else return True. The simulation approach: 1. Let M = ndfsmtodfsm(M). 2. For each string w in * such that |w| < |KM | do: Run decideFSM(M, w). 3. If M accepts at least one such string, return False. Else return True.

Totality Given an FSM M, is L(M) = M*? Construct M to accept L(M). 2. Return emptyFSM(M ).

Finiteness Given an FSM M, is L(M) finite? The graph analysis approach: The simulation approach The mere presence of a loop does not guarantee that L(M) is infinite. The loop might be: labeled only with , unreachable from the start state, or not on a path to an accepting state. 1. M = ndfsmtodfsm(M). 2. M = minDFSM(M). 3. Mark all states in M that are on a path to an accepting state. 4. Considering only marked states, determine whether there are any cycles in M. 5. If there are cycles, return True. Else return False. The simulation approach: M = ndfsmtodfsm(M). 2. For each string w in * such that ________________ do: [answer: |KM |  w  2|KM | - 1 ] Run decideFSM(M, w). 3. If M accepts at least one such string, return False. Else return True.

Equivalence other words, is L(M1) = L(M2)? We can describe two ● Given two FSMs M1 and M2, are they equivalent? In other words, is L(M1) = L(M2)? We can describe two different algorithms for answering this question.

Equivalence other words, is L(M1) = L(M2)? ● Given two FSMs M1 and M2, are they equivalent? In other words, is L(M1) = L(M2)? equalFSMs1(M1: FSM, M2: FSM) = 1. M1 = buildFSMcanonicalform(M1). 2. M2 = buildFSMcanonicalform(M2). 3. If M1 and M2 are equal, return True, else return False.

Equivalence other words, is L(M1) = L(M2)? ● Given two FSMs M1 and M2, are they equivalent? In other words, is L(M1) = L(M2)? Observe that M1 and M2 are equivalent iff: (L(M1) - L(M2))  (L(M2) - L(M1)) = . equalFSMs2(M1: FSM, M2: FSM) = 1. Construct MA to accept L(M1) - L(M2). 2. Construct MB to accept L(M2) - L(M1). 3. Construct MC to accept L(MA)  L(MB). 4. Return emptyFSM(MC).

Minimality ● Given DFSM M, is M minimal? M = minDFSM(M). 2. If |KM| = |KM | return True; else return False.

Context-Free Grammars CFG ≡ BNF (mostly) Chapter 11

Languages and Machines

Rewrite Systems and Grammars A rewrite system (a.k.a. production system or rule-based system) is: ● a list of rules, and ● an algorithm for applying them. Each rule has a left-hand side (lhs) and a right hand side (rhs) Example rules: S  aSb aS   aSb  bSabSa

Simple-rewrite algorithm simple-rewrite(R: rewrite system, w: initial string) = 1. Set working-string to w. 2. Until told by R to halt do: Match the lhs of some rule against some part of working-string. Replace the matched part of working-string with the rhs of the rule that was matched. 3. Return working-string. lhs means "left-hand side" rhs means "right-hand side"

An Example w = SaS Rules: [1] S  aSb [2] aS   ● What order to apply the rules? ● When to quit? Do a couple of examples.

Rule Based Systems ● Expert systems ● Cognitive modeling ● Business practice modeling ● General models of computation ● Grammars

Grammars Define Languages A CFG G=(V, , R, S) (Each part is finite)  is the terminal alphabet; it contains the symbols that make up the strings in L(G), and N is the nonterminal alphabet a set of working symbols that G uses to structure the language. These symbols disappear by the time the grammar finishes its job and generates a string. Note:  ∩ N = . Rule alphabet: V =  ∪ N R: A set of productions of the form A  β, where A ∊ N and β ∊ V*. G has a unique start symbol, S ∊ N

Generating Many Strings Multiple rules may match. Grammar: S  aSb, S  bSa, and S   Derivation so far: S  aSb  aaSbb  Three choices at the next step: S  aSb  aaSbb  aaaSbbb (using rule 1), S  aSb  aaSbb  aabSabb (using rule 2), S  aSb  aaSbb  aabb (using rule 3).

Generating Many Strings One rule may match in more than one way. Grammar: S  aTTb, T  bTa, and T   Derivation so far: S  aTTb  Two choices at the next step: S  aTTb  abTaTb  S  aTTb  aTbTab 

When to Stop May stop when: The working string no longer contains any nonterminal symbols (including, when it is ). In this case, we say that the working string is generated by the grammar. Example: S  aSb  aaSbb  aabb

When to Stop Rules: S  aSb, S  bTa, and S   May stop when: There are nonterminal symbols in the working string but none of them is in a substring that is the left-hand side of any rule in the grammar. In this case, we have a blocked or non-terminated derivation but no generated string. Example: Rules: S  aSb, S  bTa, and S   Derivations: S  aSb  abTab  [blocked]

When to Stop It is possible that neither (1) nor (2) is achieved. Example: G contains only the rules S  Ba and B  bB, with S the start symbol. Then all derivations proceed as: S  Ba  bBa  bbBa  bbbBa  bbbbBa  ...

Context-free Grammars, Languages, and PDAs Context-free Language L Context-free Grammar Accepts PDA

Context-Free Grammars No restrictions on the form of the right hand sides. S  abDeFGab But require single non-terminal on left hand side. S  but not ASB 

anbn Balanced Parentheses language ambn : m>= n Find a grammar that generates each S   S  aSb

Context-Free Grammars A context-free grammar G is a quadruple, (V, , R, S), where: ● V is the rule alphabet, which contains nonterminals and terminals. ●  (the set of terminals) is a subset of V, ● R (the set of rules) is a finite subset of (V - ) V*, ● S (the start symbol) is an element of V - . Example: ({S, a, b}, {a, b}, {S  a S b, S  }, S) Mention N = V -  Rules are also known as productions.