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

Name: Kunal Goswami Roll Number: 17ETCS002086

Compilers Laboratory
B.Tech. 6th Semester
Batch: 2017

Department: Computer Science and Engineering

Faculty of Engineering & Technology


Ramaiah University of Applied Sciences
Name: Kunal Goswami Roll Number: 17ETCS002086
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013

Faculty Engineering & Technology


Programme B. Tech. in Computer Science and Engineering
Course Compilers Laboratory
Year/Semester 2017/6th Semester
Course Code CSC312A

List of Experiments

LEX PROGRAMS

1. Program to count the number of vowels and consonants in a given string.


2. Program to find the longest word in a given string.
3. Program to count number of:
a. +positive and –negative integers
b. +positive and –negative fractions
4. Program to count the number of characters, words, spaces, end of lines in a given input file.
5. Program to count the no of ‘scanf’ and ‘printf’ statements in a C program. Replace them with
‘readf’ and ‘writef’ statements respectively.
6. Program to perform addition, subtraction, multiplication, division and power. Note: Without
Precedence.

YACC& LEX PROGRAMS

7. Program to recognize a valid variable, which starts with a letter, followed by any number of
letters or digits.
8. Program to evaluate an arithmetic expression involving operating +, -, * and /
9. Program to recognize strings ‘aaab’, ‘abbb’, ‘ab’ and ‘a’ using grammar (a^nb^m, n>=0,
m>=0)
Name: Kunal Goswami Roll Number: 17ETCS002086

Laboratory 1
Title of the Laboratory Exercise: Program to count the number of vowels and consonants in a given
string

1. Introduction and Purpose of Experiment


Students learn to use Lex program to find out vowels and consonants in a given string.

2. Aim and Objectives


Aim

● To write a program to count the number of vowels and consonants in a given string
Objectives

At the end of this lab, the student will be able to

• Define regular expression for vowels and consonants


• Count the number of vowels and consonants

3. Experimental Procedure
Students are required to carry out the following steps:

• Algorithm
• Write the Lex program
• Compile and execute the program (steps)
• Complete the documentation for the given problem

4. Presentation of Results

Figure 1 Simple program


Name: Kunal Goswami Roll Number: 17ETCS002086

The program presented in Figure 1 returns the string “Welcome to Konoha” when the user
enters “gm”

Output screenshot:

Figure 2 Program output

Code for vowels and consonants counting:

%{

int V=0; \\integer to store the number of vowels


int C=0; \\integer to store the number of consonant

%}

%%

[aeiou] /*if a vowel is found*/ {printf("vowel found!\n "); V++; }

[a-zA-Z] /*if a consonant is found*/ { C++; }


. ;

%%

int yywrap()

return 1;

main(){
Name: Kunal Goswami Roll Number: 17ETCS002086

yylex();

printf("Number of consonents = %d\nNumber of vowels = %d\n",V,C);

}
Output Screenshot:

Figure 3 Test case 1

Figure 4 Test case 2

5. Analysis and Discussions


Lex tool is used to split the text into a list of tokens based on the regular expression defined.

In this experiment our objective is to write a Lex program which counts the number of
vowels and consonants in the entered string.

Table 1 Test data analysis


Test case Input Expected output Program output Test result

1 “hello” 2 vowels and 3 2 vowels and 3 Pass


consonants consonants

2 “this is a string” 2 vowels and 3 2 vowels and 3 Pass


consonants consonants
Name: Kunal Goswami Roll Number: 17ETCS002086

6. Conclusions
Grammar was written for the required program. This grammar was implemented using Lex
tool. The program has been thoroughly tested. From Figures 3, 4 and Table 1 the working of
the program can be verified.
7. Comments
a. Limitations of Experiments

The ‘lex’ tool will not recognize comments with ‘\\’. An error will be returned.

b. Limitations of Results

• The user has to manually end the input string using “ctrl+d” command. The program
would be user friendly if the user could enter the number of character of the input string
beforehand.
• The program written does not recognize capital vowels

c. Learning happened

• Basic compilation process


• Need for lexical analysis
• Working of lexical analyzer
• Using ‘lex’ tool
d. Recommendations

Usage of YACC tool can be learned to understand the compilation process better

Bibliography

1. What is Lex (lexical analyzer generator)? - Definition from WhatIs.com. Available at:
http://searchenterpriselinux.techtarget.com/definition/Lex [Accessed 30 Jan. 2017].

