Lab Assgn2
Lab Assgn2
Lab Assignment 2 :
Objective : To get familiar with the use of Lex, the Lexical Analyzer generator tool. Write
regular expressions for a few basic tokens and generate the lexical analyzer through Lex.
Experiment with the generated scanner and perform experiments to understand its working.
Augment the scanner to add more functionality.
Practice Problems
Note that after logging into the server, you have to save all your work undertaken in a
laboratory session in a separate directory, as you had done in the first lab.
As soon as you have completed a problem call one of us to demonstrate the working.
P1. You are given a lex script in the file “exp1.l” which has been uploaded into your course
page in msteams. Generate a scanner by performing the steps given at the end of this
document, and give the executable a suitable name, such as “scan1”.
There are several inputs given to you for experimentation, these are also uploaded as files
with names, “inp1”, “inp2”, “inp3”, “inp4”.
input1 input2 input3 input4
ababcabc aba b ab bbbba ababab bbbba abababbbbba
Run your generated scanner on all the 4 inputs and examine the output generated by the
scanner. Explain with reasons the differences, if any, among the 4 outputs.
P2. Perform the change stated below in the file “exp1.l” while retaining all the other
statements without change. Save the changed script file by a separate name, say, “exp2.l”
Existing Code Changed Code
a*b {printf("Token 1 found \n");} a*b {printf("Token 1 found \n");return 1;}
c+ {printf("Token 2 found \n");} c+ {printf("Token 2 found \n");return 2;}
Now repeat the steps of generating a fresh scanner, give a different name, such scan2 and
then use it to recognize tokens in all the 4 input files. Record your findings and explain the
cause of the behavior of the scanner.
SP2024/CS 334 : Compiler Design Lab / Lab 2 :Working wth gcc / Anup & Supratim / 1
P3. Perform the change stated below in the file “exp2.l” while retaining all the other
statements without change. Save the changed script file by a separate name, say, “exp3.l”.
Existing Code Changed Code
int main() int main()
{ yylex(); { while(yylex());
} }
Redo all the tasks stated in P2 for this script file, “exp3.l”
P4. You are permitted to make minimal changes to the script file, “exp3.l” and create a new
file, “exp4.l” such that the output produced by the scanner generated from exp4.l is of the
form as shown below.
Behavior of scanner generated from “exp3.l” Behavior of scanner generated from “exp4.l”
on input1 : ababcabc on input1 : ababcabc
Token 1 found Token : ab found
Token 1 found Token : ab found
Token 2 found Token : c found
Token 1 found Token : ab found
Token 2 found Token : c found
Run your scanner over all the 3 remaining inputs and explain your findings.
P5. You can experiment with the functioning of the scanner by creating your own input files
and / or modifying the lex script to change the behavior to your liking.
SP2024/CS 334 : Compiler Design Lab / Lab 2 :Working wth gcc / Anup & Supratim / 2
LABORATORY HANDOUT
Step 2 : Compile the lex.yy.c program using gcc; you may need to provide the lex library,
using the option ll (read as hyphen el el where ‘hyphen el’ denotes reference to a library and
the last ‘el’ stands for lex). If your environment has already included the lex library, you can
skip the “-ll” option.
On successful compilation the executable code is generated by gcc and it has been named
as “scan1” using option “-o” and “scan1” is our executable scanner.
Step 3: For ease of experimentation, you may use relevant features of the shell, such as i/o
redirection for using scan1 over the different input files.
A small digression for those who are NOT familiar with shell programming. There are 3
operators used by the shell for i/o redirection (redirecting the standard input, standard output
and standard error files to other files chosen by an user rather than their default binding to
device files.
SP2024/CS 334 : Compiler Design Lab / Lab 2 :Working wth gcc / Anup & Supratim / 3
$ ./a.out 1> file2 equivalent, will be written to “file2”
2> $ ./a.out 2> file3 The error messages produced by a.out, using the
standard error channel, will be written to “file3”
>> or 2>> $ ./a.out >> file4 Same effect as > or 2> except that the data is
$ ./a.out 2>> file5 appended to the files instead of overwriting.
SP2024/CS 334 : Compiler Design Lab / Lab 2 :Working wth gcc / Anup & Supratim / 4