LEX for terminal
2)Write a lex program to check whether a given email id is valid or not.
3)Write a lex program to check whether a given mobile number is valid or not.
4)Write a lex program to check whether a given input is digit or not.
5)Write a lex program to accept a string starting with vowel.
6)Write a lex program to count the number of letters and numbers in a given string.
7)write a lex program to count the number of vowels and consonants in a given
string.
8)Write a lex program to count the number of lines,spaces,tabs and characters.
9)Write a lex program to count number of words.
10)Write a lex program to find the length of a string.
11)Write a lex program to find the size of a word.
12)Write a lex program to recognize keyword,identifier,number,operator,separator.
13)Write a lex program to count the number of positive and negative numbers
14)Write a lex program to count the total number of tokens
...................................................................................
1./*Lex program to take check whether the given number is even or odd */
%{
#include<stdio.h>
int i;
%}
%%
[0-9]+ {i=atoi(yytext);
if(i%2==0)
printf("Even");
else
printf("Odd");}
%%
int yywrap(){}
/* Driver code */
int main()
{
yylex();
return 0;
}
...................................................................................
2./*lex code to accept a valid email */
%
{
int flag = 0; %
} %
% [a - z.0 - 9 _] + @[a - z] + ".com" | ".in"
flag = 1; %
%
main() {
yylex();
if (flag == 1)
printf("Accepted");
else
printf("Not Accepted");
}
...................................................................................
3./* Lex Program to check valid Mobile Number */
%{
/* Definition section */
%}
/* Rule Section */
%%
[1-9][0-9]{9} {printf("\nMobile Number Valid\n");}
.+ {printf("\nMobile Number Invalid\n");}
%%
// driver code
int main()
{
printf("\nEnter Mobile Number : ");
yylex();
printf("\n");
return 0;
}
...................................................................................
4./* Lex program to check whether input is digit or not. */
%{
#include<stdio.h>
#include<stdlib.h>
%}
/* Rule Section */
%%
^[0-9]* printf("digit");
^[^0-9]|[0-9]*[a-zA-Z] printf("not a digit");
. ;
%%
int main()
{
// The function that starts the analysis
yylex();
return 0;
}
...................................................................................
5./* Lex Program to accept string starting with vowel */
% {
int flag = 0;
% }
%%
[aeiouAEIOU].[a-zA-Z0-9.]+ flag=1;
[a-zA-Z0-9]+
%%
main()
{
yylex();
if (flag == 1)
printf("Accepted");
else
printf("Not Accepted");
}
...................................................................................
6)Write a lex program to count the number of letters and numbers in a given string.
...................................................................................
7)write a lex program to count the number of vowels and consonants in a given
string.
%{
int vow_count=0;
int const_count =0;
%}
%%
[aeiouAEIOU] {vow_count++;}
[a-zA-Z] {const_count++;}
%%
int yywrap(){}
int main()
{
printf("Enter the string of vowels and consonants:");
yylex();
printf("Number of vowels are: %d\n", vow_count);
printf("Number of consonants are: %d\n", const_count);
return 0;
}
...................................................................................
8)Write a lex program to count the number of lines,spaces,tabs and characters.
%{
#include<stdio.h>
int lc=0,sc=0,tc=0,ch=0,wc=0; // GLOBAL VARIABLES
%}
// RULE SECTION
%%
[\n] { lc++; ch+=yyleng;}
[ \t] { sc++; ch+=yyleng;}
[^\t] { tc++; ch+=yyleng;}
[^\t\n ]+ { wc++; ch+=yyleng;}
%%
int yywrap(){ return 1; }
/* After inputting press ctrl+d */
// MAIN FUNCTION
int main(){
printf("Enter the Sentence : ");
yylex();
printf("Number of lines : %d\n",lc);
printf("Number of spaces : %d\n",sc);
printf("Number of tabs, words, charc : %d , %d , %d\n",tc,wc,ch);
return 0;
}
...................................................................................
9./lex program to count number of words
%{
#include<stdio.h>
#include<string.h>
int i = 0;
%}
/* Rules Section*/
%%
([a-zA-Z0-9])* {i++;} /* Rule for counting
number of words*/
"\n" {printf("%d\n", i); i = 0;}
%%
int yywrap(void){}
int main()
{
// The function that starts the analysis
yylex();
return 0;
}
...................................................................................
10./lex program to find the length of a string/
%{
#include<stdio.h>
int length;
%}
/* Rules Section*/
%%
[a-z A-Z 0-9]+ {length=yyleng; }
%%
int main()
{
yylex();
printf("length of given string is : %d", length);
return 0;
}
...................................................................................
11./lex code to find the length of the longest word/
% {
int counter = 0; %
}
%
% [a - zA - Z] + {
if (yyleng > counter) {
counter = yyleng;
}
} %
%
main() {
yylex();
printf("largest: %d", counter);
printf("\n");
}
...................................................................................
12.)Write a lex program to recognize keyword,identifier,number,operator,separator.
/*Definition Section*/
%{
#include<stdio.h>
#define NUMBER 400
#define COMMENT 401
#define STRINGS 402
#define IDENTIFIER 403
%}
/* Rules Section */
%%
[\t ]+ ;
[0-9]+ | [0-9]*\.[0-9]+ { return NUMBER;}
#.* { return COMMENT;}
\"[^ \"\n]*\" { return STRINGS;}
[a-zA-Z][a-zA-Z0-9]+ { return IDENTIFIER;}
\n { return '\n';}
%%
/* User Subroutine section */
main( )
{
int val;
while( val = yylex( ))
printf("value is %d\n",val);
}
int yywrap( )
{
return 1;
}...................................................................................
13./* Lex program to Identify and Count Positive and Negative Numbers */
%{
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;
}
...................................................................................
14./*Lex code to count total number of tokens */
%{
int n = 0 ;
%}
// rule section
%%
//count number of keywords
"while"|"if"|"else" {n++;printf("\t keywords : %s", yytext);}
// count number of keywords
"int"|"float" {n++;printf("\t keywords : %s", yytext);}
// count number of identifiers
[a-zA-Z_][a-zA-Z0-9_]* {n++;printf("\t identifier : %s", yytext);}
// count number of operators
"<="|"=="|"="|"++"|"-"|"*"|"+" {n++;printf("\t operator : %s", yytext);}
// count number of separators
[(){}|, ;] {n++;printf("\t separator : %s", yytext);}
// count number of floats
[0-9]*"."[0-9]+ {n++;printf("\t float : %s", yytext);}
// count number of integers
[0-9]+ {n++;printf("\t integer : %s", yytext);}
. ;
%%
int main()
{
yylex();
printf("\n total no. of token = %d\n", n);
}
...................................................................................
15.//Lex program to count the number of identifiers
%{#include<iostream.h>
int count=0;
char ch=0;
%}
digit[0-9]
letter[a-zA-Z_]
%%
{letter}({letter}|{digit})* {
count++;
}
%%
int main()
{
yylex();
printf("count: %d",count);
return 0;
}
Comments
Post a Comment