Professional Documents
Culture Documents
CSC312A Laboratory Manual Kunal Goswami
CSC312A Laboratory Manual Kunal Goswami
Compilers Laboratory
B.Tech. 6th Semester
Batch: 2017
List of Experiments
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
● To write a program to count the number of vowels and consonants in a given string
Objectives
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
The program presented in Figure 1 returns the string “Welcome to Konoha” when the user
enters “gm”
Output screenshot:
%{
%}
%%
%%
int yywrap()
return 1;
main(){
Name: Kunal Goswami Roll Number: 17ETCS002086
yylex();
}
Output Screenshot:
In this experiment our objective is to write a Lex program which counts the number of
vowels and consonants in the entered string.
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
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].
Laboratory 2
Title of the Laboratory Exercise: Program to find the longest word in a given string.
3. Experimental Procedure
Students are required to carry out the following steps:
• Algorithm
If yes, store the current word in ‘Lword’ and update the length’ Print
%{
#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();
}
Output Screenshot:
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];
%}
%%
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);
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
In this experiment our objective is to write a Lex program which finds the longest word in
the sentence entered.
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
c. Learning happened
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
3. Experimental Procedure
Students are required to carry out the following steps:
• Algorithm
• Write the Lex program
4. Presentation of Results
%{
#include <stdio.h>
%%
[-][0-9]+ { negative++;}
.;
%%
int main(){
Output Screenshot:
%{
%%
[+]?[0-9]+[\.][0-9]+ { decimal_positive++;}
[-][0-9]+[\.][0-9]+ { decimal_negative++;}
[+]?[0-9]+ { positive++;}
[-][0-9]+ { negative++;}
.;
%%
int yywrap(){
return 1;
}
}
Name: Kunal Goswami Roll Number: 17ETCS002086
• 0-9 = digit
• Digit can repeat any number of times.
• ‘.’ is denoted using escape sequence
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.
c. Learning happened
d. Recommendations
• The program can be improved by displayed the longest AND the shortest words in
the sentence entered.
Bibliography
Laboratory 4
Title of the Laboratory Exercise: Program to count the number of characters, words, spaces,
end of lines in a given input file.
3. Experimental Procedure
Students are required to carry out the following steps:
• Algorithm
• Write the Lex program
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.
• If a word is found:
o Increment word-count by 1.
space-count by 1.
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;
%}
%%
[ ] { spaces++; }
\n { newLine++;}
Name: Kunal Goswami Roll Number: 17ETCS002086
. {charac++;}
%%
int yywrap(){
return 1;
}
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 3
Figure 4
Figure 5
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).
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
• The program does not consider numbers as characters. The program count
characters in the English alphabet only.
d. Learning happened
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
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 /
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.
token NUMBER
token ID
Lex code
%{
#include "7.tab.h"
%}
%%
\n { return NL ;}
. { return yytext[0]; }
%%
%{
#include<stdio.h>
%}
%token NUMBER ID NL
%%
| ID
| NUMBER ;
%%
main () {
Output Screenshot:
Figure 1 The process of running a program which needs both lex and yacc
Name: Kunal Goswami Roll Number: 17ETCS002086
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
b. Limitations of Results
Name: Kunal Goswami Roll Number: 17ETCS002086
c. Learning happened
d. Recommendations
Bibliography
Compilers: Principles, Techniques, and Tools Aho, A. V., Lam, M. S., Sethi, R., and Ullman, J. D. (2007)
[Accessed 3 March. 2017].