Formal Languages and Automata
Total Page:16
File Type:pdf, Size:1020Kb
Formal Languages and Automata Stephan Schulz & Jan Hladik [email protected] [email protected] L ⌃⇤ ✓ with contributions from David Suendermann 1 Table of Contents Introduction Formal Grammars Lecture-specific material Organisation The Chomsky Lecture 1 Formal languages Hierarchy Lecture 2 overview Right-linear Grammars Lecture 3 Formal language Context-free Lecture 4 basics Grammars Lecture 5 Regular Languages and Push-Down Automata Lecture 6 Finite Automata Properties of Lecture 7 Regular Expressions Context-free Lecture 8 Finite Automata Languages Lecture 9 Non-Determinism Lecture 10 Parsers and Bison Regular Lecture 11 Turing Machines and expressions and Lecture 12 Languages of Type 1 and Finite Automata Lecture 13 0 Minimisation Lecture 14 Turing Machines Equivalence Lecture 15 Unrestricted The Pumping Lemma Lecture 16 Grammars Properties of Regular Lecture 17 Linear Bounded Languages Lecture 18 Scanners and Flex Automata Formal Grammars and Properties of Context-Free Languages Type-0-languages Selected Solutions 2 Outline Formal Grammars and Context-Free Introduction Languages Organisation Parsers and Bison Formal languages overview Formal language basics Turing Machines and Languages of Type 1 and 0 Regular Languages and Finite Automata Lecture-specific material Scanners and Flex Selected Solutions 3 Outline Formal Grammars and Context-Free Introduction Languages Organisation Parsers and Bison Formal languages overview Formal language basics Turing Machines and Languages of Type 1 and 0 Regular Languages and Finite Automata Lecture-specific material Scanners and Flex Selected Solutions 4 Introduction I Stephan Schulz I Dipl.-Inform., U. Kaiserslautern, 1995 I Dr. rer. nat., TU Munchen,¨ 2000 I Visiting professor, U. Miami, 2002 I Visiting professor, U. West Indies, 2005 I Lecturer (Hildesheim, Offenburg, . ) since 2009 I Industry experience: Building Air Traffic Control systems I System engineer, 2005 I Project manager, 2007 I Product Manager, 2013 I Professor, DHBW Stuttgart, 2014 Research: Logic & Automated Reasoning 5 Introduction I Jan Hladik I Dipl.-Inform.: RWTH Aachen, 2001 I Dr. rer. nat.: TU Dresden, 2007 I Industry experience: SAP Research I Work in publicly funded research projects I Collaboration with SAP product groups I Supervision of Bachelor, Master, and PhD students I Professor: DHBW Stuttgart, 2014 Research: Semantic Web, Semantic Technologies, Automated Reasoning 6 Literature I Scripts I The most up-to-date version of this document as well as auxiliary material will be made available online at http://wwwlehre.dhbw-stuttgart.de/ ˜sschulz/fla2017.html and http://wwwlehre.dhbw-stuttgart.de/ ˜hladik/FLA I Books I John E. Hopcroft, Rajeev Motwani, Jeffrey D. Ullman: Introduction to Automata Theory, Languages, and Computation I Michael Sipser: Introduction to the Theory of Computation I Dirk W. Hoffmann: Theoretische Informatik I Ulrich Hedtstuck:¨ Einfuhrung¨ in die theoretische Informatik 7 Computing Environment I For practical exercises, you will need a complete Linux/UNIX environment. If you do not run one natively, there are several options: I You can install VirtualBox (https://www.virtualbox.org) and then install e.g. Ubuntu (http://www.ubuntu.com/) on a virtual machine I For Windows, you can install the complete UNIX emulation package Cygwin from http://cygwin.com I For MacOS, you can install fink (http://fink.sourceforge.net/) or MacPorts (https://www.macports.org/) and the necessary tools I You will need at least flex, bison, gcc, grep, sed, AWK, make, and a good text editor 8 Outline of the Lecture Introduction Right-linear Grammars Lecture 1 Organisation Context-free Lecture 2 Formal languages Grammars Lecture 3 overview Push-Down Automata Lecture 4 Formal language Properties of Lecture 5 basics Context-free Lecture 6 Regular Languages and Languages Lecture 7 Finite Automata Parsers and Bison Lecture 8 Regular Expressions Turing Machines and Lecture 9 Finite Automata Languages of Type 1 and Lecture 10 The Pumping Lemma 0 Lecture 11 Properties of Regular Turing Machines Lecture 12 Languages Unrestricted Lecture 13 Scanners and Flex Grammars Lecture 14 Formal Grammars and Linear Bounded Lecture 15 Context-Free Languages Automata Lecture 16 Formal Grammars Properties of Lecture 17 The Chomsky Type-0-languages Lecture 18 Hierarchy Lecture-specific material Selected Solutions 9 Outline Formal Grammars and Context-Free Introduction Languages Organisation Parsers and Bison Formal languages overview Formal language basics Turing Machines and Languages of Type 1 and 0 Regular Languages and Finite Automata Lecture-specific material Scanners and Flex Selected Solutions 10 Formal language concepts Alphabet: finite set Σ of symbols (characters) a; b; c I f g Word: finite sequence w of characters (string) ab = ba I 6 Language: (possibly infinite) set L of words ab; ba = ba; ab I f g f g Formal: L defined precisely I opposed to natural languages, where there are borderline cases 11 Some formal languages Example I names in a phone directory I phone numbers in a phone directory I legal C identifiers I legal C programs I legal HTML 4.01 Transitional documents I empty set I ASCII strings I Unicode strings More? 12 Language classes This course: four classes of different complexity and expressivity 1 regular languages: limited power, but easy to handle I “strings that start with a letter, followed by up to 7 letters or digits” I legal C identifiers I phone numbers 2 context-free languages: more expressive, but still feasible I “every <token> is matched by </token>” I nested dependencies I (most aspects of) legal C programs I many natural languages (English, German) Jan says that we Jan sagt, dass wir let die Kinder the children dem Hans help das Haus Hans anstreichen paint helfen the house ließen 13 Language classes (cont’) 3 context-sensitive languages: even more expressive, difficult to handle computationally I “every variable has to be declared before it is used” (arbitrary sequence, arbitrary amounts of code in between) I cross-serial dependencies I (remaining aspects of) legal C programs I most remaining natural languages (Swiss German) Jan sait¨ das mer Jan says that we d’chind the children em Hans Hans es huus the house lond¨ let helfe help aastriche paint 4 recursively enumerable languages: most general (Chomsky) class; undecidable I all (valid) mathematical theorems (in first-order logic) I programs terminating on a particular input 14 Automata I abstract formal machine model, characterised by states, letters, transitions, and external memory I accept words For every language class discussed in this course, a machine model exists such that for every language L there is an automaton (L) that accepts exactly the words in L. A regular finite automaton/finite state machine context-free ; pushdown automaton context-sensitive ; linearly bounded Turing machine recursively enumerable ; (unbounded) Turing machine ; 15 Example: Finite Automaton Initial state Transitions click Off On click States 16 Example: Finite Automaton Formally: Initial state Transitions Q = Off; On is the set of states I f g Σ = click is the alphabet I f g click I The transition function δ is given by δ click Off On Off On click On Off I The initial state is Off I There are no accepting states States 17 ATC scenario Theoretische Grundlagen des Software Engineering Stephan Schulz • Aggregator ATC Center (controllers) • 3 18 ATC redundancy Theoretische Grundlagen des Software Engineering Stephan Schulz • Aktive server: - Accepts sensor data - Provides ASP ATC - Sends “alive” messages Passive server - Ignores sensor data Ser- Ser- - Monitors “alive” messages ver ver - Takes over in case of failure A B Sensors • 4 19 Theoretische Grundlagen des Software Engineering Stephan Schulz •Finite automaton to the rescue Zustandsdiagramm Zwei Eingaben (“Buchstaben”)! q0 ‣ timeout: 0.1 Sekunden sind timeout vergangen! I Two events (“letters”) alive 1 q I timeout‣ alive:: 0.1Andere seconds Server have passedist aktiv! timeout alive: message from active server I q , q , q : Server ist passiv! States‣ q 0q 1q :2 Server is passive alive q2 I 0; 1; 2 No processing of input timeout I • Keine Verarbeitung, keine I No sendingalives of! alive messages alive q3 I State q3: Server becomes active I Process‣ Wenn input, q3 erreicht provide output wird: to! ATC I Send alive messages every 0.1 timeout seconds• Übername als aktiver Server (schicke alives) • 5 20 Exercise: Finite automaton b d q i q g 2 3 n e n q0 q1 q4 Does this automaton accept the words begin; end; bind; bend? 21 Turing Machine “Universal computer” I Very simple model of a computer I Infinite tape, one read/write head I Tape can store letters from a alphabet I Finite automaton controls read/write and movement operations I Very powerful model of a computer I Can compute anything any real computer can compute I Can compute anything an “ideal” real computer can compute I Can compute everything a human can compute (?) 22 Formal grammars Formalism to generate (rather than accept) words over alphabet terminal symbols: may appear in the produced word (alphabet) non-terminal symbols: may not appear in the produced word (temporary symbols) production rules: l r means: l can be replaced by r anywhere in! the word Example Grammar for arithmetic expressions over 0; 1 f g Σ = 0; 1; +; ; (; ) N = fE · g P = fEg 0; E 1; fE ! (E) ! E ! E + E E ! E E ! · g 23 Exercise: Grammars Using I the non-terminal symbol S I the terminal symbols b; d; e; g; i; n I the production rules S begin; beg e; in ind; in n; eg