Lab Assignment-I
Programming in LeX
Compiler Construction
UCS802
Submitted by:
Anurag Sharma
101503043
COMPUTER SCIENCE AND ENGINEERING DEPARTMENT
Thapar Institute of Engineering and Technology, Patiala
November, 2018
1
Table of Contents
1. Lex program to display the same line as read from keyboard. 1
2. Write a program to display only digits. 2
3. WAP to print line number of each line in a file. 3
4. WAP to remove comments from a file i.e to print all data except comments. 4
5. WAP to check entered number is Integer or Float. 5
6. WAP to check a given substring in text. 7
7. WAP to convert text into uppercase. 8
8. WAP to check for valid identifier. 9
9. WAP to check given string is verb or noun. 11
10. WAP to identify signed or unsigned integer. 13
11. WAP to print a string that starts and ends with “a”. 15
12. WAP to count number of lines, words and characters in text file. 16
13. WAP to convert decimal to hexadecimal. 18
2
1. Lex program to display the same line as read from keyboard.
%{
#include <stdio.h>
%}
%%
. | \n {ECHO;}
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("abc.txt", "r");
yylex();
return 0;
}
Input : This is a sample text.
Output :
3
2. Write a program to display only digits.
%{
#include <stdio.h>
%}
%%
[a-zA-Z]*
[\t |\n| |\r|\v]*
[0-9] {ECHO;}
.
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("abc.txt", "r");
yylex();
return 0;
}
Input : 12345--abc?67890
Output :
4
3. WAP to print line number of each line in a file.
%{
#include <stdio.h>
int i=2;
%}
%%
\n {printf("\nLine %d. ", i++);}
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("abc.txt", "r");
printf("Line 1. ");
yylex();
return 0;
}
Input :
This is line 1
this is line 2
Output :
5
4. WAP to remove comments from a file i.e to print all data except
comments.
%{
#include <stdio.h>
%}
%%
\/\/(.*)
\/\*(.*)(\n)*(.*)(\n)*\*\/
. | \n {ECHO;}
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("abc.txt", "r");
yylex();
return 0;
}
Input :
My name is
Output :
6
5. WAP to check entered number is Integer or Float.
%{
#include <stdio.h>
%}
%%
[-][0-9]*\.[0-9]+ |
[+]?[0-9]*\.[0-9]+ {printf("%s : float", yytext);}
[-]0*[1-9]+ |
[+]?0*[0-9]+ {printf("%s : integer", yytext);} front
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("number.txt", "r");
yylex();
return 0;
}
Input :
0.2
34
0000134
-89
-9.6
7
Output :
6. WAP to check a given substring in text.
%{
#include <stdio.h>
int a=0;
%}
%%
[.\n]*(anurag)[.\n]* {printf("Substring found"), a=1;}
[ \t\n]+ |
.+
%%
int yywrap()
{
if(!a)
printf("Substring not found");
return 1;
}
int main()
{
yyin=fopen("test.txt", "r");
yylex();
return 0;
8
}
Input : my name is Anurag
Output :
7. WAP to convert text into uppercase.
%{
#include <stdio.h>
#include <string.h>
%}
lower [a-z]
%%
{lower} {printf("%c",yytext[0]-32);}
. | \n ECHO;
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("abc.txt", "r");
yylex();
return 0;
}
9
Input : this text is in lower case
Output :
8. WAP to check for valid identifier.
%{
#include <stdio.h>
int a=0;
%}
%%
if|else|while|int|switch|for|char {printf("%s : Keyword",
yytext);}
[a-zA-z_]+[a-zA-z0-9_]* {printf("%s : Valid identifier",
yytext), a=1;}
[0-9]* {printf("%s : Number", yytext);}
[ \t\n]+ |
.+
%%
int yywrap()
{
if(!a)
printf("No valid identifier found");
return 1;
}
int main()
{
yyin=fopen("identifier.txt", "r");
10
yylex();
return 0;
}
Input :
if
_123as
123
viruj
Output :
9. WAP to check given string is verb or noun.
%{
#include <stdio.h>
%}
%%
[\t\n ]+
is|am|are|were|was|be|being|been|do|does|did|will|would|should|c
an|could|has|have|had|catch|go {printf("%s : is a
verb\n",yytext);}
bed|cat|movie|train|country|book|phone|match|speaker|man|mountai
n|state|ocean|cat|milk|rice {printf("%s : is a
noun\n",yytext);}
[a-zA-z]+ {printf("Might be noun or verb");}
11
.
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("q9.txt", "r");
yylex();
return 0;
}
Input : cat
Output :
10. WAP to identify signed or unsigned integer.
%{
#include <stdio.h>
#include <string.h>
#include <math.h>
void check(char *);
%}
%%
[-][0-9]+ {printf("%s : Signed", yytext);}
[0-9]+ check(yytext);
%%
int yywrap()
12
{
return 1;
}
int main()
{
yyin=fopen("q10.txt", "r");
yylex();
return 0;
}
void check(char *a)
{
unsigned int len=strlen(a), num=0;
for(int i=0;i<len;i++)
num=num*10+(a[i]-'0');
if(num<=(pow(2,31)-1))
printf("%s : Signed", a);
else
printf("%s : Unsigned", a);
}
Input :
2147483647
21474836479
-123
Output :
13
11 . WAP to print a string that starts and ends with “a”.
%{
#include <stdio.h>
#include <string.h>
%}
%%
a[a-z]*a[ ] {printf("%s", yytext);} //checking each string with the given
contraints
[ \t\n]+
.
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("q11.txt", "r");
yylex();
return 0;
}
Input :
adjglsfdga asgfgc adfgdfaa
Output :
14
12 . WAP to count number of lines, words and characters in text file.
%{
#include <stdio.h>
int nline=0, nchar=0, nwords=0; //initializing the variables
%}
%%
[a-zA-z]+ {
nwords++;
nchar=nchar+yyleng; //yyleng is the length of the character array
}
\n {nline++, nchar++;}
. {nchar++;} //incrementing when a single line character is matched
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("test.txt", "r");
yylex();
printf("Number of words : %d\n", nwords);
printf("Number of lines : %d\n", nline);
printf("Number of characters : %d\n", nchar);
return 0;
}
Input :
Hello my name is Anurag.
?>: loll
15
Output :
13 . WAP to convert decimal to hexadecimal.
%{
#include <stdio.h>
#include <string.h>
void convert(char *);
%}
%%
[0-9]+ convert(yytext);
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("q13.txt", "r");
yylex();
return 0;
}
void convert(char *a)
{
int num=0, len=strlen(a),arr[100]={},i=0,tmp;
for(int j=0;j<len;j++)
16
num=num*10+(a[j]-'0');
while(num>16) //forming the hexadecimal number and storing in an array
{
tmp=num/16;
arr[i++]=num%16;
num=tmp;
}
num>=10 ? printf("%c",(55+num)) : printf("%d",num);
i--;
while(i>=0)
{
if(arr[i]>=10)
{
char y=55+arr[i];
printf("%c",y);
}
else
printf("%d",arr[i]);
i--;
}
}
Input :
2482
123
14
Output :
17