Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

2024 COMPILER DESIGN LABORATORY CSP1601 - Week 0.

docx

COMPILER DESIGN LABORATORY CSP1601

Week 0: Installation of Linux OS (Ubuntu) if required, installation of lex/flex, getting familiar with lex/flex syntax,
running sample program(s).
Instructions:
(If you have Ubuntu or WSL already installed, then go to Step no. 4)
1. Download virtualbox and install from:
Local server: \\192.168.1.52\Users\VirtualBox-7.0.6-155176-Win.exe
(copy and paste the above path in your browser)

Click Next and continue to finish the installation

2. Download latest Ubuntu OS from:


Local server: \\192.168.1.52\Users\ubuntu-22.04.1-desktop-amd64.iso
(copy and paste the above path in your browser)

3. Install the Ubuntu OS downloaded on the installed virtualbox


a. Open the installed virtualbox and click New

b. Give the os name and select the downloaded iso file (Ubuntu OS)

c. Set the username and password, hostname (Remember this password to be used later to login)

d. Set the memory (RAM) and CPU cores. RAM = 4096 MB, Processors=2

e. Set the disk space (25GB)

f. Click Finish

g. The Virtual Machine (Ubuntu) will automatically be powered up and installation will run

h. Once the installation is finished. Log in using the password you set before.

Note: Now, you have Ubuntu installed on virtualbox inside a Windows OS.
If you open the virtualbox windows, the mouse and keyboard will be captured by the virtualbox.
To release the mouse and keyboard capture from virtualbox, press Ctrl (on the right side of your
keyboard)

i. Install VBoxLinuxAdditions and also make vboxuser root/admin by doing the following

a) Click
Devices->Insert Guest Additions CD Image..
b) Open a terminal using the command
Ctrl + Alt + T

c) In the terminal run the following command (to switch to root user):
su -

Then enter the password that you set before

d) Run the following command:


cd /media/vboxuser/VBox_Gas_7.0.06/

e) Run the following command:


. /VBoxLinuxAdditions.run

Let the installation run and finish


f) Run the following command (to make vboxuser root/admin):
usermod -a -G sudo vboxuser

g) Run the following command to exit or to switch back to vboxuser


exit

4. Install lex/flex on Ubuntu


Run the following commands:
sudo apt-get update
sudo apt-get install flex

5. Run a sample lex/flex program


a. Create a sample lex/flex program (using gedit text editor). Save/name the sample lex/flex program as
sample.l

//sample program (source: geekforgeeks)


/*** Definition Section has one variable
which can be accessed inside yylex()
and main() ***/
%{
int count = 0;
%}

/*** Rule Section has three rules, first rule


matches with capital letters, second rule
matches with any character except newline and
third rule does not take input after the enter***/
%%
[A-Z] {printf("%s capital letter\n", yytext);
count++;}
. {printf("%s not a capital letter\n", yytext);}
\n {return 0;}
%%

/*** Code Section prints the number of


capital letter present in the given input***/
int yywrap(){}
int main(){

// Explanation:
// yywrap() - wraps the above rule section
/* yyin - takes the file pointer
which contains the input*/
/* yylex() - this is the main flex function
which runs the Rule Section*/
// yytext is the text in the buffer

// Uncomment the lines below


// to take input from file
// FILE *fp;
// char filename[50];
// printf("Enter the filename: \n");
// scanf("%s",filename);
// fp = fopen(filename,"r");
// yyin = fp;

yylex();
printf("\nNumber of Capital letters "
"in the given input - %d\n", count);

return 0;
}

b. Compile the lex/flex program (sample.l). The compilation will produce an output file called lex.yy.c
lex sample.l
c. Compile the lex.yy.c file (generated in the previous step) using gcc. This will produce the executable
code/file called a.out (a.out is the generated lexical analyzer / scanner)
gcc lex.yy.c
if you want to specify an output filename (e.g., sample_scanner) instead of the auto generated a.out,
use the following command
gcc lex.yy.c -o sample_scanner

d. Run the lexical analyzer/scanner


./a.out
or
./sample_scanner

You might also like