Theory of Programming Languages COMP360

Similar documents
Specifying Syntax COMP360

Computer Sciences Department

Theory and Compiling COMP360

Theory of Languages and Automata

Theory of Computations Spring 2016 Practice Final Exam Solutions

Theory of Computations Spring 2016 Practice Final

Theory of Computation, Homework 3 Sample Solution

(a) R=01[((10)*+111)*+0]*1 (b) ((01+10)*00)*. [8+8] 4. (a) Find the left most and right most derivations for the word abba in the grammar

Decidable Problems. We examine the problems for which there is an algorithm.

Final Course Review. Reading: Chapters 1-9

CISC 4090 Theory of Computation

TAFL 1 (ECS-403) Unit- V. 5.1 Turing Machine. 5.2 TM as computer of Integer Function

Turing Machine Languages

Lexical Scanning COMP360

A Characterization of the Chomsky Hierarchy by String Turing Machines

Turing Machines. A transducer is a finite state machine (FST) whose output is a string and not just accept or reject.

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2016

The Big Picture. Chapter 3

1. [5 points each] True or False. If the question is currently open, write O or Open.

Universal Turing Machine Chomsky Hierarchy Decidability Reducibility Uncomputable Functions Rice s Theorem Decidability Continued

COMP Logic for Computer Scientists. Lecture 25

Introduction to Computers & Programming

1 Parsing (25 pts, 5 each)

Problems, Languages, Machines, Computability, Complexity

ONE-STACK AUTOMATA AS ACCEPTORS OF CONTEXT-FREE LANGUAGES *

We can create PDAs with multiple stacks. At each step we look at the current state, the current input symbol, and the top of each stack.

CSCI312 Principles of Programming Languages!

CS402 - Theory of Automata Glossary By

1. Draw the state graphs for the finite automata which accept sets of strings composed of zeros and ones which:

Formal languages and computation models

Enumerations and Turing Machines

Languages and Compilers

CS21 Decidability and Tractability

CT32 COMPUTER NETWORKS DEC 2015

Outline. Language Hierarchy

ECS 120 Lesson 16 Turing Machines, Pt. 2

Finite Automata. Dr. Nadeem Akhtar. Assistant Professor Department of Computer Science & IT The Islamia University of Bahawalpur

MATH Iris Loeb.

Structure of a Compiler: Scanner reads a source, character by character, extracting lexemes that are then represented by tokens.

Limitations of Algorithmic Solvability In this Chapter we investigate the power of algorithms to solve problems Some can be solved algorithmically and

Last lecture CMSC330. This lecture. Finite Automata: States. Finite Automata. Implementing Regular Expressions. Languages. Regular expressions

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

Equivalence of NTMs and TMs

Formal Languages. Formal Languages

CSCI 270: Introduction to Algorithms and Theory of Computing Fall 2017 Prof: Leonard Adleman Scribe: Joseph Bebel

CSE 105 THEORY OF COMPUTATION

Midterm Exam II CIS 341: Foundations of Computer Science II Spring 2006, day section Prof. Marvin K. Nakayama

Theory Bridge Exam Example Questions Version of June 6, 2008

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

Languages and Automata

Lecture 5: The Halting Problem. Michael Beeson

Automata & languages. A primer on the Theory of Computation. The imitation game (2014) Benedict Cumberbatch Alan Turing ( ) Laurent Vanbever

14.1 Encoding for different models of computation

Compiler Design. 2. Regular Expressions & Finite State Automata (FSA) Kanat Bolazar January 21, 2010

Languages and Models of Computation

Regular Expressions & Automata

R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

Decision Properties for Context-free Languages

pp Variants of Turing Machines (Sec. 3.2)

Multiple Choice Questions

Lecture 18: Turing Machines

Interpreter. Scanner. Parser. Tree Walker. read. request token. send token. send AST I/O. Console

Material from Recitation 1

Lexical Analysis - 2

Introduction to Automata Theory. BİL405 - Automata Theory and Formal Languages 1

THE HALTING PROBLEM. Joshua Eckroth Chautauqua Nov

I have read and understand all of the instructions below, and I will obey the Academic Honor Code.

Chapter 11 :: Functional Languages

CS 275 Automata and Formal Language Theory. First Problem of URMs. (a) Definition of the Turing Machine. III.3 (a) Definition of the Turing Machine

6.045J/18.400J: Automata, Computability and Complexity. Practice Quiz 2

CS 441G Fall 2018 Exam 1 Matching: LETTER

Formal Grammars and Abstract Machines. Sahar Al Seesi

Presented By : Abhinav Aggarwal CSI-IDD, V th yr Indian Institute of Technology Roorkee. Joint work with: Prof. Padam Kumar

JNTUWORLD. Code No: R

Chapter 14: Pushdown Automata

Name: CS 341 Practice Final Exam. 1 a 20 b 20 c 20 d 20 e 20 f 20 g Total 207

Theory of Computation

CSE 105 THEORY OF COMPUTATION

