0% found this document useful (0 votes)
42 views4 pages

Lab - 05 Lexical Analyzer Using JFlex

The document describes a lab assignment on lexical analysis with JFlex. The objectives are to successfully implement lexical analysis using JFlex. The tasks are to 1) write a JFlex program that capitalizes comments in a TINY program, and 2) extend the program to tokenize TINY code by recognizing integers, identifiers, operators, reserved words, and printing unrecognized tokens. Students are instructed to test their program on sample TINY code and output the tokenized results.

Uploaded by

SR Reliability
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views4 pages

Lab - 05 Lexical Analyzer Using JFlex

The document describes a lab assignment on lexical analysis with JFlex. The objectives are to successfully implement lexical analysis using JFlex. The tasks are to 1) write a JFlex program that capitalizes comments in a TINY program, and 2) extend the program to tokenize TINY code by recognizing integers, identifiers, operators, reserved words, and printing unrecognized tokens. Students are instructed to test their program on sample TINY code and output the tokenized results.

Uploaded by

SR Reliability
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Foundation University Islamabad,

Rawalpindi Campus

Department of Software Engineering

CS 344: Compiler Construction

Lab [05]: Lexical Analysis with JFlex (Part II)


Foundation University Islamabad,
Rawalpindi Campus

Lab [05]: Lexical Analysis with JFlex (Part II)


Introduction

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

1. Successful understanding/implementation of Lexical Analysis using Jflex

Tools/Software Requirement

1. Jflex on Linux or Windows platform

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):

Test your program on the following code in TINY

{
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

Your output should be as follows for the above program

{
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

Use the code given above in Task 1 as input.

Your output should resemble as follows


read <Reserved Word, read>
int <Unrecognized token, int>
x <identifier, x>
; <Statement Terminator, ;>
if <Reserved Word, if>
Foundation University Islamabad,
Rawalpindi Campus

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>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy