You are on page 1of 16

LEX PROGRAM

PRESENTED BY- ARINDAM CHAKRABORTY (10)


ABHISHEK CHAKRABORTY(51)
BABLU KR.SHAW(08)
INTRODUCTION AND USAGE OF LEX
03-04
CONTENTS
STRUCTURE OF LEX PROGRAM
05-08

CONFLICT RESOLUTION OF LEX


03

THE REJECT OPERATOR


04
LEX
•It is tool which Generate Lexical Analyzer.
•Lexical analyzer is first phase of compiler which take input
as source code and generate output as tokens
Token is a sequence of character that can be taken as a single logical
entity.
In programming language, keywords, constants, identifiers, strings,
numbers, operators and punctuations symbols can be considered as
tokens.

Token-name: token-name is an abstract symbol that is used during


syntax analysis.
Attribute-value: attribute-value points to an entry in the
symbol table for this token. Information from the symbol-table entry is
needed for semantic analysis and code generation.
CREATING A LEXICAL ANALYZER WITH LEX
Structure of Lex Programs
A Lex program has the following form:
declarations
°/.7.
translation rules
°/.0/.
auxiliary functions
The translation rules each have the form
Pattern { Action }
U
/* definitions of manifest constants
LT, LE, EQ, NE, GT, GE,
IF, THEN, ELSE, ID, NUMBER, RELOP */
'/.}
/* regular definitions */
delim [ \t\n]
ws {delim}+
letter [A-Za-z]
digit [0-9]
id {letter}({letter}|{digit})*
number {digit}+(\.{digit}+)?(E[+-]?{digit}
+)?
°/.y.
{ws} {/* no action and no return */}
if {return(IF);}
then {return(THEN);}
else {return(ELSE);}
{id} {yylval = (int) installID(); return(ID);}
{number} {yylval = (int) installNumO ; return(NUMBER);}
1 I <M {yylval = LT; return(RELOP) ;}
"<=" {yylval = LE; return(RELOP) ;}
II _ II {yylval = EQ; return(RELOP) ;}
"<>" {yylval = NE; return(RELOP) ;}
{yylval = GT; return(RELOP) ;}
{yylval = GE; return(RELOP) •}
int installlDQ
{/* function to install the lexeme, whose
first character is pointed to by yytext,
arid whose length is yyleng, into the
symbol table and return a pointer
thereto */
int installNumO {/* similar to installlD, but puts numerical
constants into a separate table */
}
A B F 2

If aaa* then 1; null


A 9 1 if [a-z]+ then 2;

?
A A A
Conflict Resolution
Thumb Rule:
r e g e t n i keyword

If ‘Integer’ return keywor


d;
identifier
s r e g e t n i

if [a-z]+ return identifi


er;
identifier
t n i
"<" { return(LESS);
}
LESSEQ
= < "=&”
}
{ return(EQUAL);

"<=" { return(LESSEQ
); }
She s++; She { s++; REJECT; }
he h++; he { h++; }
\n ; \n ;
. ; . ;
THANK YOU

References:
https://www.ibm.com/support/knowledgecenter/SSLTBW_2.1.0/com.ibm.zos.v2r1.bpxa600/bpxa683.htm

http://dinosaur.compilertools.net/lex/

You might also like