SlideShare a Scribd company logo
Module 4
Lexical Analyser
Compilation Sequence
Creating a Lexical Analyzer with Lex:
1. First a specification of a lexical analyzer is prepared by
creating a program lex.l in the lex language.
2. lex.l is run through the lex compiler to produce a C
program lex.yy.c
3. Finally, lex.yy.c is run through the C compiler to produce
an object program a.out
Creating a Lexical Analyzer with Lex:
Lex
Compiler
Lex Source Program
lex . l
lex.yy.c
C
Compiler
a. out
lex.yy.c
Lexical Analyses phase reads
characters (src pgm)
stream of tokens
an identifier,
a keyword (if, while etc.)
a punctuation character or
a multi-character operator like : =
The general format of Lex source is:
%{
Definitions
%}
%%
{rules}
%%
{user subroutines}
Regular
Expression
Description
A-Z, 0-9, a-z Characters and numbers that form part of the pattern.
. Matches any single character except n.
*
Match with zero or more occurrences of the preceding pattern
or expression.
Example: [0-9]*
+
Matches with one or more occurrences of the preceding
pattern.
Example: [a-z]+
?
Match with zero or one occurrence of the preceding pattern or
expression.
Example: -?[0-9]* : starts with an – sign
^
1. Matches the beginning of a line as the first character.
Example: ^verb means input starts with a verb word
2. Used as for negation in Character class.
Example: [^0-9]+ means Except 0-9
Characters that form the regular expressions:
[ ]
A character class. Matches any character in the brackets. – Used to
denote a range.
Example: [A-Z] implies all characters from A to Z.
$
Matches the end of the line as the last character of the pattern.
Example: a+b$
{ }
Indicates how many times a pattern can be present.
Example: A{1,3} implies one or three occurrences of A may be
present.
| Logical OR between expressions.

Used to escape meta characters. Also used to remove the special
meaning of characters as defined in this table.
Example: ” [a-z]+ ”
“ ”
The string written in quotes matches literally.
Example: “hello”
/
Look ahead. Matches the preceding pattern only if followed by the
succeeding expression. Example: A0/1 matches A0 only if A01 is
the input.
( )
Groups a series of regular expressions.
Example: ([0-9]+) | ([0-9]*.[0-9]+)
LEX Actions:
Action Description
BEGIN
It indicates the start state. The lexical analyzer starts at
state 0.
ECHO It emits the input as it is.
Char *yytext
When the lexer matches or recognizes the token from the
input token then the lexeme is stored in a null-terminated
string called yytext.
FILE *yyin It is the standard input file.
FILE *yyout It is the standard output file.
int yyleng
It stores the length or number of characters in the input
string.
yylex( )
This is an important function. As soon as a call to yylex( ) is
encountered, the scanner starts scanning the source
program.
yywrap( ) It calls when the scanner encounters the end of file.
yylval It gives the value associated with the token.
Some examples of regular expression and their meanings are given in the
following table.
Regular
Expression
Meaning
joke[rs] Matches either jokes or joker
A{1,2}shis+ Matches AAshis, Ashis, AAshi, Ashi
(A[b-e])? Matches zero or one occurrences of A followed by
any character from b to e.
 Tokens in Lex are declared like variable name in C.
 Every token has an associated expression.
Token Associated Expression Meaning
number ([0-9])+ 1 or more occurrences of
a digit
chars [A-Za-z] Any character
blank " " A blank space
word (chars)+ 1 or more occurrences
of chars
variable (chars)+(number)*(chars)*( number)*

More Related Content

DOCX
LEX & YACC
PPTX
Compiler Design_LEX Tool for Lexical Analysis.pptx
PPT
Chapter-2-lexical-analyser and its property lecture note.ppt
PPT
compiler Design laboratory lex and yacc tutorial
PPTX
Language for specifying lexical Analyzer
PPTX
module 4.pptx
PPT
Module4 lex and yacc.ppt
DOCX
Compiler Design
LEX & YACC
Compiler Design_LEX Tool for Lexical Analysis.pptx
Chapter-2-lexical-analyser and its property lecture note.ppt
compiler Design laboratory lex and yacc tutorial
Language for specifying lexical Analyzer
module 4.pptx
Module4 lex and yacc.ppt
Compiler Design

Similar to module 4_ Lex_new.ppt (20)

