Compiler Construction

Slides:



Advertisements
Similar presentations
CSE 311 Foundations of Computing I
Advertisements

4b Lexical analysis Finite Automata
CSC 361NFA vs. DFA1. CSC 361NFA vs. DFA2 NFAs vs. DFAs NFAs can be constructed from DFAs using transitions: Called NFA- Suppose M 1 accepts L 1, M 2 accepts.
Compiler Baojian Hua Lexical Analysis (II) Compiler Baojian Hua
Complexity and Computability Theory I Lecture #4 Rina Zviel-Girshin Leah Epstein Winter
Nondeterministic Finite Automata CS 130: Theory of Computation HMU textbook, Chapter 2 (Sec 2.3 & 2.5)
Finite Automata CPSC 388 Ellen Walker Hiram College.
1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3 School of Innovation, Design and Engineering Mälardalen University 2012.
Regular Expressions Finite State Automaton. Programming Languages2 Regular expressions  Terminology on Formal languages: –alphabet : a finite set of.
1 CIS 461 Compiler Design and Construction Fall 2012 slides derived from Tevfik Bultan et al. Lecture-Module 5 More Lexical Analysis.
Introduction to Computability Theory
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.
Courtesy Costas Busch - RPI1 Non Deterministic Automata.
1 The scanning process Goal: automate the process Idea: –Start with an RE –Build a DFA How? –We can build a non-deterministic finite automaton (Thompson's.
Lecture 3 Goals: Formal definition of NFA, acceptance of a string by an NFA, computation tree associated with a string. Algorithm to convert an NFA to.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Nondeterminism.
Lecture 3 Goals: Formal definition of NFA, acceptance of a string by an NFA, computation tree associated with a string. Algorithm to convert an NFA to.
1 Non-Deterministic Automata Regular Expressions.
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.
1 Non-Deterministic Finite Automata. 2 Alphabet = Nondeterministic Finite Automaton (NFA)
Lexical Analysis The Scanner Scanner 1. Introduction A scanner, sometimes called a lexical analyzer A scanner : – gets a stream of characters (source.
CSE 311: Foundations of Computing Fall 2014 Lecture 23: State Minimization, NFAs.
Lexical Analysis — Part II: Constructing a Scanner from Regular Expressions Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
NFA ε - NFA - DFA equivalence. What is an NFA An NFA is an automaton that its states might have none, one or more outgoing arrows under a specific symbol.
Nondeterministic Finite Automata CS 130: Theory of Computation HMU textbook, Chapter 2 (Sec 2.3 & 2.5)
Lexical Analysis — Part II: Constructing a Scanner from Regular Expressions.
REGULAR LANGUAGES.
Lexical Analysis Constructing a Scanner from Regular Expressions.
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
CS412/413 Introduction to Compilers Radu Rugina Lecture 4: Lexical Analyzers 28 Jan 02.
TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP.
Lexical Analysis: Finite Automata CS 471 September 5, 2007.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2010.
CMSC 330: Organization of Programming Languages Finite Automata NFAs  DFAs.
CMSC 330: Organization of Programming Languages Theory of Regular Expressions Finite Automata.
Lexical Analysis – Part II EECS 483 – Lecture 3 University of Michigan Wednesday, September 13, 2006.
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
CS 154 Formal Languages and Computability February 9 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron.
Nondeterministic Finite Automata (NFAs). Reminder: Deterministic Finite Automata (DFA) q For every state q in Q and every character  in , one and only.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen.
LECTURE 5 Scanning. SYNTAX ANALYSIS We know from our previous lectures that the process of verifying the syntax of the program is performed in two stages:
1 Lexical Analysis Uses formalism of Regular Languages Uses formalism of Regular Languages Regular Expressions Regular Expressions Deterministic Finite.
1 Section 11.2 Finite Automata Can a machine(i.e., algorithm) recognize a regular language? Yes! Deterministic Finite Automata A deterministic finite automaton.
Deterministic Finite Automata Nondeterministic Finite Automata.
CS412/413 Introduction to Compilers Radu Rugina Lecture 3: Finite Automata 25 Jan 02.
Compiler Construction Lecture Three: Lexical Analysis - Part Two CSC 2103: Compiler Construction Lecture Three: Lexical Analysis - Part Two Joyce Nakatumba-Nabende.
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Topic 3: Automata Theory 1. OutlineOutline Finite state machine, Regular expressions, DFA, NDFA, and their equivalence, Grammars and Chomsky hierarchy.
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Lexical analysis Finite Automata
Two issues in lexical analysis
Recognizer for a Language
Chapter 2 FINITE AUTOMATA.
Lexical Analysis — Part II: Constructing a Scanner from Regular Expressions Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
COSC 3340: Introduction to Theory of Computation
Lexical Analysis — Part II: Constructing a Scanner from Regular Expressions Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Lecture 4: Lexical Analysis II: From REs to DFAs
CSE 311: Foundations of Computing
NFAs and Transition Graphs
Finite Automata.
4b Lexical analysis Finite Automata
Automating Scanner Construction
Lexical Analysis — Part II: Constructing a Scanner from Regular Expressions Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
4b Lexical analysis Finite Automata
CSE 311 Foundations of Computing I
Chapter 1 Regular Language
Lexical Analysis Uses formalism of Regular Languages
Presentation transcript:

Compiler Construction Sohail Aslam Lecture 7 compiler: intro

Table Encoding of FA b Transition table a a 1 2 a b 1 err 2

Simulating FA trans_table[NSTATES][NCHARS]; accept_states[NSTATES]; state = INITIAL; while(state != err){ c = input.read(); if(c == EOF ) break; state=trans_table[state][c]; } return accept_states[state];

Simulating FA trans_table[NSTATES][NCHARS]; accept_states[NSTATES]; state = INITIAL; while(state != err){ c = input.read(); if(c == EOF ) break; state=trans_table[state][c]; } return accept_states[state];

Simulating FA trans_table[NSTATES][NCHARS]; accept_states[NSTATES]; state = INITIAL; while(state != err){ c = input.read(); if(c == EOF ) break; state=trans_table[state][c]; } return accept_states[state];

Simulating FA trans_table[NSTATES][NCHARS]; accept_states[NSTATES]; state = INITIAL; while(state != err){ c = input.read(); if(c == EOF ) break; state=trans_table[state][c]; } return accept_states[state];

Simulating FA trans_table[NSTATES][NCHARS]; accept_states[NSTATES]; state = INITIAL; while(state != err){ c = input.read(); if(c == EOF ) break; state=trans_table[state][c]; } return accept_states[state];

Simulating FA trans_table[NSTATES][NCHARS]; accept_states[NSTATES]; state = INITIAL; while(state != err){ c = input.read(); if(c == EOF ) break; state=trans_table[state][c]; } return accept_states[state];

RE → Finite Automata Can we build a finite automaton for every regular expression? Yes, – build FA inductively based on the definition of Regular Expression

NFA Nondeterministic Finite Automaton (NFA) Can have multiple transitions for one input in a given state Can have e - moves

Epsilon Moves ε – moves machine can move from state A to state B without consuming input e A B

NFA operation of the automaton is not completely defined by input 1 1 1 A B C On input “11”, automaton could be in either state

Execution of FA A NFA can choose Whether to make e-moves. Which of multiple transitions to take for a single input.

Acceptance of NFA NFA can get into multiple states Rule: NFA accepts if it can get in a final state 1 1 A B C

DFA and NFA Deterministic Finite Automata (DFA) One transition per input per state. No e - moves

Execution of FA A DFA can take only one path through the state graph. Completely determined by input.

NFA vs DFA NFAs and DFAs recognize the same set of languages (regular languages) DFAs are easier to implement – table driven.

NFA vs DFA For a given language, the NFA can be simpler than the DFA. DFA can be exponentially larger than NFA.

NFA vs DFA NFAs are the key to automating RE → DFA construction.

RE → NFA Construction Thompson’s construction (CACM 1968) Build an NFA for each RE term. Combine NFAs with e-moves.

RE → NFA Construction Subset construction NFA → DFA Build the simulation. Minimize number of states in DFA (Hopcroft’s algorithm)

RE → NFA Construction Key idea: NFA pattern for each symbol and each operator. Join them with e-moves in precedence order.

RE → NFA Construction a s0 s1 NFA for a a e b s0 s1 s3 s4 NFA for ab

RE → NFA Construction a NFA for a s0 s1

RE → NFA Construction a NFA for a s0 s1 b NFA for b s3 s4

RE → NFA Construction a NFA for a s0 s1 b NFA for b s3 s4 a b s0 s1 s3

RE → NFA Construction a NFA for a s0 s1 b NFA for b s3 s4 a e b s0 s1 NFA for ab

RE → NFA Construction a s1 s2 e e s0 s5 b e s3 s4 e NFA for a | b

RE → NFA Construction a s1 s2 NFA for a

RE → NFA Construction a s1 s2 b s3 s4 NFA for a and b

RE → NFA Construction a s1 s2 e e s0 s5 b e s3 s4 e NFA for a | b

RE → NFA Construction e e a e s0 s1 s2 s4 e NFA for a*

RE → NFA Construction a s1 s2 NFA for a

RE → NFA Construction e e a e s0 s1 s2 s4 e NFA for a*

Example RE → NFA s4 s5 s0 s1 s2 s3 s8 s9 s6 s7 NFA for a ( b|c )* e e

Example RE → NFA building NFA for a ( b|c )* a s0 s1

Example RE → NFA NFA for a, b and c b s4 s5 a s0 s1 c s6 s7

Example RE → NFA s4 s5 s0 s1 s3 s8 s6 s7 NFA for a and b|c e e e e b a

Example RE → NFA s4 s5 s0 s1 s2 s3 s8 s9 s6 s7 NFA for a and ( b|c )*

Example RE → NFA s4 s5 s0 s1 s2 s3 s8 s9 s6 s7 NFA for a ( b|c )* e e