Assignment 4 CSE 517: Natural Language Processing

Final Exam 1, CS154. April 21, 2010

THEORY OF COMPUTATION

MAT 7003 : Mathematical Foundations. (for Software Engineering) J Paul Gibson, A207.

T.E. (Computer Engineering) (Semester I) Examination, 2013 THEORY OF COMPUTATION (2008 Course)

ECS 120 Lesson 7 Regular Expressions, Pt. 1

Recursively Enumerable Languages, Turing Machines, and Decidability

The Turing Machine. Unsolvable Problems. Undecidability. The Church-Turing Thesis (1936) Decision Problem. Decision Problems

Regular Languages (14 points) Solution: Problem 1 (6 points) Minimize the following automaton M. Show that the resulting DFA is minimal.

(Refer Slide Time: 0:19)

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

Source of Slides: Introduction to Automata Theory, Languages, and Computation By John E. Hopcroft, Rajeev Motwani and Jeffrey D.

05. Turing Machines and Spacetime. I. Turing Machines and Classical Computability.

CMSC 132: Object-Oriented Programming II

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

Theory of Computation p.1/?? Theory of Computation p.2/?? A Turing machine can do everything that any computing

Actually talking about Turing machines this time

Variants of Turing Machines

Introduction to Lexing and Parsing

Languages, Automata, Regular Expressions & Scanners. Winter /8/ Hal Perkins & UW CSE B-1

A Note on the Succinctness of Descriptions of Deterministic Languages

Closure Properties of CFLs; Introducing TMs. CS154 Chris Pollett Apr 9, 2007.

Transcription:

Theory of Programming Languages COMP360

Sometimes it is the people no one imagines anything of, who do the things that no one can imagine Alan Turing

What can be computed? Before people even built computers, there was considerable work on the theory of computing The concepts of computing and accepting a string that is a member of a language are almost the same

History In the seventeenth century Gottfried Leibniz formulated the Entscheidungsproblem or decision problem In 1928, mathematician David Hilbert published a paper on the decision problem, what can be computed In 1936, Alonzo Church and Alan Turing published independent papers showing that a general solution to the Entscheidungsproblem is impossible

Alan Turing Alan Turing was a British mathematician He invented the Turing machine According to the Church Turing thesis, Turing machines are capable of computing anything that is computable

Noam Chomsky Noam Chomsky is an American linguist In the 1956 he developed a theory on the classes of formal grammars The Chomsky hierarchy defines a several classes of languages, each embedded in the next Chomsky has also been known for his activism

Chomsky Hierarchy There are four classes of languages Each class is a subset of another class Regular languages are the simplest while recursively enumerable (RE) languages are the most complex

Theoretical Computers Each class of languages can be recognized by a type of abstract machine The classes can be defined by those languages that can be recognized by the machine for that class

Languages and Machines Language Regular Expressions Context Free Context Sensitive Recursively Enumerable Machine Deterministic Finite State Automata (DFA) Push Down Automata (PDA) Bounded Turing Machine Turing Machine

Click the correct choice A. All regular languages are context free B. All context free languages are regular C. No regular languages are context free D. No context free languages are regular E. All Recursively Enumerable languages are regular

Finite state automaton A finite state automaton (FSA) is a graph with directed labeled arcs, two types of nodes (final and non-final state), and a unique start state: This is also called a state machine The language accepted by machine M is set of strings that move from start node to a final node Programming Language design and Implementation -4th Edition Copyright Prentice Hall, 2000

FSA Execution The FSA has a current state which starts at the start state The input string is examined left to right one character at a time Based on the current input character, the FSA can move from the current state to a new state following an arrow labeled with the current input character Execution ends at the end of the input or if the FSA cannot move

More on FSAs An FSA can have more than one final state: Programming Language design and Implementation -4th Edition Copyright Prentice Hall, 2000

Example FSA This FSA recognizes a Java or C++ character string Remember that \ is an escape sequence that allows you to put a quote character in a string

Try It Write a DFA to recognize /* */ style comments

Deterministic FSAs Deterministic FSA: For each state and for each input symbol, there is exactly one transition Non-deterministic FSA (NDFSA): Remove this restriction At each node there is 0, 1, or more than one transition for each alphabet symbol A string is accepted if there is some path from the start state to some final state Example nondeterministic FSA (NDFSA): 01 is accepted via path: ABD even though 01 also can take the paths: ACC or ABC and C is not a final state Programming Language design and Implementation -4th Edition Copyright Prentice Hall, 2000

Equivalence of FSA and NDFSA Important early result: NDFSA = DFSA Let subsets of states be states in DFSA Keep track of which subset you can be in Any string from {A} to either {D} or {CD} represents a path from A to D in the original NDFSA. Programming Language design and Implementation -4th Edition Copyright Prentice Hall, 2000

Try It Write a DFA to recognize // style comments You can use \n as new line or end of line

