CSE450. Translation of Programming Languages. Automata, Simple Language Design Principles

Similar documents
CSE450. Translation of Programming Languages. Lecture 20: Automata and Regular Expressions

Implementation of Lexical Analysis

Implementation of Lexical Analysis

Implementation of Lexical Analysis

Kinder, Gentler Nation

Lexical Analysis. Finite Automata. (Part 2 of 2)

Lexical Analysis. Chapter 2

Introduction to Lexical Analysis

Lexical Analysis. Lecture 2-4

Chapter 3 Lexical Analysis

Compiler course. Chapter 3 Lexical Analysis

Implementation of Lexical Analysis. Lecture 4

Lexical Analysis. Lecture 3-4

Lexical Analysis. Implementation: Finite Automata

Implementation of Lexical Analysis

Implementation of Lexical Analysis

Implementation of Lexical Analysis

CS 432 Fall Mike Lam, Professor. Finite Automata Conversions and Lexing

Administrivia. Lexical Analysis. Lecture 2-4. Outline. The Structure of a Compiler. Informal sketch of lexical analysis. Issues in lexical analysis

6 NFA and Regular Expressions

Formal Languages and Compilers Lecture IV: Regular Languages and Finite. Finite Automata

Automating Construction of Lexers

Finite automata. We have looked at using Lex to build a scanner on the basis of regular expressions.

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

(Refer Slide Time: 0:19)

Lexical Analysis - 2

Regular Languages. MACM 300 Formal Languages and Automata. Formal Languages: Recap. Regular Languages

Front End: Lexical Analysis. The Structure of a Compiler

Formal Languages and Compilers Lecture VI: Lexical Analysis

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

lec3:nondeterministic finite state automata

ECS 120 Lesson 7 Regular Expressions, Pt. 1

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

2010: Compilers REVIEW: REGULAR EXPRESSIONS HOW TO USE REGULAR EXPRESSIONS

Formal Definition of Computation. Formal Definition of Computation p.1/28

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

Dr. D.M. Akbar Hussain

Lexical Analysis. Prof. James L. Frankel Harvard University

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

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

UNIT -2 LEXICAL ANALYSIS

Converting a DFA to a Regular Expression JP

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

CS2 Language Processing note 3

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Writing a Lexical Analyzer in Haskell (part II)

Regular Languages and Regular Expressions

1. (10 points) Draw the state diagram of the DFA that recognizes the language over Σ = {0, 1}

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Lexical Error Recovery

Decision Properties of RLs & Automaton Minimization

Derivations of a CFG. MACM 300 Formal Languages and Automata. Context-free Grammars. Derivations and parse trees

Monday, August 26, 13. Scanners

Wednesday, September 3, 14. Scanners

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

David Griol Barres Computer Science Department Carlos III University of Madrid Leganés (Spain)

Concepts Introduced in Chapter 3. Lexical Analysis. Lexical Analysis Terms. Attributes for Tokens

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

Compiler Construction

Scanners. Xiaokang Qiu Purdue University. August 24, ECE 468 Adapted from Kulkarni 2012

Zhizheng Zhang. Southeast University

2. Lexical Analysis! Prof. O. Nierstrasz!

CS5371 Theory of Computation. Lecture 8: Automata Theory VI (PDA, PDA = CFG)

CSE 413 Programming Languages & Implementation. Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions

CT32 COMPUTER NETWORKS DEC 2015

1 Parsing (25 pts, 5 each)

Compiler phases. Non-tokens

Midterm I (Solutions) CS164, Spring 2002

Formal Languages and Automata

CS415 Compilers. Lexical Analysis

Lexical Analyzer Scanner

Lexical Analyzer Scanner

CSE 413 Programming Languages & Implementation. Hal Perkins Winter 2019 Grammars, Scanners & Regular Expressions

Lecture 3: Lexical Analysis

Compiler Construction

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Finite Automata and Scanners

Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata

CS308 Compiler Principles Lexical Analyzer Li Jiang

Regular Expression Constrained Sequence Alignment

Introduction to Lexing and Parsing

CSE302: Compiler Design

Structure of Programming Languages Lecture 3

Administrativia. Extra credit for bugs in project assignments. Building a Scanner. CS164, Fall Recall: The Structure of a Compiler

Definition 2.8: A CFG is in Chomsky normal form if every rule. only appear on the left-hand side, we allow the rule S ǫ.

CMSC 350: COMPILER DESIGN

Principles of Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Computer Sciences Department

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

G52LAC Languages and Computation Lecture 6

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

CS 310: State Transition Diagrams

