Steps to use Flex
description
Transcript of Steps to use Flex
![Page 1: Steps to use Flex](https://reader033.fdocuments.us/reader033/viewer/2022050809/56815f4a550346895dce2742/html5/thumbnails/1.jpg)
1
Steps to use Flex
Ravi ChotraniNew York University
Reviewed ByProf. Mohamed Zahran
![Page 2: Steps to use Flex](https://reader033.fdocuments.us/reader033/viewer/2022050809/56815f4a550346895dce2742/html5/thumbnails/2.jpg)
2
Outline What is Flex Structure of a Flex File Steps to use Flex Example Summary
![Page 3: Steps to use Flex](https://reader033.fdocuments.us/reader033/viewer/2022050809/56815f4a550346895dce2742/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050809/56815f4a550346895dce2742/html5/thumbnails/4.jpg)
4
What is Flex(Contd.)
![Page 5: Steps to use Flex](https://reader033.fdocuments.us/reader033/viewer/2022050809/56815f4a550346895dce2742/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050809/56815f4a550346895dce2742/html5/thumbnails/6.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050809/56815f4a550346895dce2742/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050809/56815f4a550346895dce2742/html5/thumbnails/8.jpg)
8
Example
![Page 9: Steps to use Flex](https://reader033.fdocuments.us/reader033/viewer/2022050809/56815f4a550346895dce2742/html5/thumbnails/9.jpg)
9
Example(Contd.)
![Page 10: Steps to use Flex](https://reader033.fdocuments.us/reader033/viewer/2022050809/56815f4a550346895dce2742/html5/thumbnails/10.jpg)
10
Example(Contd.)
![Page 11: Steps to use Flex](https://reader033.fdocuments.us/reader033/viewer/2022050809/56815f4a550346895dce2742/html5/thumbnails/11.jpg)
11
Example(Contd.)
![Page 12: Steps to use Flex](https://reader033.fdocuments.us/reader033/viewer/2022050809/56815f4a550346895dce2742/html5/thumbnails/12.jpg)
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