COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.

Slides:



Advertisements
Similar presentations
Compiler construction in4020 – lecture 2 Koen Langendoen Delft University of Technology The Netherlands.
Advertisements

COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
Lex -- a Lexical Analyzer Generator (by M.E. Lesk and Eric. Schmidt) –Given tokens specified as regular expressions, Lex automatically generates a routine.
 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.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
Lexical Analysis - Scanner- Contd Computer Science Rensselaer Polytechnic Compiler Design Lecture 4(01/26/98)
Tools for building compilers Clara Benac Earle. Tools to help building a compiler C –Lexical Analyzer generators: Lex, flex, –Syntax Analyzer generator:
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
Chapter 3 Chang Chi-Chung. The Structure of the Generated Analyzer lexeme Automaton simulator Transition Table Actions Lex compiler Lex Program lexemeBeginforward.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
Lecture 2: Lexical Analysis CS 540 George Mason University.
A brief [f]lex tutorial Saumya Debray The University of Arizona Tucson, AZ
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
CMSC 331, Some material © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Chapter 4 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.
Compilers: lex/3 1 Compiler Structures Objectives – –describe lex – –give many examples of lex's use , Semester 1, Lex.
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
COMP 3438 – Part II - Lecture 2: Lexical Analysis (I) Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ. 1.
Scanning & FLEX CPSC 388 Ellen Walker Hiram College.
FLEX Fast Lexical Analyzer EECS Introduction Flex is a lexical analysis (scanner) generator. Flex is provided with a user input file or Standard.
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.
JLex Lecture 4 Mon, Jan 24, JLex JLex is a lexical analyzer generator in Java. It is based on the well-known lex, which is a lexical analyzer generator.
Lexical Analysis – Part I EECS 483 – Lecture 2 University of Michigan Monday, September 11, 2006.
Introduction to Lex Ying-Hung Jiang
1 Using Lex. 2 Introduction When you write a lex specification, you create a set of patterns which lex matches against the input. Each time one of the.
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
Introduction to Lexical Analysis and the Flex Tool. © Allan C. Milne Abertay University v
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
1 Lex & Yacc. 2 Compilation Process Lexical Analyzer Source Code Syntax Analyzer Symbol Table Intermed. Code Gen. Code Generator Machine Code.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
C Chuen-Liang Chen, NTUCS&IE / 35 SCANNING Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei,
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.
LECTURE 6 Scanning Part 2. FROM DFA TO SCANNER In the previous lectures, we discussed how one might specify valid tokens in a language using regular expressions.
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 Analysis.
NFAs, scanners, and flex.
Tutorial On Lex & Yacc.
4 Lexical analysis.
Using SLK and Flex++ Followed by a Demo
TDDD55- Compilers and Interpreters Lesson 2
4 Lexical analysis.
Review: Compiler Phases:
Lecture 5: Lexical Analysis III: The final bits
Compiler Structures 3. Lex Objectives , Semester 2,
Appendix B.1 Lex Appendix B.1 -- Lex.
More on flex.
Regular Expressions and Lexical Analysis
Systems Programming & Operating Systems Unit – III
NFAs, scanners, and flex.
Compiler Design 3. Lexical Analyzer, Flex
Lexical Analysis - Scanner-Contd
Lex Appendix B.1 -- Lex.
Presentation transcript:

COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University pursuant to Part VB of the Copyright Act 1968 (the Act). The material in this communication may be subject to copyright under the Act. Any further reproduction or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice.

Lexical Analysis CSE2303 Formal Methods I Lecture 6

Overview Lexical Analyzer Implementing Finite Automata Introduction to flex

Simple Calculator Example Tokens are: –Numbers, operators, spaces, and newlines * 0.456\n *0.456\n

C Example Tokens are: –variables, keywords, constants, operators, etc. m main(){printf( ain(){printf(“H l Hello\n“ ” ); \n lo\n ” );} e }

Lexical Analyzer Reads the input one character at a time. Splits the input up into tokens. Implemented using a Finite Automaton or NFA.