2. What is Lex? What is Yacc? Available at: https://luv.asn.au/overheads/lex_yacc/ [Accessed


30 Jan. 2017].
Name: Kunal Goswami Roll Number: 17ETCS002086

Laboratory 2
Title of the Laboratory Exercise: Program to find the longest word in a given string.

1. Introduction and Purpose of Experiment


Students learn to use Lex program to find out vowels and consonants in a given string.

2. Aim and Objectives


Aim: To write a Program to find the longest word in a given string.

● At the end of this lab, the student will be able to

• Define regular expression for vowels and consonants

• Count the number of vowels and consonants

3. Experimental Procedure
Students are required to carry out the following steps:

• Algorithm

• Write the Lex program


• Compile and execute the program (steps)

• Complete the documentation for the given problem

4. Presentation of Results Algorithm:

Declare variables ‘Lword’ (string) and ‘length’ (int, initialize to 0)

For each input word:

o Check if the length of the current word is greater than ‘length’ o

If yes, store the current word in ‘Lword’ and update the length’ Print

the ‘Lword’ and ‘length’

Lex code to find the longest word [first occurrence]:

%{
#include <strings.h>
int longest = 0; char
longword[60];
Name: Kunal Goswami Roll Number: 17ETCS002086

%}
%%
[a-zA-Z]+ { if(yyleng > longest){
longest = yyleng;
strcpy(longword,yytext);

}
}
. | \n ;
%%
int yywrap()
{
return 1;
}

main(){
printf("Enter a string\n"); yylex();

printf("Longest word : %s | Length : %d \n ",longword,longest);

}
Output Screenshot:

Figure 1 Test case 1

Figure 2 Test case 2

Demonstration of first occurrence is presented in Figure 4. Even though the word ‘good’ is of length
4, the first longest word is chosen.
Name: Kunal Goswami Roll Number: 17ETCS002086

Program to print the longest word, last occurrence: [change in the code is highlighted]

%{
#include <strings.h>
int longest = 0; char
longword[60];

%}
%%

