Lab - 05 Lexical Analyzer Using JFlex
Lab - 05 Lexical Analyzer Using JFlex
Rawalpindi Campus
The lexical analyzer is the part of the compiler that reads the source text, it may also perform
certain secondary tasks at the user interface. One such task is stripping out comments and
white space in the form of blanks, tabs and new line characters, from the source program.
Another is correlating error messages from the compiler with the source program i.e. keeping a
correspondence between errors and source line numbers.
Objectives
Tools/Software Requirement
Description
Lexical analysis is the process of converting a sequence of characters into a sequence of tokens.
A program or function which performs lexical analysis is called a lexical analyzer, lexer or
scanner. A lexer often exists as a single function which is called by a parser or another function.
Lab Tasks
1. Write a Jflex input file that will produce a program that capitalizes all comments in a TINY
program (Capitalize only letters):
{
Factorial – Sample Program in TINY
}
read int x; { input an integer }
if 0 < x then
fact := 1;
repeat
fact := fact * x;
x := x – 1;
until x = 0;
write fact;;
Foundation University Islamabad,
Rawalpindi Campus
end
{
FACTORIAL – SAMPLE PROGRAM IN TINY
}
read int x; { INPUT AN INTEGER }
if 0 < x then
fact := 1;
repeat
fact := fact * x;
x := x – 1;
until x = 0;
write fact;;
end
2. Extend the Jflex program to process TINY language code done in previous lab by
incorporating following type of tokens:
1. Match integers constants i.e. numbers having 1 or more digits.
2. Match Identifiers having one or more letters.
3. Operators: +, -, *, /, =, <, (, ), ;, :=
4. Reserved Words: if, then, else, end, repeat, until, read, write
5. Remove the comments from the program
6. Print unrecognized characters with the message of unrecognized tokens
0 <Number, 0>
< <Less Than, < >
x <identifier, x>
then < Reserved Word, then>
fact <identifier, fact>
:= <Assignment, :=>
1 <Number, 1>
; < Statement Terminator, ;>
repeat < Reserved Word, repeat>
fact <identifier, fact>
:= <Assignment, :=>
fact <identifier, fact>
* <Multiplication, *>
x <identifier, x>
; < Statement Terminator, ;>
x <identifier, x>
:= <Assignment, :=>
x <identifier, x>
- <Minus, ->
1 <Number, 1>
; < Statement Terminator, ;>
until <Reserved Word, until>
x <identifier, x>
= <Equals, =>
0 <Number, 0>
; < Statement Terminator, ;>
write <Reserved Word, write>
fact <identifier, fact>
; < Statement Terminator, ;>
; < Statement Terminator, ;>
end <Reserved Word, end>