Estd 1919
Estd 1919
Estd 1919
PRESENTED BY:
ROLL NO :- 11200220025
The compiler writer uses specialized tools that produce components that can easily
be integrated in the compiler and help implement various phases of a compiler.
compiler design
The program fragments written by the user are executed in the order in which the
corresponding regular expressions occur in the input stream.
Lex can generate analyzers in either "C" or "Ratfor”, a language which can be
translated automatically to portable Fortran.
The code for Lex was originally developed by Eric Schmidt and Mike Lesk.
compiler design
It is easy, for example, to interface Lex and Yacc, an open source program that
generates code for the parser in the C programming language.
Lex can perform simple transformations by itself but its main purpose is to
facilitate lexical analysis.
Scanning
Pre-processing
Strip out comments and white space
Macro functions
Lexical analysis
compiler design
Token
Types of words in source program
Keywords, operators, identifiers, constants, literal strings, punctuation
symbols(such as commas, semicolons)
Lexeme
Actual words in source program
Pattern
A rule describing the set of lexemes that can represent a particular token in
source program
Relation {<.<=,>,>=,==,<>}
Lexical Errors
compiler design
Contd.......
Pre-scanning
compiler design
Compilation Sequence
compiler design
What is Lex?
Contd......
Lex turns the user's expressions and actions (called source in this memo)
into the host general-purpose language.Tokens are the terminals of a
language
Properties Of yylex()
Reads the input stream generates tokens, returns O at the end of input stream.
Each time yylex() is called, the scanner continues processing the input from
where it last left off.
An Overview Of a Lex
compiler design
Files are divided into three sections, separated by lines that contain only two
percent signs, as follows:
Definition section %%
Rules section %%
C code section
The definition section defines macros and imports header files written in C. It
is also possible to write any C code here, which will be copied verbatim into
the generated source file.
compiler design
Contd......
The C code section contains C statements and functions that are copied
verbatim to the generated source file. These statements presumably contain
code called by the rules in the rules section. In large programs it is more
convenient to place this code in a separate file linked in at compile time.
compiler design
Example
The following is an example Lex file for the flex version of Lex.
It recognizes strings of numbers (positive integers) in the input, and simply prints them
out.
Contd......
[0-9]+ {
/* yytext is a string containing the matched text.*/
printf("Saw an integer: %s\n", yytext);
}
./ \n { /* Ignore all other characters. */
}
%%
/*** C Code section ***/
int main(void)
{
/* Call the lexer, then quit. */
yylex();
return 0;
}
compiler design
Contd......
If this input is given to flex, it will be converted into a C file, lex.yy.c. This can
be compiled into an executable which matches and outputs strings of integers.
Eg.
abc123z.!&*2gj6
The program will print:
This file is compiled and linked with the -Ifl' library to produce an executable.
When the executable is run, it analyzes its input for occurrences of the regular
expressions. Whenever it finds one, it executes the corresponding C code.
compiler design
Contd......