Other Issues - § 3.9 – Not Discussed

Slides:



Advertisements
Similar presentations
4b Lexical analysis Finite Automata
Advertisements

Compiler Baojian Hua Lexical Analysis (II) Compiler Baojian Hua
 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.
Winter 2007SEG2101 Chapter 81 Chapter 8 Lexical Analysis.
Chapter 3: Lexical Analysis
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.
Chapter 3 Lexical Analysis
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). Overview Main task: to read input characters and group them into “ tokens. ” Secondary tasks: –Skip comments and whitespace;
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.
Other Issues - § 3.9 – Not Discussed More advanced algorithm construction – regular expression to DFA directly.
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.
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.
1 November 1, November 1, 2015November 1, 2015November 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Lexical Analyzer in Perspective
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,
By Neng-Fa Zhou Lexical Analysis 4 Why separate lexical and syntax analyses? –simpler design –efficiency –portability.
IN LINE FUNCTION AND MACRO Macro is processed at precompilation time. An Inline function is processed at compilation time. Example : let us consider this.
1 Using Lex. Flex – Lexical Analyzer Generator A language for specifying lexical analyzers Flex compilerlex.yy.clang.l C compiler -lfl a.outlex.yy.c a.outtokenssource.
Using Scanner Generator Lex By J. H. Wang May 10, 2011.
Flex Fast LEX analyzer CMPS 450. Lexical analysis terms + A token is a group of characters having collective meaning. + A lexeme is an actual character.
By Neng-Fa Zhou Programming language syntax 4 Three aspects of languages –Syntax How are sentences formed? –Semantics What does a sentence mean? –Pragmatics.
Lexical Analysis.
1st Phase Lexical Analysis
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
using Deterministic Finite Automata & Nondeterministic Finite Automata
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
Deterministic Finite Automata Nondeterministic Finite Automata.
Lecture 2 Compiler Design Lexical Analysis By lecturer Noor Dhia
Department of Software & Media Technology
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
Lecture 2 Lexical Analysis
Chapter 3 Lexical Analysis.
NFAs, scanners, and flex.
Lexical analysis Finite Automata
Compilers Welcome to a journey to CS419 Lecture5: Lexical Analysis:
The time complexity for e-closure(T).
COMPILER DESIGN UNIT-I.
Two issues in lexical analysis
Recognizer for a Language
Review: NFA Definition NFA is non-deterministic in what sense?
Syntax Analysis Sections :.
Chapter 3: Lexical Analysis
Lexical Analysis Why separate lexical and syntax analyses?
Department of Software & Media Technology
Lexical and Syntax Analysis
פרק 3 ניתוח לקסיקאלי תורת הקומפילציה איתן אביאור.
Chapter 3: Lexical Analysis
Lexical Analysis - An Introduction
Lexical Analysis and Lexical Analyzer Generators
Review: Compiler Phases:
Lecture 5: Lexical Analysis III: The final bits
Recognition of Tokens.
4b Lexical analysis Finite Automata
Finite Automata & Language Theory
Designing a Predictive Parser
4b Lexical analysis Finite Automata
Lexical Analysis - An Introduction
Lecture 5 Scanning.
Regular Expressions and Lexical Analysis
Compiler Design 3. Lexical Analyzer, Flex
Presentation transcript:

Other Issues - § 3.9 – Not Discussed More advanced algorithm construction – regular expression to DFA directly

Final Notes : R.E. to NFA Construction So, an NFA may be simulated by algorithm, when NFA is constructed using Previous techniques Algorithm run time is proportional to |N| * |x| where |N| is the number of states and |x| is the length of input Alternatively, we can construct DFA from NFA and use the resulting Dtran to recognize input: space required O(|r|) O(|r|*|x|) O(|x|) O(2|r|) DFA NFA time to simulate where |r| is the length of the regular expression.

