120 likes | 429 Views
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)
E N D
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) • It is a tool for generating programs that perform pattern-matching on text. • Flex is a free implementation of the original Unix lex program.
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
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 lexerand prints out the token and lexeme pairs. • Compile this file by running the following command flex lexicalAnalyzer.l
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: gcclex.yy.c to generate a.out executable. • Test the executable using : ./a.out < "lexer_test.pas"
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