Upload
yash-yadav
View
192
Download
2
Embed Size (px)
DESCRIPTION
Lex programs solution in linux terminal for B.Sc.(H) Computer Science
Citation preview
1. Write a Lex program to count the number of lines and
characters in the input file.
%{
int lines=0, words=0, ch=0, bl=0;
%}
word [^ \n\t]+
eol \n
%%
{word} {words++; ch+=yyleng;}
" " bl++;
{eol} lines++;
. {ch++;} /* any char except new line */
%%
int main(void)
{
yylex();
printf( " lines = %d words = %d characters = %d blanks = %d \n " ,
lines , words , ch , bl );
}
Output:
$ lex lex1.l
$ cc lex.yy.c -ll
$ ./a.out < names
lines = 3 words = 3 characters = 22 blanks = 0
$ cat names
Lakshya
Sachin
Udheeshen
2. Write a Lex program that implements the Caesar cipher:
it replaces every letter with the one three letters after
in in alphabetical order, wrapping around at Z.
e.g. a is replaced by d, b by e, and so on z by c
%%
[a-wA-W] printf ("%c", yytext[0] + 3);
[x-zX-Z] printf("%c", yytext[0] - 23);
%%
int main(void)
{
yylex();
}
Output:
$ lex lex2.l
$ cc lex.yy.c -ll
$ ./a.out
This is a Lex program
Wklv lv d Oha surjudp
XYZ
ABC
3. Write a Lex program that finds the longest word (defined
as a contiguous string of upper and lower case letters) in
the input.
%{ #include<string.h>
int longest=0; char longword[60];
%}
%%
[a-zA-Z]+ { if( yyleng > longest )
{ longest=yyleng;
strcpy(longword, yytext);
}
}
.
\n
%%
int main( void )
{
yylex();
printf( "Longest word is \" %s \" , having %d characters\n" ,
longword, longest);
}
Output:
$ lex lex3.l
$ cc lex.yy.c -ll
$ ./a.out
This is Lex programming.
Longest word is " programming " , having 11 characters
4. Write a Lex program that distinguishes keywords,
integers, floats, identifiers, operators, and comments in
any simple programming language.
%{#include<math.h>
%}
DIGIT [0-9]
ID [a-z][a-z0-9]*
COMMENT \/\*.*\*\/
%%
{DIGIT}+ { printf("\nAn Integer: %s ", yytext, atoi(yytext));
}
{DIGIT}+"."{DIGIT}* {printf("\nA Float: %s ", yytext, atof(yytext));}
("void")|("int")|("char")|("if")|("else")|("for")|("while")|("do")|("
exit")|("cout")|("cin")|("main")|("break")|("continue") {printf("\nA
Keyword: %s",yytext); }
{ID} {printf("\nAn Identifier: %s",yytext);}
{COMMENT} printf("\nComments are: %s",yytext);
[+|-|*|/] printf("\nAn Operator: %s", yytext);
[\t\n]+
%%
int main()
{ yylex();
return 0;
}
$ lex abc.l
$ cc lex.yy.c -ll
$ ./a.out
/*this is a program to swap two num*/ void swap() { int a=3; b=6;
temp=a; a=b; b=temp; cout<<a<<b;}
Comments are: /*this is a program to swap two num*/
A Keyword: void
An Identifier: swap() {
A Keyword: int
An Identifier: a=
An Integer: 3 ;
An Identifier: b=
An Integer: 6 ;
An Identifier: temp=
An Identifier: a;
An Identifier: a=
An Identifier: b;
An Identifier: b=
An Identifier: temp;
A Keyword: cout<<
An Identifier: a<<
An Identifier: b;}
5. Write a LEX program to count the number of identifiers
in a C file.
%{ #include<math.h>
int count=0;
%}
Digit [0-9]*
Id [a-z][a-z0-9]*
Datatype [ "int" | "char" | "float" | "long" | "double" | "bool" ]
%%
{Datatype}" "{Id}"="{Digit} | {Id}";" count++;
{Datatype}" "{Id}";" count++;
.
\n
%%
int main(void)
{
yylex();
printf("Number of Identifiers : %d\n", count);
return 0;
}
Output:
$ lex lex5.l
$ cc lex.yy.c -ll
$ ./a.out < t1.C
$ ./a.out < t1.C
Number of Identifiers : 5
$ cat t1.C
/*this is a program to swap two num*/
void swap()
{
int a=3;
b=6;
temp=a;
a=b;
b=temp;
cout<<a<<b;
}
6. Write a LEX program to count the number of words,
characters, blank spaces and lines in a C file.
%{
int ch=0, bl=0, line=0, wr=0;
%}
%%
[\n] {line++; wr++;}
[\t] {bl++; wr++;}
" " {bl++; wr++;}
[^ \n\t] {ch++;}
%%
int main(void)
{
FILE *fp;
char file[55];
printf(" \n Enter a file name: ");
scanf("%s", file);
fp= fopen(file, "r");
yyin=fp;
yylex();
printf(" Characters= %d \n blanks= %d \n lines=%d \n words=%d", ch,
bl, line, wr);
return 0;
}
Output:
$ lex lex6.l
$ cc lex.yy.c -ll
$ ./a.out < t1.c
Enter a file name: Characters= 76
blanks= 29
lines=10
words=39lakshya@lakshya-Aspire-4736Z:~/practicals$ cat t1.c
/*this is a program to swap two num*/
void swap()
{
int a=3;
b=6;
temp=a;
a=b;
b=temp;
cout<<a<<b;
}
7. Write a LEX specification program that generates a C
program which takes a string “abcd” and prints the
following output.
abcd
abc
ab
a
%%
a|ab|abc|abcd printf("%s\n",yytext);REJECT;
.|\n
%%
int main(void)
{
yylex();
return 0;
}
Output:
$ lex lex7.l
$ cc lex.yy.c -ll
$ ./a.out
abcd
abcd
abc
ab
a
8. A program in LEX to recognize a valid arithmetic
expression.