Pulling Together Concepts Designing Lexical Analyzer Generator Reg. Expr.  NFA construction NFA  DFA conversion DFA simulation for lexical analyzer Recall Lex Structure Pattern Action … … Each pattern recognizes lexemes Each pattern described by regular expression (a | b)*abb  e.g.  (abc)*ab  etc. Recognizer!

Lex Specification  Lexical Analyzer Let P1, P2, … , Pn be Lex patterns (regular expressions for valid tokens in prog. lang.) Construct N(P1), N(P2), … N(Pn) Note: accepting state of N(Pi) will be marked by Pi Construct NFA: N(P1) Lex applies conversion algorithm to construct DFA that is equivalent!   N(P2)  N(Pn)

Pictorially Lex Specification Lex Compiler Transition Table (a) Lex Compiler lexeme input buffer FA Simulator Transition Table (b) Schematic lexical analyzer

Example P1 : a P2 : abb P3 : a*b+ 3 patterns NFA’s : start 1 b a 2 3 4 5 8 7 6 P1 P2 P3

Example – continued (2) Combined NFA :  1 a P1 2  a b b start P2 3 4 P2 3 4 5 6 a b  P3 7 8 b Examples a a b a {0,1,3,7} {2,4,7} {7} {8} death pattern matched: - P1 - P3 - a b b {0,1,3,7} {2,4,7} {5,8} {6,8} pattern matched: - P1 P3 P2,P3  break tie in favor of P2

Example – continued (3) Alternatively Construct DFA: (keep track of correspondence between patterns and new accepting states) P2 {8} - {6,8} P3 {5,8} none {7} P1 {2,4,7} {0,1,3,7} Pattern b a STATE Input Symbol break tie in favor of P2

Minimizing the Number of States of DFA Construct initial partition  of S with two groups: accepting/ non-accepting. (Construct new )For each group G of  do begin Partition G into subgroups such that two states s,t of G are in the same subgroup iff for all symbols a states s,t have transitions on a to states of the same group of . Replace G in new by the set of all these subgroups. Compare new and . If equal, final:=  then proceed to 4, else set  := new and goto 2. Aggregate states belonging in the groups of final

example a a A a F B a b b a D C b b b a a A,C,D B,F b Minimized DFA: b

Using LEX Lex Program Structure: declarations %% translation rules %% auxiliary procedures Name the file e.g. test.lex Then, “lex test.lex” produces the file “lex.yy.c” (a C-program)

LEX %{ /* definitions of all constants LT, LE, EQ, NE, GT, GE, IF, THEN, ELSE, ... */ %} ...... letter [A-Za-z] digit [0-9] id {letter}({letter}|{digit})* %% if { return(IF);} then { return(THEN);} {id} { yylval = install_id(); return(ID); } install_id() { /* procedure to install the lexeme to the ST */ C declarations declarations Rules Auxiliary

Example of a Lex Program int num_lines = 0, num_chars = 0; %% \n {++num_lines; ++num_chars;} . {++num_chars;} main( argc, argv ) int argc; char **argv; { ++argv, --argc; /* skip over program name */ if ( argc > 0 ) yyin = fopen( argv[0], "r" ); else yyin = stdin; yylex(); printf( "# of lines = %d, # of chars = %d\n", num_lines, num_chars ); }

Another Example %{ #include <stdio.h> %} WS [ \t\n]* %% [0123456789]+ printf("NUMBER\n"); [a-zA-Z][a-zA-Z0-9]* printf("WORD\n"); {WS} /* do nothing */ . printf(“UNKNOWN\n“); main( argc, argv ) int argc; char **argv; { ++argv, --argc; if ( argc > 0 ) yyin = fopen( argv[0], "r" ); else yyin = stdin; yylex(); }

Concluding Remarks Looking Ahead: Focused on Lexical Analysis Process, Including - Regular Expressions Finite Automaton Conversion Lex Interplay among all these various aspects of lexical analysis Looking Ahead: The next step in the compilation process is Parsing: Top-down vs. Bottom-up - Relationship to Language Theory