Lexical Analyzer Generator Lex (Flex in Recent Implementation)
Lexical Analyzer Generator Lex (Flex in Recent Implementation)
Lexical Analyzer Generator Lex (Flex in Recent Implementation)
2012-2013
Compiler Design 2
Contents:-
1- Lex
What is Lex
Structure of Lex Program
Lex Predefined Variables
Lex Library Routines
Install on Windows
Install on Linux (Ubuntu)
Run on Windows
Run on Linux (Ubuntu)
4- Decaf Example
Example Code
Run The Example
5- References
References
1. Lex
What is Lex?
• The main job of a lexical analyzer (scanner) is to break up an
input stream into tokens (tokenize input streams).
• Ex :-
a = b + c * d;
Definitions
%{
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
%}
Operators “\[]^-?.*+|()$/{}%<>
\$ = “$”
\\ = “\”
• Every character but blank, tab (\t), newline (\n) and the list above is
always a text character
Translation Rules
Pattern { action }
{DIGIT}+ { return(Integer); } // RD
• You can use your Lex routines in the same ways you use routines in
other programming languages (Create functions,
identifiers).
• yymore()
• yyless(n)
• yywarp()
Windows
1. Download the Windows version of FLEX
4. Now add the BIN folders of both folders into your PATH variable. In case
you don't know how to go about this, see how to do it on Windows
XP ,Windows Vista and Windows 7. You will have to add ';
C:\GNUWin32\bin;C:\Dev-Cpp' to the end of your PATH
C:\>gcc --version
gcc (GCC) 3.4.2 (mingw-special)
Copyright (C) 2004 Free Software Foundation, Inc.
this is free software; see the source for copying
conditions.
Downloading m4:
wget ftp://ftp.gnu.org/gnu/m4/m4-1.4.10.tar.gz
cd m4-1.4.10
Configuring m4:
./configure
Compiling m4:
make
Installing m4:
wget http://prdownloads.sourceforge.net/flex/flex-.5.33.tar.gz?download
cd flex-2.5.33
PATH=$PATH: /usr/local/m4
./configure
Compiling flex:
make
Installing flex:
Windows
Open CMD
To run Lex on a source file, type flex (lex source file.l) Command
It produces a file named lex.yy.c which is a C program for the lexical analyzer.
To run the lexical analyzer program, type a.exe < input file > output file Command
Linux (Ubuntu)
Open Terminal
To run Lex on a source file, type flex (lex source file.l) Command
It produces a file named lex.yy.c which is a C program for the lexical analyzer.
To compile lex.yy.c, type gcc lex.yy.c Command
To run the lexical analyzer program, type ./a.out < input file > output file Command
4. Decaf Example
CODE
//Definitions Section
%{
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
%}
DIGIT [0-9]
LITTER [a-zA-Z]
C [//]
S []
L [\n]
ID {LITTER}({LITTER}|{DIGIT}|_)*
double {DIGIT}+(\.{DIGIT}+)?([Ee][+-]?{DIGIT}+)?
hex (0[xX])({DIGIT}|[a-fA-F])*
String (\"({LITTER}|{S})*\")
Sym [~!@#$%^&*()_+|><""''{}.,]
Comment (({LITTER}|{DIGIT}|{Sym}|{S}|{L})*)
Comment2 ({C}+({LITTER}|{DIGIT}|{Sym}|{S})*{L})
%%
//Rule Section
{DIGIT}+ {
{double} {
printf( "A double: %s (%g)\n", yytext,
atof( yytext ) ); }
{hex} {
{String} {
void|int|double|bool|string|class|interface|null|this|extends|implements|for|while|if|else|
return|break|new|NewArray|Print|ReadInteger|ReadLine|true|false
"+"|"-"|"*"|"/"|"="|"=="|"<"|"<="|">"|">="|"!="|"&&"|"||"|"!"|";"|","|"."|"["|"]"|"("|")"|"{"|"}"
%%
int argc;
char **argv;
if ( argc > 0 )
else
yyin = stdin;
yylex();
}
int yywrap(){
return 1; }
Type this command a.exe<source prog file .txt> output file.txt (Windows)
or ./ a.out <source prog file .txt> output file.txt (Ubunto)
5. References
lex & yacc 2nd_edition John R. Levine, Tony
Mason, Doug Brown, O’reilly