AIM: To Study About Lexical Analyzer Generator (LEX) and Flex (Fast Lexical Analyzer) Lexical Analyzer Generator (LEX)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Compiler Design Practical-3

Practical-3

AIM: To Study about Lexical Analyzer Generator(LEX) and Flex(Fast


Lexical Analyzer)

Lexical Analyzer Generator(LEX):

 Lex stands for "lexical analyzer generator."


 Lex is a program designed to generate scanners, which recognize lexical patterns in
text.
 Its main purpose is to facilitate lexical analysis, the processing of character sequences
such as source code to produce symbol sequences called tokens for use as input to
other programs such as parsers.
 Lex can be used with a parser generator to perform lexical analysis.
 Lex is a lexical analyser tool mostly used with yacc parse generator .
 It lexically analyses (i.e. matches) the patterns (regular expressions) given as input
string or as a file.
 Following are the lex predefined functions and variables :-
o yyin :- the input stream pointer (i.e it points to an input file which is to be
scanned or tokenised), however the default input of default main() is stdin .

o yylex() :- implies the main entry point for lex, reads the input stream generates
tokens, returns zero at the end of input stream . It is called to invoke the lexer
(or scanner) and each time yylex() is called.

o yytext :- a buffer that holds the input characters that actually match the pattern
(i.e lexeme) or say a pointer to the matched string .

o yyleng :- the length of the lexeme .

o yylval :- contains the token value .

o yyval :- a local variable .*

o yyout :- the output stream pointer (i.e it points to a file where it has to keep the
output), however the default output of default main() is stdout .

o yywrap() :- it is called by lex when input is exhausted (or at EOF). default


yywrap always return 1.

o yymore() :- returns the next token .

o yyless(k) :- returns the first k characters in yytext .

o yyparse() :- it parses (i.e builds the parse tree) of lexeme .*

161040107059 1
Compiler Design Practical-3

Fast Lexical Analyzer(FLEX):

 FLEX (fast lexical analyzer generator) is a tool/computer program for generating


lexical analyzers (scanners or lexers) written by Vern Paxson in C around 1987.
 It is used together with Berkeley Yacc parser generator or GNU Bison parser
generator. Flex and Bison both are more flexible than Lex and Yacc and produces
faster code.
 Bison produces parser from the input file provided by the user. The function yylex() is
automatically generated by the flex when it is provided with a .l file and this yylex()
function is expected by parser to call to retrieve tokens from current/this token stream.

 Program Structure:
In the input file, there are 3 sections:

1. Definition Section: The definition section contains the declaration of variables,


regular definitions, manifest constants. In the definition section, text is enclosed
in “%{ %}” brackets. Anything written in this brackets is copied directly to the
file lex.yy.c

Syntax:
%{
// Definitions
%}

2. Rules Section: The rules section contains a series of rules in the form: pattern
action and pattern must be unintended and action begin on the same line in {}
brackets. The rule section is enclosed in “%% %%”.

Syntax:
%%
pattern action
%%

3. User Code Section: This section contain C statements and additional functions.
We can also compile these functions separately and load with the lexical analyzer.

161040107059 2
Compiler Design Practical-3

Basic Program Structure:


%{
// Definitions
%}

%%
Rules
%%

User code section

161040107059 3

You might also like