CS314 – Section 5 Recitation 2

Slides:



Advertisements
Similar presentations
CSE 311 Foundations of Computing I
Advertisements

Finite Automata CPSC 388 Ellen Walker Hiram College.
COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou.
Regular Expressions Finite State Automaton. Programming Languages2 Regular expressions  Terminology on Formal languages: –alphabet : a finite set of.
Lexical Analysis - Scanner Computer Science Rensselaer Polytechnic Compiler Design Lecture 2.
NFAs Sipser 1.2 (pages 47–54). CS 311 Fall Recall… Last time we showed that the class of regular languages is closed under: –Complement –Union.
1 Introduction to Computability Theory Lecture4: Regular Expressions Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
1 Introduction to Computability Theory Discussion1: Non-Deterministic Finite Automatons Prof. Amos Israeli.
Lecture 3UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 3.
Lexical Analysis III Recognizing Tokens Lecture 4 CS 4318/5331 Apan Qasem Texas State University Spring 2015.
CS5371 Theory of Computation
FSA Lecture 1 Finite State Machines. Creating a Automaton  Given a language L over an alphabet , design a deterministic finite automaton (DFA) M such.
Topics Automata Theory Grammars and Languages Complexities
1.Defs. a)Finite Automaton: A Finite Automaton ( FA ) has finite set of ‘states’ ( Q={q 0, q 1, q 2, ….. ) and its ‘control’ moves from state to state.
CPSC 388 – Compiler Design and Construction
Regular Languages A language is regular over  if it can be built from ;, {  }, and { a } for every a 2 , using operators union ( [ ), concatenation.
Topic #3: Lexical Analysis
Thopson NFA Presenter: Yuen-Shuo Li Date: 2014/5/7 Department of Computer Science and Information Engineering National Cheng Kung University, Taiwan R.O.C.
Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Machine-independent code improvement Target code generation Machine-specific.
어휘분석 (Lexical Analysis). Overview Main task: to read input characters and group them into “ tokens. ” Secondary tasks: –Skip comments and whitespace;
1 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language.
Lecture # 3 Chapter #3: Lexical Analysis. Role of Lexical Analyzer It is the first phase of compiler Its main task is to read the input characters and.
Automating Construction of Lexers. Example in javacc TOKEN: { ( | | "_")* > | ( )* > | } SKIP: { " " | "\n" | "\t" } --> get automatically generated code.
Lexical Analysis I Specifying Tokens Lecture 2 CS 4318/5531 Spring 2010 Apan Qasem Texas State University *some slides adopted from Cooper and Torczon.
Lexical Analyzer (Checker)
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
COMP3190: Principle of Programming Languages DFA and its equivalent, scanner.
What is a language? An alphabet is a well defined set of characters. The character ∑ is typically used to represent an alphabet. A string : a finite.
Review: Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Symbol.
Finite Automata – Definition and Examples Lecture 6 Section 1.1 Mon, Sep 3, 2007.
CMSC 330: Organization of Programming Languages Theory of Regular Expressions Finite Automata.
Exercise 1 Consider a language with the following tokens and token classes: ID ::= letter (letter|digit)* LT ::= " " shiftL ::= " >" dot ::= "." LP ::=
Exercise Solution for Exercise (a) {1,2} {3,4} a b {6} a {5,6,1} {6,2} {4} {3} {5,6} { } b a b a a b b a a b a,b b b a.
UNIT - I Formal Language and Regular Expressions: Languages Definition regular expressions Regular sets identity rules. Finite Automata: DFA NFA NFA with.
using Deterministic Finite Automata & Nondeterministic Finite Automata
Overview of Previous Lesson(s) Over View  A token is a pair consisting of a token name and an optional attribute value.  A pattern is a description.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
Set, Alphabets, Strings, and Languages. The regular languages. Clouser properties of regular sets. Finite State Automata. Types of Finite State Automata.
Deterministic Finite Automata Nondeterministic Finite Automata.
CS412/413 Introduction to Compilers Radu Rugina Lecture 3: Finite Automata 25 Jan 02.
Lecture 2 Compiler Design Lexical Analysis By lecturer Noor Dhia
1 Regular grammars Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Department of Software & Media Technology
Topic 3: Automata Theory 1. OutlineOutline Finite state machine, Regular expressions, DFA, NDFA, and their equivalence, Grammars and Chomsky hierarchy.
Deterministic Finite-State Machine (or Deterministic Finite Automaton) A DFA is a 5-tuple, (S, Σ, T, s, A), consisting of: S: a finite set of states Σ:
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
CIS Automata and Formal Languages – Pei Wang
Finite automate.
CS510 Compiler Lecture 2.
Lecture 2 Lexical Analysis
Lexical analysis Finite Automata
CS314 – Section 5 Recitation 1
CS314 – Section 5 Recitation 3
Deterministic Finite Automata
Two issues in lexical analysis
Finite Automata & Regular Languages
Deterministic Finite Automata
REGULAR LANGUAGES AND REGULAR GRAMMARS
Chapter 7 Regular Grammars
Review: Compiler Phases:
COSC 3340: Introduction to Theory of Computation
Regular languages, regular expressions, & finite automata (intro)
CSE 311: Foundations of Computing
Finite Automata.
4b Lexical analysis Finite Automata
Compiler Construction
4b Lexical analysis Finite Automata
CSE 311 Foundations of Computing I
Lecture 5 Scanning.
Presentation transcript:

CS314 – Section 5 Recitation 2 Long Zhao (lz311@rutgers.edu) Regular Expressions DFA, NFA Homework 1 Slides available at http://www.ilab.rutgers.edu/~lz311/

Regular expressions Token: shortest string of characters with individual meaning. E.g. keywords, identifier, constant, operator, … Language has few to hundreds of tokens. C language: keyword (int, float, return), identifier (var1, func), constant (0x01, 0.01), operator (+, -, …) Regular expression: a pattern used to specify a set of strings required for a particular purpose.

Regular expressions (From Scott book) A regular expression is one of 1. A character 2. The empty string, denoted ϵ 3. Two regular expressions next to each other, meaning any string generated by the first one followed by (concatenated with) any string generated by the second one 4. Two regular expressions separated by a vertical bar ( | ), meaning any string generated by the first one or any string generated by the second one 5. A regular expression followed by a Kleene star, meaning the concatenation of zero or more strings generated by the expression in front of the star

Regex exercises Find regex representing the language Language Regex {0} {0,1} {0,01} {0, ϵ}{001} {1}*{10} {10,11,1100}*

Regex exercises Find regex representing the language Language Regex {0} {0,1} 0|1 {0,01} 0|01 {0, ϵ}{001} (0|ϵ)001 {1}*{10} 1*10 {10,11,1100}* (10|11|1100)*

Regex exercises Construct a regular expression for binary numbers of length two. Construct a regular expression for binary numbers with even length. Find regular expressions over {0, 1} that determine the language which consists of all strings with even number of 1s.

Regex exercises (0|1)(0|1) or (00|01|10|11) Construct a regular expression for binary numbers of length two. (0|1)(0|1) or (00|01|10|11)

Regex exercises ((0|1)(0|1))* or (00|01|10|11) * Construct a regular expression for binary numbers with even length. ((0|1)(0|1))* or (00|01|10|11) *

Regex exercises What would be the regular expression corresponding to the language which consists of all strings of 0s and 1s that have odd length?

Regex exercises What would be the regular expression corresponding to the language which consists of all strings of 0s and 1s that have odd length? Based on the previous example, we can add only 1 digit to the front or the end of the string. Thus, the regular expression can be (0|1)(00|01|10|11)* or (00|01|10|11)*(0|1).

Regex exercises (0*10*10*)* Find regular expressions over {0, 1} that determine the language which consists of all strings with even number of 1s. (0*10*10*)*

Regex exercises Find regular expressions over {0, 1} that determine the language which consists of all strings with odd number of 0s.

Regex exercises (1*01*)(1*01*01*)* Find regular expressions over {0, 1} that determine the language which consists of all strings with odd number of 0s. (1*01*)(1*01*01*)*

Regex exercises Construct a regular expression for integers (e.g., 0, 42, -137, etc.). Construct a regular expression for floating point numbers that don’t use scientific notation (e.g., 3.5, 0.2, -47.3). Construct a regular expression for all strings of a’s that have at least 3 a’s. Is there more than one way to write this?

Regex exercises 0|(-|ϵ)(1-9)(0-9)* Construct a regular expression for integers (e.g., 0, 42, -137, etc.). 0|(-|ϵ)(1-9)(0-9)*

(-|ϵ)0|(1-9)(0-9)*.(0-9)+ (-0.0 is allowed in this regex) Regex exercises Construct a regular expression for floating point numbers that don’t use scientific notation (e.g., 3.5, 0.2, -47.3). (-|ϵ)0|(1-9)(0-9)*.(0-9)+ (-0.0 is allowed in this regex)

Regex exercises (a*)aaa (a+)aa Construct a regular expression for all strings of a’s that have at least 3 a’s. Is there more than one way to write this? (a*)aaa (a+)aa

Finite-State Automaton S0 S3 1 1 S2

Finite-State Automaton A Finite-State Automaton is a quadruple: <S, s, F, T> S is a set of states, e.g., {S0,S1,S2,S3} s is the start state, e.g., S0 F is a set of final states, e.g., {S3} T is a set of labeled transitions, of the form (state, input) => state

DFA & NFA DFA - Determistic Finite Automaton: At most one transition for a state and an input symbol. (Recognizers should be a DFA.) NFA - Nondeterministic Finite Automaton: More than one transition possible for a state and an input symbol.

Constructing a DFA from a RE Regular Expression (RE) →NFA with ϵ moves NFA with ϵ moves to NFA NFA→DFA

NFA with ϵ moves (5 rules) B AB

NFA with ϵ moves (5 rules) A|B B ϵ ϵ ϵ ϵ A ϵ A* ϵ

NFA with ϵ moves (Examples) (A|B)C A ϵ ϵ ϵ C B ϵ ϵ

NFA with ϵ moves (Examples) A|BC A ϵ ϵ ϵ B C ϵ ϵ

NFA with ϵ moves (Examples) ((A|B)C)* ϵ A ϵ ϵ ϵ ϵ C ϵ ϵ B ϵ ϵ

DFA (Examples) A A ϵ A B AB

DFA (Examples) A A|B B A A+ A* A A