 We are given the following regular definition: if -> if then -> then else -> else relop -> |>|>= id -> letter(letter|digit)* num -> digit + (.digit.

Slides:



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

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.
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 and Syntactic Analysis Here, we look at two of the tasks involved in the compilation process –Given source code, we need to first break it into.
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.
1 Chapter 2: Scanning 朱治平. Scanner (or Lexical Analyzer) the interface between source & compiler could be a separate pass and places its output on an.
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.
Chapter 3 Lexical Analysis
Topic #3: Lexical Analysis
CPSC 388 – Compiler Design and Construction Scanners – Finite State Automata.
1 Flex. 2 Flex A Lexical Analyzer Generator  generates a scanner procedure directly, with regular expressions and user-written procedures Steps to using.
Lexical Analysis Natawut Nupairoj, Ph.D.
Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Machine-independent code improvement Target code generation Machine-specific.
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.
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)
SCRIBE SUBMISSION GROUP 8 Date: 7/8/2013 By – IKHAR SUSHRUT MEGHSHYAM 11CS10017 Lexical Analyser Constructing Tokens State-Transition Diagram S-T Diagrams.
Scanning & FLEX CPSC 388 Ellen Walker Hiram College.
CS30003: Compilers Lexical Analysis Lecture Date: 05/08/13 Submission By: DHANJIT DAS, 11CS10012.
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,
Review: Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Symbol.
IN LINE FUNCTION AND MACRO Macro is processed at precompilation time. An Inline function is processed at compilation time. Example : let us consider this.
CSc 453 Lexical Analysis (Scanning)
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.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
The Role of Lexical Analyzer
Lexical Analysis (Scanning) Lexical Analysis (Scanning)
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
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
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.
Lexical Analyzer in Perspective
Finite automate.
CS510 Compiler Lecture 2.
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.
Compilers Welcome to a journey to CS419 Lecture5: Lexical Analysis:
CSc 453 Lexical Analysis (Scanning)
Regular Definition and Transition Diagrams
Lexical and Syntax Analysis
Lexical and Syntactic Analysis
פרק 3 ניתוח לקסיקאלי תורת הקומפילציה איתן אביאור.
Example TDs : id and delim
Lexical Analysis and Lexical Analyzer Generators
Recognition of Tokens.
Scanner Scanner Introduction to Compilers.
Designing a Predictive Parser
Scanner Scanner Introduction to Compilers.
Lexical Analysis - An Introduction
Scanner Scanner Introduction to Compilers.
Scanner Scanner Introduction to Compilers.
Scanner Scanner Introduction to Compilers.
CSc 453 Lexical Analysis (Scanning)
Presentation transcript:

 We are given the following regular definition: if -> if then -> then else -> else relop -> |>|>= id -> letter(letter|digit)* num -> digit + (.digit + )? (E(+|-)?digit + )? letter -> [a-z]|[A-Z] digit ->[0-9]

 Recognize the keyword: if, then, else and lexemes: relop, id, num  delim -> blank|tab|newline ws -> delim + if a match for ws is found lexical analyzer does not return a token to parser. It proceeds to find a token following the white space and return that to parser.

 Transition diagram depicts the actions that takes place when a lexical analyzer is called by parser to get the next token  TD keeps track of information about characters that are seen as fwd pointer scans the input  Position in TD are drawn as circles called states  States are connected by arrows called edges  Edges leaving state s have labels indicating i/p characters that can next appear after transition diagram have reached state s.

 Start state: state where control resides when we begin to recognize a token.  No valid transitions indicate failure  Accepting state: state in which token can be found.  * indicates state in which retraction must takes place start letter letter/digit delimiter *

 There may be several transition diagrams  If failure occurs while following one transition diagram, then retract the fwd pointer to where it was in start state of this diagram and activate next transition diagram  If failure occurs in all transition diagrams, lexical error will be detected and error recovery routines will be invoked  e.g. DO 5 I=1.25 DO 5 I=1,25

 Initialize appropriately the symbol table in which information about identifiers is stored  Enter the reserved words into symbol table before any characters in the i/p are seen.  Make a note in the symbol table of the token to be returned when the keyword is identified.  Return statement next to accepting state uses gettoken() and install_id() to obtain token and attribute value  When a lexeme is identified, symbol table is checked  if found as keyword install_id() will return 0  If an identifier, pointer to symbol table entry will be returned  gettoken() will return the corresponding token

 When accepting state is reached,  call a procedure install_num() that enters the lexeme into table of numbers and returns a pointer to created entry  Returns the token NUM

 Token nexttoken( )  {  While (1)  {  switch(state) {  case 0: c=nextchar();  If (c==blank|| c==tab|| c==newline) {  State =0;  lexeme_beginning++;  }  else if (c==’<’) state=1;  else if (c ==’=’)state=5;  else if (c==’>’) state=6;  else state=fail();  break;  case 1: c= nextchar();  if (c==’=’) state=2;  else if (c==’>’) state=3;  else state=4;  break;  case 2: token.attribute=LE;  token.name=relop;  return token;

 case 8: retract (1);  token.attribute=GT;  token.name=relop;  return token;  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;  case11: retract (1);  entry=install_id( );  name=gettoken();  token.name= name;  token. attribute=entry;  return token;  break;  /* cases here for numbers*/

 case 25: c= nextchar();  if (isidgit(c)) state=26;  else state=fail();  break;  case 26: c= nextchar();  if (isidgit(c)) state=26;  else state=27;  break;  case 27:retract (1); install_num( );  return (NUM);  }

 int state=0, start=0;  int lexical_value;  int fail()  {  forward=token_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: /* compiler error*/  }  return start;  }