Regular expressions Can write regular language as an expression: 0*11*(0 100*1)1* 0*11*1 Operators: Concatenation (adjacency) OR written as Kleene closure (* is 0 or more instances) Repeat one or more is written as superscript plus + Programming Language design and Implementation -4th Edition Copyright Prentice Hall, 2000

FSA and Regular Languages An FSA can recognize any regular language Regular languages are often used for pattern matching or searches A popular tool for searching files for a word is grep, General Regular Expression Parser

Try It Write a DFA to recognize both // style and /* */ style comments

Equivalence of FSA and regular grammars Programming Language design and Implementation -4th Edition Copyright Prentice Hall, 2000

Why do we care about regular languages? Programs are composed of tokens: Identifier Number Keyword Special symbols Each of these can be defined by regular grammars Programming Language design and Implementation -4th Edition Copyright Prentice Hall, 2000

Compiling There are several steps in compiling a program The first step is usually for a FSA program to read the program source code and create a list of tokens The list of tokens is then used by the following context free parser

FSA Limits The only memory that a FSA has is the current state As a FSA inputs symbols, it moves to different states An FSA cannot count or compare an arbitrary number of symbols

Push Down Automata A Push Down Automata (PDA) has a FSA and a stack memory The top of the stack, input symbol and FSA state determine what a PDA will do The input is examined one character at a time A PDA can: Push a new symbol on the stack Pop the top symbol from the stack Change the FSA state

Push Down Automata Languages A Push Down Automata (PDA) can recognize context free grammars Most programming languages are context free grammars All regular languages are a subset of context free grammars A PDA can count things

PDA Operation A PDA looks at the input character, the top of stack character and FSA state then makes a decision It can push or pop the stack and change FSA state Like a FSA, it accepts the input if it ends in an accepting state It is a FSA with limited memory picture from Wikipedia

Turing Machine A Turing Machine (TM) has a FSA and an infinite tape The input is on the tape TM can take an action based on the current symbol on the tape and the current FSA state Overwrite the symbol on the tape Move left or right Change the FSA sate

Bounded Turing Machine A Bounded Turing Machine (BTM) has a limited tape A BTM can recognize a context-sensitive grammar (CSG)

Classical Languages Recognized Machine Deterministic Finite State Automata (DFA) Push Down Automata (PDA) Bounded Turing Machine Turing Machine Language Regular expressions a n b n a n b n c n Anything that can be recognized

Most Powerful A Turing Machine can compute anything that any computer can compute If something cannot be computed on a Turing Machine, it cannot be computed with any computer

Beyond Turing Machines There are some problems that cannot always be solved by a Turing Machine or any other computer These are non-computable problems

What is a Non-Computable Problem? Non-Computable Problems are well defined problems that are impossible to solve for all cases, by any algorithm These problems are provably non-computable. They cannot be solved with a faster computer or a better algorithm The best known undecidable problem is the Halting Problem

Halting Problem Can you write a program that inputs another program and some data to determine if the other program will eventually terminate (halt) when using the data? Proof by contradiction: Assume we have such a program (called Q) figure from Algorithmics, The Spirit of Computing, 2 nd ed. by David Harel

Create program S calling program Q as a method. If Q answers Yes, go into an infinite loop Halting Problem Proof figure from Algorithmics, The Spirit of Computing, 2 nd ed. by David Harel

Call program S using S as the input If Q says S halts, then it doesn t If Q says S does not halt, then it does This is a contradiction Therefore Q cannot exist Halting Problem Proof figure from Algorithmics, The Spirit of Computing, 2 nd ed. by David Harel

Sometimes but not Always You can frequently solve a problem known to be noncomputable The simple Hello World program will halt while (true) {} never halts While you may be able to solve specific instances of the problem, you cannot write a program to always solve the problem

Non-Computable Problems There are a number of problems that have been proved non-computable including: Post Correspondence Problem Wang Tiles Domino Snakes

Post correspondence problem Given a set of domino-like tiles with numbers on the top and bottom, can a series of tiles be placed side by side so that the string of numbers on the top matches the string of numbers on the bottom Assume you have an infinite number of each type of domino 100 0 1 1 100 00

Solution to Example Simple solution to this instance of the problem requiring seven dominos 100 1 100 100 1 0 0 1 00 1 1 00 100 100

More Difficult Problem If you change the bottom number on the last tile so that it only contains one zero, the problem becomes much more difficult This simple change has a solution, but it requires 75 dominos 100 0 1 1 100 0

Wang tiles When using a set of square tiles with colored edges, can the tiles be arranged without rotation or reflection so that they tile a plane with adjacent tiles having edges of the same color? Assume you have an infinite supply of each tile

Tiling Solution

Periodic and Aperiodic The previous solution has a 2 by 4 block that can be repeated forever Some sets of tiles can tile an infinite plane without repeating

Aperiodic Tiling

Domino Snakes Using tiles similar to Wang Tiles, can a given set of tiles form a path from two given points so that all adjacent tile edges have matching colors?

Snake Solution