PPT
LEX lexical analyzer for compiler theory.ppt
PPTX
Introduction of flex
PPT
Lex and Yacc Tool M1.ppt
PPT
system software
PPTX
Lex programming
PPTX
Ch 2.pptx
PDF
lecture_lex.pdf
PPT
LexicalAnalysis in Compiler design .pt
PPT
Compiler Design in Engineering for Designing
PDF
LANGUAGE PROCESSOR
DOCX
Yacc topic beyond syllabus
PPTX
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
PPT
Yacc lex
DOC
Pcd question bank
PPTX
A Role of Lexical Analyzer
PPTX
Lex & yacc
PPTX
Lexical analyzer generator lex
PDF
It’s sometimes useful to make a little language for a simple problem.pdf
PDF
Handout#02
PPT
atc 3rd module compiler and automata.ppt
LEX lexical analyzer for compiler theory.ppt
Introduction of flex
Lex and Yacc Tool M1.ppt
system software
Lex programming
Ch 2.pptx
lecture_lex.pdf
LexicalAnalysis in Compiler design .pt
Compiler Design in Engineering for Designing
LANGUAGE PROCESSOR
Yacc topic beyond syllabus
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Yacc lex
Pcd question bank
A Role of Lexical Analyzer
Lex & yacc
Lexical analyzer generator lex
It’s sometimes useful to make a little language for a simple problem.pdf
Handout#02
atc 3rd module compiler and automata.ppt
Ad

Recently uploaded (20)

PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
web development for engineering and engineering
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPT
Project quality management in manufacturing
DOCX
573137875-Attendance-Management-System-original
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Operating System & Kernel Study Guide-1 - converted.pdf
Foundation to blockchain - A guide to Blockchain Tech
Lecture Notes Electrical Wiring System Components
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
bas. eng. economics group 4 presentation 1.pptx
web development for engineering and engineering
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Sustainable Sites - Green Building Construction
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
Project quality management in manufacturing
573137875-Attendance-Management-System-original
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Ad

module 4_ Lex_new.ppt

  • 3. Creating a Lexical Analyzer with Lex: 1. First a specification of a lexical analyzer is prepared by creating a program lex.l in the lex language. 2. lex.l is run through the lex compiler to produce a C program lex.yy.c 3. Finally, lex.yy.c is run through the C compiler to produce an object program a.out
  • 4. Creating a Lexical Analyzer with Lex: Lex Compiler Lex Source Program lex . l lex.yy.c C Compiler a. out lex.yy.c
  • 5. Lexical Analyses phase reads characters (src pgm) stream of tokens an identifier, a keyword (if, while etc.) a punctuation character or a multi-character operator like : =
  • 6. The general format of Lex source is: %{ Definitions %} %% {rules} %% {user subroutines}
  • 7. Regular Expression Description A-Z, 0-9, a-z Characters and numbers that form part of the pattern. . Matches any single character except n. * Match with zero or more occurrences of the preceding pattern or expression. Example: [0-9]* + Matches with one or more occurrences of the preceding pattern. Example: [a-z]+ ? Match with zero or one occurrence of the preceding pattern or expression. Example: -?[0-9]* : starts with an – sign ^ 1. Matches the beginning of a line as the first character. Example: ^verb means input starts with a verb word 2. Used as for negation in Character class. Example: [^0-9]+ means Except 0-9 Characters that form the regular expressions:
  • 8. [ ] A character class. Matches any character in the brackets. – Used to denote a range. Example: [A-Z] implies all characters from A to Z. $ Matches the end of the line as the last character of the pattern. Example: a+b$ { } Indicates how many times a pattern can be present. Example: A{1,3} implies one or three occurrences of A may be present. | Logical OR between expressions. Used to escape meta characters. Also used to remove the special meaning of characters as defined in this table. Example: ” [a-z]+ ” “ ” The string written in quotes matches literally. Example: “hello” / Look ahead. Matches the preceding pattern only if followed by the succeeding expression. Example: A0/1 matches A0 only if A01 is the input. ( ) Groups a series of regular expressions. Example: ([0-9]+) | ([0-9]*.[0-9]+)
  • 9. LEX Actions: Action Description BEGIN It indicates the start state. The lexical analyzer starts at state 0. ECHO It emits the input as it is. Char *yytext When the lexer matches or recognizes the token from the input token then the lexeme is stored in a null-terminated string called yytext. FILE *yyin It is the standard input file. FILE *yyout It is the standard output file. int yyleng It stores the length or number of characters in the input string. yylex( ) This is an important function. As soon as a call to yylex( ) is encountered, the scanner starts scanning the source program. yywrap( ) It calls when the scanner encounters the end of file. yylval It gives the value associated with the token.
  • 10. Some examples of regular expression and their meanings are given in the following table. Regular Expression Meaning joke[rs] Matches either jokes or joker A{1,2}shis+ Matches AAshis, Ashis, AAshi, Ashi (A[b-e])? Matches zero or one occurrences of A followed by any character from b to e.
  • 11.  Tokens in Lex are declared like variable name in C.  Every token has an associated expression. Token Associated Expression Meaning number ([0-9])+ 1 or more occurrences of a digit chars [A-Za-z] Any character blank " " A blank space word (chars)+ 1 or more occurrences of chars variable (chars)+(number)*(chars)*( number)*