DFA: Automata where the next state is uniquely given by the current state and the current input character.

CSE Lecture 4: Scanning and parsing 28 Jan Nate Nystrom University of Texas at Arlington

Behaviour Diagrams UML

CS402 Theory of Automata Solved Subjective From Midterm Papers. MIDTERM SPRING 2012 CS402 Theory of Automata

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

Chapter 3: Lexing and Parsing

CSc 453 Lexical Analysis (Scanning)

CSE 105 THEORY OF COMPUTATION

Transcription:

CSE45 Translation of Programming Languages Automata, Simple Language Design Principles

Finite Automata State Graphs A state: The start state: An accepting state: A transition: a

A Simple Example A finite automaton that accepts only "":

Another Simple Example Alphabet: {, } A finite automaton accepting any number of 's followed by a single

And Another Example Alphabet: {, } What language does this recognize?

Another Simple Example Alphabet still {, } The operation of the automaton is not completely defined by the input On input "" the automaton could be in either state

Epsilon Moves Another kind of transition: -moves

Deterministic and Non-deterministic Automata Deterministic Finite Automata (DFA) One transition per input per state No -moves Non-deterministic Finite Automata (NFA) Can have multiple transitions for one input in a given state Can have -moves Finite automata have finite memory Need only to encode the current state

Execution of Finite Automata A DFA can take only one path through the state graph Completely determined by the input NFSs can choose Whether to make -moves Which of multiple transitions for a single input to take

Acceptance of NFAs An NFA can get into multiple states Input: Rule: NFA accepts if it can get in a final state.

NFA vs. DFA () NFAs and DFAs recognize the same set of languages (regular languages) NFAs are easier to design You can just list all the rules you want to allow DFAs are easier to implement There are no choices to consider

NFA vs. DFA (2) For a given language the NFA can be simpler than the DFA NFA DFA DFA can be exponentially larger than the NFA

Lexical Analysis Overview Non-deterministic Finite Automata Regular Expressions Deterministic Finite Automata Lexical Specification Table-driven Implementation of DFA

Regular Expressions to NFA () For each kind of regular expression, we can define an NFA. We wil do this by using recursive definition. Notation: NFA for regular expression A: A Base cases: For For input 'a' a

Regular Expressions to NFA (2) For AB A B For A B A B

Regular Expressions to NFA (3) For A* A How do we do "A+"? What about "A?"?

Example of RE to NFA Conversion Consider the regular expression ( )* The NFA is: A B C E D F G H I J

Lexical Analysis Overview Non-deterministic Finite Automata Regular Expressions Deterministic Finite Automata Lexical Specification Table-driven Implementation of DFA

NFA to DFA: The Trick Simulate the NFA Each state of DFA = a non-empty subset of states of the NFA Start state = the set of NFA states reachable through -moves from the NFA start state Add a transition (S, a) S' to DFA if and only if: S' is the set of NFA states reachable from the states in S after seeing the input a (remember to consider -moves as well!)

NFA to DFA Example A B C E D F G H I J FGABCDHI ABCDHI EJGABCDHI

NFA to DFA Remark An NFA may be in many states at any time How many different possible states are there? If there are n states, the NFA must be in some subset of those n states How many non-empty subsets are there? 2 n - = finitely many

Lexical Analysis Overview Non-deterministic Finite Automata Regular Expressions Deterministic Finite Automata Lexical Specification Table-driven Implementation of DFA

Table Implementation A DFA can be implemented by a 2-dimensional table T One dimension is "states" Other dimension is "input symbols" For every transition (Si, a) Sk define T[i, a] = k DFA "execution" If in state Si and input a, read T[i, a] = k and go to state Sk Very efficient

Table Implementation of DFA T S U S T U T T U U T U

Final notes about Implementation NFA to DFA conversion is at the heart of tools such as Lex But, DFAs can be huge In practice, lex-like tools trade off speed for space in the choice of NFA and DFA representations

Simple Language Design Principles

Notes on Language Design In 986, Jon Bentley wrote a Programming Pearls column in Communications of the ACM about "Little Languages". In it, he lists seven criteria for designing a little language.. Orthogonality: Different statements in the language should each have their own purpose. 2. Generality: Each command in the language should be as versatile as possible as long as it does not make it much more complex.

3. Parsimony: Do not use more commands in your language that it really needs to have. 4. Completeness: Make sure your language can do everything you need it to. 5. Similarity: Use language components that the user will understand. 6. Extensibility: Your language should be easy to add new commands and features to. 7. Openness: You should be able to interface your language with other available languages.