Matching a Regular Expression Write a C function yylex which reads in a character string, consisting of a ’s and b ’s, one character at a time and identifies whether or not the string matches the following regular expression. (a + bb + baa*b)*(baa*)

Matching a Regular Expression (clarification) –If no characters can be read then yylex should return 0. –If the string matches (a + bb + baa*b)*(baa*) then return yylex should return 1. –Otherwise yylex should return 2.

b b a a a b

#define END 0 #define MATCH 1 #define OTHER 2 int yylex() { int currentState = 0; int table[3][2] = {{0, 1}, {2, 0}, {2, 0}}; int c = getchar(); if (c == EOF) return END; while (c != EOF) { if (c == ‘a’) currentState = table[currentState][0]; if (c == ‘b’) currentState = table[currentState][1]; c = getchar(); } if (currentState == 2) return MATCH; return OTHER; }

Word identification Write a C function yylex which reads in a character string one character at a time and identifies the following tokens: –newline, –space, and –word.

Word identification (clarification) –If no characters can be read then yylex should return 0. –If yylex reads a newline it should return 1. –If yylex reads a space (blank or tab) then it should return 2. –Otherwise, after yylex has read a word it should return 3.

- + + space non-white + space, newline + newline

enum {END = 0, NEWLINE = 1, SPACE = 2, WORD = 3}; int yylex() { int c = getchar(); if (c == EOF) return END; if (c == ‘\n‘) return NEWLINE; if (c == ‘ ‘ || c == ‘\t‘) return SPACE; while (c != EOF) { if (c == ‘ ‘ || c == ‘\t‘ || c == ‘\n‘) { ungetc(c, stdin); return WORD; } c = getchar(); } return WORD; }

flex Lexical analyzer generator –It writes a lexical analyzer Assumption –each token matches a regular expression Needs –set of regular expressions –for each expression an action Produces –A C program

Kleene’s Theorem Kleene’s Theorem is the basis of flex The input for flex –A set of patterns and corresponding actions Patterns –are regular expressions –by Kleene’s Theorem these can be represented as FA. FA –are easily to implement

Process Write a flex program example.l Run it through flex flex example.l The file lex.yy.c contains the function yylex Compile your C programs with the flag -lfl example.l flex lex.yy.c

A flex Program … definitions … % … rules … % … subroutines …

Sections … definition section –Code between %{ … %} copied. –Definitions used to define long expressions … rule section –Each rule has a pattern and an action. –The patterns are regular expressions … subroutine section –Consists of users subroutines. –Copied after the end of the flex generated code

%{ enum {MATCH = 1, OTHER = 2}; %} R (a|bb|baa*b)*(baa*) % {R} {return MATCH;} [ab]+ {return OTHER;} % (a + bb + baa*b)*(baa*) (a + bb + baa*b)*(baa*)

int main() { int val; … val = yylex(); … } match.c flex match.l gcc –o match match.c lex.yy.c -lfl match.l %{ enum {MATCH = 1, OTHER = 2}; %} R (a|bb|baa*b)*(baa*) % {R} {return MATCH;} [ab]+ {return OTHER;} %

%{ enum {NEWLINE = 1, SPACE = 2, WORD = 3}; %} % \n {return NEWLINE;} [ \t] {return SPACE;} [^ \t\n]+ {return WORD;} % Word Identification

Global Variables char* yytext –Contains the text of the current token. int yyleng –Holds the length of the current token.

%{ long charCount = 0; long wordCount = 0; long lineCount = 0; %} % \n {charCount++; lineCount++;} [ \t] charCount++; [^ \t\n]+ {wordCount++; charCount += yyleng;} % Word Count

Important To see all characters you can use od od -bc filename To remove carriage returns you can: dos2unix unix_filename or tr –d ‘\r’ unix_filename flex does not handle carriage returns

More Information Check the courseware web site. Man pages –login and type: xman flex & Library –J.Levine, et al., “lex & yacc”. –A.V. Aho and J.D. Ullman, “Principles of Compiler Design”.

Revision Understand what a lexical analyzer does. Know how to implement a finite automaton. Be able to use flex.