1 IMPLEMENTATION OF FINITE AUTOMAT IN CODE There are several ways to translate either a DFA or an NFA into code. Consider, again the example of a DFA that.

Slides:



Advertisements
Similar presentations
COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou.
Advertisements

Chapter 2 Lexical Analysis Nai-Wei Lin. Lexical Analysis Lexical analysis recognizes the vocabulary of the programming language and transforms a string.
Chapter 3 Lexical Analysis Yu-Chen Kuo.
Chapter 3 Lexical Analysis. Definitions The lexical analyzer produces a certain token wherever the input contains a string of characters in a certain.
Lexical Analyzer Second lecture. Compiler Construction Outline Informal sketch of lexical analysis Identifies tokens in input string Issues in lexical.
CS-338 Compiler Design Dr. Syed Noman Hasany Assistant Professor College of Computer, Qassim University.
 Lex helps to specify lexical analyzers by specifying regular expression  i/p notation for lex tool is lex language and the tool itself is refered to.
Lexical Analysis (2 Lectures). CSE244 Compilers 2 Overview Basic Concepts Regular Expressions –Language Lexical analysis by hand Regular Languages Tools.
Chapter 3: Lexical Analysis
Chapter 3 Chang Chi-Chung. The Structure of the Generated Analyzer lexeme Automaton simulator Transition Table Actions Lex compiler Lex Program lexemeBeginforward.
Lexical Analysis Recognize tokens and ignore white spaces, comments
Lexical Analysis The Scanner Scanner 1. Introduction A scanner, sometimes called a lexical analyzer A scanner : – gets a stream of characters (source.
Scanner 1. Introduction A scanner, sometimes called a lexical analyzer A scanner : – gets a stream of characters (source program) – divides it into tokens.
 We are given the following regular definition: if -> if then -> then else -> else relop -> |>|>= id -> letter(letter|digit)* num -> digit + (.digit.
Chapter 3 Lexical Analysis
Topic #3: Lexical Analysis
CPSC 388 – Compiler Design and Construction Scanners – Finite State Automata.
Lexical Analysis Natawut Nupairoj, Ph.D.
Lexical Analysis - An Introduction. The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
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.
Topic #3: Lexical Analysis EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Lexical Analyzer (Checker)
Lexical and Syntax Analysis
Scanning & FLEX CPSC 388 Ellen Walker Hiram College.
TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP.
CH3.1 CS 345 Dr. Mohamed Ramadan Saady Algebraic Properties of Regular Expressions AXIOMDESCRIPTION r | s = s | r r | (s | t) = (r | s) | t (r s) t = r.
Lexical Analysis: Finite Automata CS 471 September 5, 2007.
1 Lexical Analysis and Lexical Analyzer Generators Chapter 3 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Using Scanner Generator Lex By J. H. Wang May 10, 2011.
Joey Paquet, 2000, Lecture 2 Lexical Analysis.
Lexical Analysis S. M. Farhad. Input Buffering Speedup the reading the source program Look one or more characters beyond the next lexeme There are many.
Overview of Previous Lesson(s) Over View  Symbol tables are data structures that are used by compilers to hold information about source-program constructs.
Scanner Introduction to Compilers 1 Scanner.
1 January 18, January 18, 2016January 18, 2016January 18, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Lexical Analysis.
1st Phase Lexical Analysis
Chapter 2 Scanning. Dr.Manal AbdulazizCS463 Ch22 The Scanning Process Lexical analysis or scanning has the task of reading the source program as a file.
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.
Chapter 2-II Scanning Sung-Dong Kim Dept. of Computer Engineering, Hansung University.
COMP 3438 – Part II - Lecture 3 Lexical Analysis II Par III: Finite Automata Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ. 1.
Finite automate.
CS510 Compiler Lecture 2.
Lecture 2 Lexical Analysis
Chapter 3: Lexical Analysis and Flex
Scanner Scanner Introduction to Compilers.
Chapter 3 Lexical Analysis.
Lecture 5 Transition Diagrams
Lecture 2 Lexical Analysis Joey Paquet, 2000, 2002, 2012.
Chapter 2 Scanning – Part 1 June 10, 2018 Prof. Abdelaziz Khamis.
Finite-State Machines (FSMs)
COMPILER CONSTRUCTION
Compilers Welcome to a journey to CS419 Lecture5: Lexical Analysis:
CSc 453 Lexical Analysis (Scanning)
Finite-State Machines (FSMs)
Regular Definition and Transition Diagrams
Chapter 3: Lexical Analysis
פרק 3 ניתוח לקסיקאלי תורת הקומפילציה איתן אביאור.
Example TDs : id and delim
Lexical Analysis and Lexical Analyzer Generators
Recognition of Tokens.
Scanner Scanner Introduction to Compilers.
Scanner Scanner Introduction to Compilers.
Lexical Analysis - An Introduction
Compiler Structures 2. Lexical Analysis Objectives
Scanner Scanner Introduction to Compilers.
Scanner Scanner Introduction to Compilers.
Scanner Scanner Introduction to Compilers.
CSc 453 Lexical Analysis (Scanning)
Presentation transcript:

1 IMPLEMENTATION OF FINITE AUTOMAT IN CODE There are several ways to translate either a DFA or an NFA into code. Consider, again the example of a DFA that accepts identifiers consisting of a letter followed by a sequence of letters and/ or digits in its amended form that includes lookahead and the principal of longest substring.

2 Digit Start Letter In_id {Other} 2 Finish Return ID IMPLEMENTATION OF FINITE AUTOMAT IN CODE (cont’d)

3 Simulation of the DFA { Starting in state 1} If the next character is a letter then advance the input: { now in state 2} While the next character is a letter or a digit do advance the input { stay in state 2} End while; { go to state 3 without consuming input } Accept Else { Error or other cases } End if;

4 Constructing Transition Diagrams for Tokens  Transition Diagrams (TD) are used to represent the tokens  As characters are read, the relevant TDs are used to attempt to match lexeme to a pattern

5  Each TD has: – States : Represented by Circles – Actions : Represented by Arrows between states – Start State : Beginning of a pattern (Arrowhead) – Final State(s) : End of pattern (Concentric Circles) Each TD is Deterministic - No need to choose between 2 different actions !

6 Example TDs  Recognition Of Relational Operators start other => * RTN(G) RTN(GE) > = : We’ve accepted “>” and have read other char that must be unread (means push back into input stream)

7 Example : All RELOPs start< 0 other = 67 8 return(relop, LE) 5 4 > = 12 3 other > = * * return(relop, NE) return(relop, LT) return(relop, EQ) return(relop, GE) return(relop, GT)

8 Example TDs : id startletter 9 other 1110 letter or digit * id : return( get_token(), install_id())

9 Example TDs : Unsigned #s startdigit 20 *. 21 digit 24 other 23 digit 22 * return(num, install_num()) startdigit 25 other 2726 digit *

10 Implementing Transition Diagrams class Scanner { char _la; // The lookahead character Token nextToken() { startLexeme(); // reset window at start while(true) { switch(_state) { case 0: { _la = getChar(); if (_la == ‘<’) _state = 1; else if (_la == ‘=’) _state = 5; else if (_la == ‘>’) _state = 6; else failure(state); }break; case 6: { _la = getChar(); if (_la == ‘=’) _state = 7; else _state = 8; }break; } case 7: { return new Token(GEQUAL); }break; case 8: { pushBack(_la); return new Token(GREATER); }

11 Implementing Transition Diagrams lexeme_beginning = forward; state = 0; token nexttoken() { while(1) { switch (state) { case 0: c = nextchar(); /* c is lookahead character */ if (c== blank || c==tab || c== newline) { state = 0; lexeme_beginning++; /* advance beginning of lexeme */ } else if (c == ‘<‘) state = 1; else if (c == ‘=‘) state = 5; else if (c == ‘>’) state = 6; else state = fail(); break; … /* cases 1-8 here */ start< 0 other = > = 12 3 > = * * FUNCTIONS USED nextchar(), forward, retract(), install_num(), install_id(), gettoken(), isdigit(), isletter(), recover() repeat until a “return” occurs

case 25; c = nextchar(); if (isdigit(c)) state = 26; else state = fail(); break; case 26; c = nextchar(); if (isdigit(c)) state = 26; else state = 27; break; case 27; retract(1); lexical_value = install_num(); return ( NUM ); Case numbers correspond to transition diagram states ! 25 digitother 2726 digit * Implementing Transition Diagrams, II advances forward looks at the region lexeme_beginning... forward retracts forward 25

case 9: c = nextchar(); if (isletter(c)) state = 10; else state = fail(); break; case 10; c = nextchar(); if (isletter(c)) state = 10; else if (isdigit(c)) state = 10; else state = 11; break; case 11; retract(1); lexical_value = install_id(); return ( gettoken(lexical_value) ); letter 9 other 1110 letter or digit * Implementing Transition Diagrams, III reads token name from ST

14 When Failures Occur: Init fail() { start = state; forward = lexeme beginning; switch (start) { case 0: start = 9; break; case 9: start = 12; break; case 12: start = 20; break; case 20: start = 25; break; case 25: recover(); break; default: /* lex error */ } return start; } Switch to next transition diagram

15 What Else Does Lexical Analyzer Do? All Keywords / Reserved words are matched as ids After the match, the symbol table or a special keyword table is consulted Keyword table contains string versions of all keywords and associated token values if begin then When a match is found, the token is returned, along with its symbolic value, i.e., “then”, 16 If a match is not found, then it is assumed that an id has been discovered

16 ASSINGMENT Design a nondeterministic finite automata in c++.