steps to use flex

12
1 Steps to use Flex Ravi Chotrani New York University Reviewed By Prof. Mohamed Zahran

Upload: chakra

Post on 16-Feb-2016

81 views

Category:

Documents


0 download

DESCRIPTION

Steps to use Flex. Ravi Chotrani New York University Reviewed By Prof. Mohamed Zahran. Outline. What is Flex Structure of a Flex File Steps to use Flex Example Summary. What is Flex. Flex (The Fast Lexical Analyzer) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Steps to use Flex

1

Steps to use Flex

Ravi ChotraniNew York University

Reviewed ByProf. Mohamed Zahran

Page 2: Steps to use Flex

2

Outline What is Flex Structure of a Flex File Steps to use Flex Example Summary

Page 3: Steps to use Flex

3

What is Flex Flex (The Fast Lexical Analyzer) It is a tool for generating programs that

perform pattern-matching on text. Flex is a free implementation of the

original Unix lex program.

Page 4: Steps to use Flex

4

What is Flex(Contd.)

Page 5: Steps to use Flex

5

Structure of a Flex File Format:

    

The definitions section: "name definition"The rules section: "pattern action"The user code section: "yylex() routine"

definitions %% rules %% user code

Required

Optional

Page 6: Steps to use Flex

6

Steps to use Flex Create a lex file which has lexical

specification rules for the lexer generator. It also has a main method within the file itself which repeatedly invokes the lexer and prints out the token and lexeme pairs.

Compile this file by running the following command

flex lexicalAnalyzer.l

Page 7: Steps to use Flex

7

Steps to use Flex(Contd.) A new file called lex.yy.c will be created

in the same folder in which the above file was present.

Compile this file by running the command:

gcc lex.yy.c to generate a.out executable. Test the executable using : ./a.out < "lexer_test.pas"

Page 8: Steps to use Flex

8

Example

Page 9: Steps to use Flex

9

Example(Contd.)

Page 10: Steps to use Flex

10

Example(Contd.)

Page 11: Steps to use Flex

11

Example(Contd.)

Page 12: Steps to use Flex

12

Summary Tool for generating programs that perform pattern-

matching on text Input file is a lex file(.l file) and output file is a c file. Input file is divided into 3 sections:

Definitions Rules User Code