0% found this document useful (0 votes)
60 views5 pages

Ui22 Lab1

The document provides examples of LEX programs to perform various tasks: 1) Identify keywords, constants, and identifiers in a file 2) Count vowels and consonants in a string 3) Count lines, spaces, and characters in a file 4) Convert uppercase letters to lowercase and vice versa in a string 5) Identify octal and hexadecimal numbers 6) Count positive, negative, and zero numbers from user input It concludes with a brief explanation of what LEX is and the syntax of LEX code.

Uploaded by

dattpatel2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views5 pages

Ui22 Lab1

The document provides examples of LEX programs to perform various tasks: 1) Identify keywords, constants, and identifiers in a file 2) Count vowels and consonants in a string 3) Count lines, spaces, and characters in a file 4) Convert uppercase letters to lowercase and vice versa in a string 5) Identify octal and hexadecimal numbers 6) Count positive, negative, and zero numbers from user input It concludes with a brief explanation of what LEX is and the syntax of LEX code.

Uploaded by

dattpatel2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab: 1

AIM: Write a some LEX program

1. Write a LEX program to identify identifiers, constants and keywords


(int, float) used in given input file.

%{
#include <stdio.h>
#include <stdlib.h>
%}

%%
int|float { printf("Keyword: %s\n", yytext); }
[0-9]+ { printf("Constant: %s\n", yytext); }
[a-zA-Z_][a-zA-Z0-9_]* { printf("Identifier: %s\n", yytext); }
\n { printf("Exiting...\n"); exit(0); }
. { /* Ignore other characters */ }

%%

int yywrap(){}

int main() {
yylex();
return 0;
}

2. Write a LEX program to


count the number of vowels and consonants in a given string.
%{
#include <stdio.h>
int vowels = 0, consonants = 0;
%}

%%
[aeiouAEIOU] { vowels++; }
[a-zA-Z] { consonants++; }
\n { printf("Vowels: %d\nConsonants: %d\n", vowels, consonants); exit(0); } . ;

%%

int yywrap() {
return 1;
}

int main() {
yylex();
return 0;
}

3. Write a LEX program to


count the number of lines, spaces and characters from a given file.

%{
int charCount = 0;
int spaceCount = 0;
int lineCount = 0;
%}

%%
\n { lineCount++; }
[ \t] { spaceCount++; }
. { charCount++; }

"exit" { printf("Lines: %d\n", lineCount);


printf("Spaces: %d\n", spaceCount);
printf("Characters: %d\n", charCount);
exit(0);
}

%%
int yywrap(){}

int main() {
yylex();
return 0;
}
4. Write a LEX program to
convert upper case to lower and lower case to upper case from a given string.

%{
#include<stdio.h>
%}

%%
[a-z] printf("%c",yytext[0] - ('a' - 'A'));
0 { return 0;}
%%

int yywrap(){}
int main()
{
yylex();
return 0;
}

5. Write a LEX program to


identify octal or hexadecimal numbers.

%{
/* Definition section */
#include<stdio.h>
int num, r, digit=0, count, pcount=0, i;
char a[20];
%}

DIGIT [0-9]
/* Rule Section */
%%

{DIGIT}+ { num=atoi(yytext);

while(num!=0)
{
r=num%16;
digit='0'+r;
if(digit>'9')
digit+=7;
a[count++]=digit;
num=num/16;

for(i=count-1;i>=pcount;--i)
printf("%c", a[i]);
pcount=count;
}

.|\n ECHO;

%%
int yywrap(){}
int main()
{
yylex();
return 0;
}

6. Write a LEX program to


count no of negative, positive and zero numbers from user input.

%{
int positive_no = 0, negative_no = 0;
%}

/* Rules for identifying and counting


positive and negative numbers*/
%%
^[-][0-9]+ {negative_no++;
printf("negative number = %s\n",
yytext);} // negative number

[0-9]+ {positive_no++;
printf("positive number = %s\n",
yytext);} // positive number
%%
/*** use code section ***/

int yywrap(){}
int main()
{

yylex();
printf ("number of positive numbers = %d,"
"number of negative numbers = %d\n",
positive_no, negative_no);

return 0;
}

Conclusion: Here we no what is lex and what are the syntax of the lex code, and also know
that how compiler will work

You might also like