Flex: A fast Lexical Analyzer Generator CSE470: Spring 2000 Updated by Prasad.

Slides:



Advertisements
Similar presentations
Lexical Analysis Consider the program: #include main() { double value = 0.95; printf("value = %f\n", value); } How is this translated into meaningful machine.
Advertisements

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.
CPSC Compiler Tutorial 2 Scanner & Lex.
Winter 2007SEG2101 Chapter 81 Chapter 8 Lexical Analysis.
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.
Lecture 2: Lexical Analysis CS 540 George Mason University.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
A brief [f]lex tutorial Saumya Debray The University of Arizona Tucson, AZ
Compilers: Yacc/7 1 Compiler Structures Objective – –describe yacc (actually bison) – –give simple examples of its use , Semester 1,
1 Flex. 2 Flex A Lexical Analyzer Generator  generates a scanner procedure directly, with regular expressions and user-written procedures Steps to using.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Compilers: lex/3 1 Compiler Structures Objectives – –describe lex – –give many examples of lex's use , Semester 1, Lex.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 YACC Parser Generator. 2 YACC YACC (Yet Another Compiler Compiler) Produce a parser for a given grammar.  Compile a LALR(1) grammar Original written.
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
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.
CS412/413 Introduction to Compilers Radu Rugina Lecture 4: Lexical Analyzers 28 Jan 02.
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.
Compiler Tools Lex/Yacc – Flex & Bison. Compiler Front End (from Engineering a Compiler) Scanner (Lexical Analyzer) Maps stream of characters into words.
Introduction to Lex Ying-Hung Jiang
Introduction to Yacc 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.
COMPILERS AND INTERPRETERS Lesson 3 – TDDD16 TDDB44 Compiler Construction 2010 Kristian Stavåker Department.
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.
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.
Lex & Yacc By Hathal Alwageed & Ahmad Almadhor. References *Tom Niemann. “A Compact Guide to Lex & Yacc ”. Portland, Oregon. 18 April 2010 *Levine, John.
ICS312 LEX Set 25. LEX Lex is a program that generates lexical analyzers Converting the source code into the symbols (tokens) is the work of the C program.
Applications of Context-Free Grammars (CFG) Parsers. The YACC Parser-Generator. by: Saleh Al-shomrani.
1 LEX & YACC Tutorial February 28, 2008 Tom St. John.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.
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 7 Lex and Intro to Parsing. LEX Last lecture, we learned a little bit about how we can take our regular expressions (which specify our valid tokens)
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
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.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 10: Compiler I: Syntax Analysis slide 1www.nand2tetris.org Building.
Computer Programming – Grammar – data types – Variables – Keywords – Operators – decision making – Loops – Arrays – Functions – files Programming Environment.
ICS611 Lex Set 3. Lex and Yacc Lex is a program that generates lexical analyzers Converting the source code into the symbols (tokens) is the work of the.
1 Syntax Analysis Part III Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
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.
Overview of Compilation The Compiler Front End
Overview of Compilation The Compiler Front End
Using SLK and Flex++ Followed by a Demo
Regular Languages.
TDDD55- Compilers and Interpreters Lesson 2
Bison: Parser Generator
Review: Compiler Phases:
Compiler Structures 3. Lex Objectives , Semester 2,
Compiler Design Yacc Example "Yet Another Compiler Compiler"
More on flex.
Systems Programming & Operating Systems Unit – III
NFAs, scanners, and flex.
Compiler Design 3. Lexical Analyzer, Flex
Presentation transcript:

Flex: A fast Lexical Analyzer Generator CSE470: Spring 2000 Updated by Prasad

Motivation Use Flex to perform Text Processing In structured programming two tasks occur repeatedly: –Lexical Analysis: Dividing the input into meaningful units. For a C program the units are variables, constants, keywords, operators, punctuation etc. These units also called as tokens. (Use Flex) –Parsing: Involves finding the relationship between input tokens. For a C program, one needs to identify valid expressions, statements, blocks, procedures etc. (Use Yacc or Bison)

An Example Recognizing all keywords of a Language. Probably also want to identify and remove comments. /* The following loop computes exponent of number */ int i, number, result; result = 1; for (i=0; i<power; i++) { result = result * number; }

About Flex Flex: Fast Lexical Analyzer Generator. Produces Lexical Analyzers in a fast and easy manner. Given a Flex source file, Flex generates an output C source code file lex.yy.c which defines the scanning routine yylex(). Flex source file (say sample.l) should contain rules for identifying tokens in the input.

Flex Source File (sample.l) Flex Compiler (Flex) Lexical Analyzer Code (lex.yy.c) C Compiler Lexical Analyzer executable Input Text File Output: Tokens Parser

Flex Source Consists of three sections: definitions, rules and user-defined routines. %{ Declaration Section %} Definitions section % Rules Section % User routines Section

Rules Format { } When a Lexical Analyzer is run (or the scanning routine yylex() is called) it analyzes the input for the occurrences of text patterns that match the regular expressions. When ever it finds one it executes the corresponding action. Flex stores the matched text in a global string variable called yytext.

Examples: [0-9]+ { printf(“An integer %s \n”, yytext); } [a-z][a-z0-9]* { printf(“An identifier %s \n”, yytext);} if | then | begin | end | function { printf(“A Keyword %s \n”, yytext); }

Example to count lines %{ int linecount = 0; %} Digit [0-9] Identifier [a-zA-Z]{a-zA-Z0-9}* % {Identifier} { printf(“%s: This is an identifier\n”, yytext); } \n { printf(“The line number is %d\n”, ++linecount); } [\t ]+ ; /*Ignore spaces */. { printf(“Unrecognized character\n”); } % main() { yylex(); }

Example to remove comments %x comment % "/*" BEGIN(comment); [^*\n]* /* eat anything that's not a '*' */ "*"+[^*/\n]* /* eat up '*'s not followed by '/'s */ \n ; "*"+"/" BEGIN(0); %