0% found this document useful (0 votes)
14 views3 pages

Compilerlexyaac

The document contains multiple examples of Lex programs that perform various text processing tasks. These include checking for matching parentheses, counting lines, words, and characters, converting specific substrings, and counting vowels and consonants in an input text. Each example includes the necessary code and structure for implementing the respective functionality.

Uploaded by

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

Compilerlexyaac

The document contains multiple examples of Lex programs that perform various text processing tasks. These include checking for matching parentheses, counting lines, words, and characters, converting specific substrings, and counting vowels and consonants in an input text. Each example includes the necessary code and structure for implementing the respective functionality.

Uploaded by

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

Exp 3: Write a Lex program to check for parenthesis

matching
%{
#include<stdio.h>
int cnt1=0,cnt2=0,cnt3=0;
%}
%%
[(] {cnt1++;}
[)] {cnt1--;}
[[] {cnt2++;}
[]] {cnt2--;}
[{] {cnt3++;}
[}] {cnt3--;}
[a-z|A-Z] {}
[\n] {if((cnt1==0)&&(cnt2==0)&&(cnt3==0))printf("matching\n");
else printf("no matching\n");
cnt1=0;cnt2=0;cnt3=0;
}
. {}
%%
int main(int argc,char argv[])
{
yyin=fopen(argv[1],"r");
yylex();
}

Exp 4:Write a lex program to display the number of


lines, words and characters in an input text.
%{
#include<stdio.h>
int line=0,word=0,ch=0;
%}
%%
[a-z|A-Z|0-9] {ch++;}
" " {word++;}
"\n" {line++;word++;}
. {}
%%
main(int argc,char*argv[])
{
yyin=fopen(argv[1],"r");
yylex();
printf("line=%d\n",line);
printf("word=%d\n",word);
printf("character=%d\n",ch);
}
Exp 5:Write a LEX Program to convert the substring
abc to ABC from the given input string.
%{
#include <stdio.h>
%}

%%
abc { printf("ABC"); }
.|\n { printf("%c", yytext[0]); }
%%

int main(int argc, char **argv)


{
yylex();
return 0;
}

int yywrap()
{
return 1;
}

Exp 6:Write a lex program to display the number of


vowels and consonants in an input text.
%{
#include <stdio.h>

int vowel_count = 0;
int consonant_count = 0;
%}

%%
[aeiouAEIOU] { vowel_count++; }
[a-zA-Z] { consonant_count++; }
.|\n ; /* Ignore other characters */
%%

int main(int argc, char **argv)


{
yylex();
printf("Vowels: %d\n", vowel_count);
printf("Consonants: %d\n", consonant_count);
return 0;
}
int yywrap()
{
return 1;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy