0% found this document useful (0 votes)
584 views

Compiler Design PPT Final

This document describes a LEX program presented by Arindam Chakraborty, Abhishek Chakraborty, and Babu Kr. Shaw. It contains an introduction to LEX and lexical analysis, the structure of LEX programs including declarations, translation rules, and auxiliary functions. It provides examples of regular expressions and actions to return tokens for keywords, identifiers, numbers, and relational operators. It also describes functions for installing lexemes into symbol tables and handling conflict resolution.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
584 views

Compiler Design PPT Final

This document describes a LEX program presented by Arindam Chakraborty, Abhishek Chakraborty, and Babu Kr. Shaw. It contains an introduction to LEX and lexical analysis, the structure of LEX programs including declarations, translation rules, and auxiliary functions. It provides examples of regular expressions and actions to return tokens for keywords, identifiers, numbers, and relational operators. It also describes functions for installing lexemes into symbol tables and handling conflict resolution.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 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