 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.

Slides:



Advertisements
Similar presentations
SYMBOL TABLES &CODE GENERATION FOR EXECUTABLES. SYMBOL TABLES Compilers that produce an executable (or the representation of an executable in object module.
Advertisements

Structure of a YACC File Has the same three-part structure as Lex Each part is separated by a % symbol The three parts are even identical: – definition.
Compiler construction in4020 – lecture 2 Koen Langendoen Delft University of Technology The Netherlands.
Lex -- a Lexical Analyzer Generator (by M.E. Lesk and Eric. Schmidt) –Given tokens specified as regular expressions, Lex automatically generates a routine.
COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou.
Converting NFAs to DFAs. NFA to DFA: Approach In: NFA N Out: DFA D Method: Construct transition table Dtran (a.k.a. the "move function"). Each DFA state.
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
1 Chapter 2: Scanning 朱治平. Scanner (or Lexical Analyzer) the interface between source & compiler could be a separate pass and places its output on an.
Tools for building compilers Clara Benac Earle. Tools to help building a compiler C –Lexical Analyzer generators: Lex, flex, –Syntax Analyzer generator:
Chapter 3 Chang Chi-Chung. The Structure of the Generated Analyzer lexeme Automaton simulator Transition Table Actions Lex compiler Lex Program lexemeBeginforward.
A brief [f]lex tutorial Saumya Debray The University of Arizona Tucson, AZ
 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
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
1 Flex. 2 Flex A Lexical Analyzer Generator  generates a scanner procedure directly, with regular expressions and user-written procedures Steps to using.
Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Machine-independent code improvement Target code generation Machine-specific.
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.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter 6 Programming Languages (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Review: Regular expression: –How do we define it? Given an alphabet, Base case: – is a regular expression that denote { }, the set that contains the empty.
Lecture 2: Lexical Analysis
Other Issues - § 3.9 – Not Discussed More advanced algorithm construction – regular expression to DFA directly.
COMP 3438 – Part II - Lecture 2: Lexical Analysis (I) Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ. 1.
FLEX Fast Lexical Analyzer EECS Introduction Flex is a lexical analysis (scanner) generator. Flex is provided with a user input file or Standard.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
CS30003: Compilers Lexical Analysis Lecture Date: 05/08/13 Submission By: DHANJIT DAS, 11CS10012.
Flex: A fast Lexical Analyzer Generator CSE470: Spring 2000 Updated by Prasad.
LEX (04CS1008) A tool widely used to specify lexical analyzers for a variety of languages We refer to the tool as Lex compiler, and to its input specification.
TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP.
Chapter 3. Lexical Analysis (1). 2 Interaction of lexical analyzer with parser.
Lexical Analyzer in Perspective
Review: Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Symbol.
1.  It is the first phase of compiler.  In computer science, lexical analysis is the process of converting a sequence of characters into a sequence.
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.
Introduction to Lex Fan Wu
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.
Practical 1-LEX Implementation
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
1 Lex & Yacc. 2 Compilation Process Lexical Analyzer Source Code Syntax Analyzer Symbol Table Intermed. Code Gen. Code Generator Machine Code.
Compiler Principle and Technology Prof. Dongming LU Mar. 26th, 2014.
YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
Overview of Previous Lesson(s) Over View  Symbol tables are data structures that are used by compilers to hold information about source-program constructs.
1 LEX & YACC Tutorial February 28, 2008 Tom St. John.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
The Role of Lexical Analyzer
Semantic Values and Symbol Tables © Allan C. Milne Abertay University v
Lexical Analysis.
1st Phase Lexical Analysis
1 Steps to use Flex Ravi Chotrani New York University Reviewed By Prof. Mohamed Zahran.
Scanner Generation Using SLK and Flex++ Followed by a Demo Copyright © 2015 Curt Hill.
More yacc. What is yacc – Tool to produce a parser given a grammar – YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
9-December-2002cse Tools © 2002 University of Washington1 Lexical and Parser Tools CSE 413, Autumn 2002 Programming Languages
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
Lexical Analyzer in Perspective
A Simple Syntax-Directed Translator
Lexical Analysis.
Chapter 3 Lexical Analysis.
NFAs, scanners, and flex.
Lecture 5 Transition Diagrams
Tutorial On Lex & Yacc.
CSc 453 Lexical Analysis (Scanning)
Compiler Construction
Review: Compiler Phases:
Other Issues - § 3.9 – Not Discussed
Appendix B.1 Lex Appendix B.1 -- Lex.
Systems Programming & Operating Systems Unit – III
Compiler Design 3. Lexical Analyzer, Flex
Lex Appendix B.1 -- Lex.
Presentation transcript:

 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 as lex compiler  Lex compiler transform i/p patterns into transition diagram and generates the code in a file lex.yy.c

Fig. Creating lexical analyzer using lex

 i/p file is lex.l, describes the lexical analyzer to be generated  lex.l runs through lex compiler to produce a C program lex.yy.c that simulate transition diagram for i/p patterns  lex.yy.c is compiled by C compiler to produce a.out, the working lexical analyzer that can produce stream of tokens from sequence of i/p characters  a.out is a subroutine to parser. It returns an integer, which is a code for one of the possible token names  Attribute value is placed in a global variable yylval, which is shared by lexical analyzer and parser

 The general format of Lex source is: {declarations} % {translation rules} % {user subroutines}  Declaration: of variables, manifest constants( identifiers declared to stand for a constant), and regular definition

 Translation rules: pattern {action} pattern-> regular expression that may use regular definitions of declaration section action -> action lexical analyzer should take when pattern pi matches a lexeme  user subroutines: auxiliary procedures needed by action

 When called by parser, lexical analyzer  Begins reading its remaining i/p until it finds the longest prefix of i/p that matches one of the patterns Pi  Then execute associated action Ai (Typically Ai will return to the parser. If it does not, lexical analyzer proceeds to find additional lexemes until one of the corresponding actions causes a return to the parser)  lexical analyzer returns a single value, token name.  The shared integer variable yylval is used to pass additional information about the lexeme found

 Int InstallID() { /*function to install lexeme, whose 1 st character is pointed to by yytext, and whose length is yyleng, into the symbol table and returns a pointer to there */ }  Int installNum() {/* similar to InstallID() but puts numerical constants into separate table*/ }

 The actions taken when id is matched are:  InstallID() is called to place the lexeme found in the symbol table  This function returns a pointer to symbol table, which is placed in global variable yylval, where it can be used by parser or a later component of compiler. installID() has two variables available to it that are set automatically by lexical analyzer generated by Lex: yytext : pointer to beginning of lexeme yyleng: length of lexeme found  token name ID is returned to parser  action taken when lexeme matching number is similar

 When several prefixes of the i/p match one or more patterns :  Prefer the longest prefix to a shorter prefix e.g: <= to <  If longest possible prefix matches two or more patterns, prefer the pattern listed first in Lex program

 Lex automatically reads one character ahead of last character that forms the lexeme, and then retract the i/p so the lexeme itself is taken from i/p stream  Sometimes we want certain pattern to be matched to i/p only when it is followed by certain other characters  Use / in the pattern to indicate the end of the part of pattern that matches the lexeme  What follows / is additional pattern that must be matched before we can decide that token in question is seen. What matches second pattern is not part of the lexeme

 e.g. IF(I,J)=3 (IF array name) IF (condition) THEN…. IF / \(.* \) {letter}