Steps to use Flex
description
Transcript of Steps to use Flex
1
Steps to use Flex
Ravi ChotraniNew York University
Reviewed ByProf. Mohamed Zahran
2
Outline What is Flex Structure of a Flex File Steps to use Flex Example Summary
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.
4
What is Flex(Contd.)
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
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
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"
8
Example
9
Example(Contd.)
10
Example(Contd.)
11
Example(Contd.)
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