[a-zA-Z]+ { if(yyleng >= longest){

longest = yyleng;
strcpy(longword,yytext);

}
}
. | \n ;
%%
int yywrap()
{
return 1;
}
main(){ printf("Enter a
string\n"); yylex();
printf("Longest word :
%s | Length : %d \n
",longword,longest);

Demonstration of longest last word occurrence program:

Figure 3 Test Case 3

Even though ‘sham’ has length 4, the program returned ‘good’ due to the fact that ‘good’ occurs
after ‘sham’.
Name: Kunal Goswami Roll Number: 17ETCS002086

5. Analysis and Discussions


Lex tool is used to split the text into a list of tokens based on the regular expression defined.

In this experiment our objective is to write a Lex program which finds the longest word in
the sentence entered.

Table 1 Test data analysis


Test Input Expected output Program output Test result
case

1 “This is a sentence” sentence sentence Pass

2 “sham is a good boy” sham sham Pass

3 “sham is a good boy” good good Pass

6. Conclusions
Grammar was written for the required for the longest word program. This grammar was
implemented using Lex tool. When there are multiple words of same longest length in a
sentence, the first program outputs the first longest occurrence whereas the second
program outputs the last longest occurrence. The program has been thoroughly tested.
From Figures 1, 2, 3 and Table 1 the working of the program can be verified.
7. Comments
a. Limitations of Experiments

If there is a word ‘co-responding’, the program will not consider it as one word.

b. Limitations of Results

The program does not consider numbers in a sentence

c. Learning happened

• Using ‘lex’ tool


• Designing grammar was mid-level programs

• Altering the algorithm to obtain required results

d. Recommendations

The program can be improved by displayed the longest AND the shortest words in the
sentence entered.
Name: Kunal Goswami Roll Number: 17ETCS002086

Laboratory 3
Title of the Laboratory Exercise: Program to count no of +positive and –negative numbers

1. Introduction and Purpose of Experiment


Students learn to use Lex program to find out vowels and consonants in a given string.

2. Aim and Objectives


Aim: To write a Program to count no of:

a. +positive and –negative integers

b. +positive and –negative fractions

3. Experimental Procedure
Students are required to carry out the following steps:

• Algorithm
• Write the Lex program

• Compile and execute the program (steps)


• Complete the documentation for the given problem

4. Presentation of Results

Grammar for positive integers: [+]?[0-9]+


The initial plus symbol is optional because numbers without any sign are assumed to be
positive. [0-9] can occur one or more times.
Grammar for positive integers: [-][0-9]+
In this case, - sign is not optional. Therefore, ‘?’ is omitted.
Lex code to count positive and negative integers:

%{
#include <stdio.h>

int positive=0; //initializing to zero int


negative=0;
%}

%%

[+]?[0-9]+ { positive++;} //increment if positive number is found


Name: Kunal Goswami Roll Number: 17ETCS002086

[-][0-9]+ { negative++;}

.;

%%

int yywrap(){ return


1;
}

int main(){

printf("Enter random positive and negative numbers\n"); yylex();


printf(" Positive = %d \t Negative = %d \t ", positive,negative); return
0;

Output Screenshot:

Figure 1 Test case 1

Figure 2 Test case


Name: Kunal Goswami Roll Number: 17ETCS002086

Program to count positive, negative and decimal numbers:

%{

#include <stdio.h> int


positive=0; //initialization
int decimal_positive=0; int
decimal_negative=0; int
negative=0;
%}

%%
[+]?[0-9]+[\.][0-9]+ { decimal_positive++;}
[-][0-9]+[\.][0-9]+ { decimal_negative++;}
[+]?[0-9]+ { positive++;}
[-][0-9]+ { negative++;}
.;
%%

int yywrap(){
return 1;
}

void main(){ printf("Enter random positive, negative and decimal


numbers\n"); yylex();
printf(" Positive = %d \t Negative = %d \t Positive Decimal = %d\t Negative Decimal = %d\n",
positive,negative,decimal_positive,decimal_negative);

}
Name: Kunal Goswami Roll Number: 17ETCS002086

Figure 3 Test Case 3

Figure 4 Test case 4

5. Analysis and Discussions

Explanation of Positive Decimal regular expression: [+]?[0-9]+[\.][0-9]+

• ‘+’ is optional => ?

• 0-9 = digit
• Digit can repeat any number of times.
• ‘.’ is denoted using escape sequence

• Digit can repeat any number of times.


Name: Kunal Goswami Roll Number: 17ETCS002086

Table 1 Program 1 test data analysis


Test Input Expected output Program output Test
case result
Positive Negative Positive Negative

1 +5, -5, +6568, 2 2 2 2 Pass


-865

2 +7, -6, 5, 89 3 1 3 1 Pass

Table 2 Program 2 output analysis


Test Input Expected output Program output Test
case result
Positive Negative Negative Positive Positive Negative
decimal decimal

1 +5, 5, 5.8, 2 1 1 1 From Figures 3 and Pass


4, it can be noted
-6.5, -6
that the program
2 -9.5, 5.1, 1 1 2 1 results obtained Pass
are as expected.
5, -7,
-1021.5

6. Conclusions
Grammar was written for the positive and negative numbers. This grammar was
implemented using Lex tool. From Figures 1, 2 and the analysis in Table 1, the working of this
program is verified.

The program developed was improved by designing regular expressions to detect positive
and negative decimal numbers. Thorough testing was done. Based on Figures 3, 4 and the
analysis in Table 2, the working of the improved program is successfully verified.
Name: Kunal Goswami Roll Number: 17ETCS002086

7. Comments
a. Limitations of Experiments

• The program does not account for fractional numbers. For instance, 1/3. It will not
be counted as a positive decimal by the program.

b. Limitations of Results

• The program developed considers 4 mutually exclusive sets. But the fact is that
‘positive decimal’ numbers must be counted under ‘positive’ set too.

• The program recognizes 0.5 but not .5 as a positive decimal

c. Learning happened

• Using ‘lex’ tool


• Meaning and purpose of yy-wrap.
• Designing grammar was medium difficulty problems
• Designing grammar to detect positive and negative decimal numbers

d. Recommendations

• The program can be improved by displayed the longest AND the shortest words in
the sentence entered.

Bibliography

1. Compilers: Principles, Techniques, and Tools


Aho, A. V., Lam, M. S., Sethi, R., and Ullman, J. D. (2007) [Accessed 5 Feb. 2017].
Name: Kunal Goswami Roll Number: 17ETCS002086

Laboratory 4
Title of the Laboratory Exercise: Program to count the number of characters, words, spaces,
end of lines in a given input file.

1. Introduction and Purpose of Experiment


Students learn to use Lex program to find out vowels and consonants in a given string.

2. Aim and Objectives


Aim: To write a program to count the number of characters, words, spaces, end of
lines in a given input file.

● At the end of this lab, the student will be able to

• Define regular expression for vowels and consonants


• Count the number of vowels and consonants

3. Experimental Procedure
Students are required to carry out the following steps:

• Algorithm
• Write the Lex program

• Compile and execute the program (steps)


• Complete the documentation for the given problem

4. Presentation of Results
Algorithm:
The idea is to recognize each word and find the summation of word-lengths to find the
total number of characters.
The number of ‘end of lines’ are found by counting the ‘\n’ characters in the input file.

• Parse the input file:

• If a word is found:
o Increment word-count by 1.

o Find the word length. Add the word length to character-count.


Name: Kunal Goswami Roll Number: 17ETCS002086

• If a space is found o Increment the

space-count by 1.

• If a newLine character (‘\n’) is found


o Increment the newLine-count by
1.

• After parsing the entire input file, o


Print all the count variables.

Lex code to find the number of words, character, spaces and new line characters:

%{

#include
<stdio.h>
int
spaces=0;
int
words=0;
int
charac=0;
int
newLine=0;

%}

%%

/*Following line will recognize each word and add the

coresponding length to the variable 'charac'*/

[a-zA-Z]+ { words++; charac+=yyleng; }

[ ] { spaces++; }

\n { newLine++;}
Name: Kunal Goswami Roll Number: 17ETCS002086

. {charac++;}

%%

int yywrap(){
return 1;
}

int main(int argc, char const *argv[])

yyin=fopen(argv[1],"r");
yylex();
printf(" spaces = %d \t words = %d \t char = %d \t newLine = %d \n",
spaces,words,charac,newLine);
return 0;

Output Screenshot:

Figure 1 Test case 1

Figure 2 Test case 2 input data


Name: Kunal Goswami Roll Number: 17ETCS002086

Figure 3

Figure 4

Figure 5

5. Analysis and Discussions

Table 1 Test results analysis


Test Input Expected output Program output Test result
case

1 Hello world Spaces = 2 words spaces = 2 words Pass


= 4 char = 25 = 4 char = 25
Lex programming!
newLine = 2 newLine = 2

2 This point…….useless” spaces = spaces = Pass


67 words 67 words
= 73 char = = 73 char =
368 newLine 368 newLine
=7 =7

3 Five years……...at work. spaces = 132 spaces = 132 Pass


words = 148 char words = 148 char
= 711 newLine = 711 newLine
= 13 = 13

Referring to input file in Figure 1, there are only two lines in the input file. Even
though a ‘\n’ is not explicitly mentioned at the end of second line, the number of
Name: Kunal Goswami Roll Number: 17ETCS002086

newlines ‘\n’ in the program output is found to be 2. This is due to the fact that OS
makes sure a text file always ends with a ‘\n’ (Rudolph, 2009).

This is applicable to all other test cases too.

Referring to Figure 2, the output obtained is as expected. There are 7 lines in the
textfile. Therefore the number of new lines is output as 7 by the program in Figure 3.

Referring to Figure 4, there are two paragraphs in the input text file. The program is
successful is detecting the empty newline (line number 6). In Figure 5, it can be noted
that program output numbers of new line as 13, as expected.

6. Conclusions
The objective was to write a lex program to count the number of words,
characters, spaces and end of lines in an input file. Grammar was written for the
same. This grammar was implemented using Lex tool. The program has been
thoroughly tested. From the output obtained in Figures 1, 3, 5 and their analysis
in Table 1 the working of the program can be verified.
7. Comments
a. Limitations of Experiments

• If there is a word ‘co-responding’, the program will not count the ‘-’ as a

character. b. Limitations of Results

• The program does not consider numbers as characters. The program count
characters in the English alphabet only.

d. Learning happened

• Using ‘lex’ tool


• Designing grammar was mid-level programs

• Altering the algorithm to obtain required results

e. Recommendations

• The program can be improved by altering the grammar to count all the
characters including special characters and numbers.
Name: Kunal Goswami Roll Number: 17ETCS002086

Bibliography

1. Compilers: Principles, Techniques, and Tools


Aho, A. V., Lam, M. S., Sethi, R., and Ullman, J. D. (2007) [Accessed 13 Feb. 2017].

2. Why should text files end with a newline?. Konrad Rudolph, 2009
Available at: http://stackoverflow.com/questions/729692/why-should-text-files-end-
with-anewline [Accessed 7 Feb. 2017].

3. What's the point in adding a new line to the end of a file?. [online]
Unix.stackexchange.com. Available at:
http://unix.stackexchange.com/questions/18743/whats-the-point-in-adding-a-new-
line-tothe-end-of-a-file [Accessed 7 Feb. 2017].

Laboratory 7
Title of the Laboratory Exercise: Program to check simple expression involving +, -,* and /

1. Introduction and Purpose of Experiment


Students learn to use Lex program to find out vowels and consonants in a given string.

2. Aim and Objectives


Aim: To write a Program to check simple expression involving +, -,* and /

At the end of this lab, the student will be able to

• Define regular expression for vowels and consonants


• Count the number of vowels and consonants

3. Experimental Procedure
Students are required to carry out the following steps:

• Algorithm
• Write the Lex program
• Compile and execute the program (steps)
• Complete the documentation for the given problem.

4. Presentation of Results Algorithm:


The objective is to write a grammar for simple operations. Lex must be able to identify the
tokens are pass it. Yacc must be able to construct the parse tree and verify the syntax of the
expression.
Name: Kunal Goswami Roll Number: 17ETCS002086

Lex file steps

• Parse the input file:


• If a number is found: o Return the

token NUMBER

• If a variable is found o Return the

token ID

Lex code

%{

#include "7.tab.h"

%}

%%

[0-9]+ { return NUMBER; }

[a-zA-Z][a-zA-Z0-9_]* { return ID; }

\n { return NL ;}

. { return yytext[0]; }

%%

Yacc file steps

Define the precedence


Define the grammar for each simple expression involving operations
Define the terminals of the grammar using the tokens defined in the lex file
If a new line character if parsed after a valid expression print ‘valid’! Otherwise print ‘Invalid’
Yacc code

%{

#include<stdio.h>

%}

%token NUMBER ID NL

%left '+' '-'

%left '*' '/'


Name: Kunal Goswami Roll Number: 17ETCS002086

%%

stmt : exp NL { printf("Valid Expression"); exit(0);}


; exp : exp '+' exp | exp '-' exp
| exp '*' exp

| exp '/' exp

| '(' exp ')'

| ID

| NUMBER ;

%%

int yywrap(){ return


1;
}

int yyerror(char *msg)

printf("Invalid Expression\n"); exit(0);

main () {

printf("Enter the expression\n"); yyparse();


}

Output Screenshot:

Figure 1 The process of running a program which needs both lex and yacc
Name: Kunal Goswami Roll Number: 17ETCS002086

Figure 2 Test case 1

Figure 3 Test case 2

Figure 4 Test case 3

Figure 5 Test case 4

Figure 6 Test case 5


Name: Kunal Goswami Roll Number: 17ETCS002086

Figure 7 Test case 6

Figure 8 Test case 7

5. Analysis and Discussions

Table 1 Test results analysis


Test Input Expected output Program output Test result
case

1 5+1 Valid expression Valid expression Pass

2 9-2 Valid expression Valid expression Pass

3 a*b Valid expression Valid expression Pass

4 Sum/number Valid expression Valid expression Pass

5 589 Invalid expression Invalid expression Pass

6 8++7 Invalid expression Invalid expression Pass

7 5+4/a-c Valid expression Valid expression Pass

The input and output presented in Figures 2-7 are presented in Table 1.

6. Conclusions
The objective was to write a lex and yacc program to identify valid simple expression..
Grammar and tokens were written for the same. This grammar was implemented using
Lex and yacc tool. The program has been thoroughly tested. From the output obtained
in Figures 2-7 and their analysis in Table 1 the working of the program can be verified.

7. Comments
a. Limitations of Experiments

• Development of two different grammar, one to identify tokens and another


to build the parse tree (check the syntax) is tedious.

b. Limitations of Results
Name: Kunal Goswami Roll Number: 17ETCS002086

• The program is unable to detect decimal numbers as valid expressions

c. Learning happened

• Using ‘’yac’ tool


• Designing grammar in yacc
• Using tokens as terminals
• How to run lex and yacc file together

d. Recommendations

• The program can be improved by writing grammar which recognized


decimal numbers also.

Bibliography

Compilers: Principles, Techniques, and Tools Aho, A. V., Lam, M. S., Sethi, R., and Ullman, J. D. (2007)
[Accessed 3 March. 2017].

You might also like