Академический Документы
Профессиональный Документы
Культура Документы
What is LEX?
Lex tool is a lex compiler. Lex is a utility or a tool that helps to generate your scanners rapidly. It
is mainly used in lexical analysis phase in order to identify the tokens using regular expressions.
What is the purpose of using LEX?
The main purpose of a lexical analyzer (scanner) is to break up an input stream into tokens i.e(to
identify the meaning and its use).
For example: a = b + c * d
ID ASSIGN ID PLUS ID MULTI ID SEMI
Use of LEX
lex.l
lec.yy.c
LEX Compiler
Compiler
Input Stream
lec.yy.c
a.out
Sequence of tokens
a.out
lex.l:
It is an input file written in a compiler language which describes the generation of a lexical
analyzer. The lex compiler transforms lex.l to a C program known as lex.yy.c.
lex.yy.c
The file is a compilation file to a C compiler. The C compiler after the compilation produces
a new file called as a.out. The C compiler performs the lexical analysis which takes the
stream of input characters and produces a stream of tokens.
yylval
It is a global variable which is shared by lexical analyzer and parser to return the name and
an attribute value of a token.
The attribute value can be a numeric code, pointer to a symbol table or nothing.
Another tool for lexical analyzer generation is Flex.
Structure of a Lex program
Lex source program is separated into three sections by %% delimiters. The general form or the
syntax of a lex program can be as follows:
Declarations
%%
Definitions
%%
Translation Rules
%%
Auxiliary Functions or User Code
%%
[Required]
[Required]
[Required]
[Optional]
Declarations: This section includes the declaration of variables, constants and regular definitions.
Definitions: It contains declarations of names, characters and operators.
Translation Rules: It consists of regular expressions and code segments.
The syntax can be given as Pattern {action}.
The actions may be a C/C++ code. Pattern is a regular expression or a regular definition. Action
refers to the segments of code.
Auxiliary Functions or User Code: These functions are compiled and loaded with the lexical
analyzer. Lexical analyzer produced by the lex starts its process by reading one character at a
time until a valid match for a pattern is found. Once a pattern match is found, the associated
action takes place to produce the tokens. The token is then given to the parser for further
processing.
IF (I, J)
=5
The above IF statement issues a conflict whether to produce IF as an array or a keyword. Hence
to resolve this, the lex rule can be follows:
IF /\
(.*\) {
Letter
Installation