i am trying to work with a flex generator and how it works,the code below replaces the the sequence of symbols in a text file {number number} to {“d” ws “d”}, and it calculates the number of replacements
%{
int count = 0;
%}
DIGIT [0-9]
%%
{DIGIT}{DIGIT} {count++; printf("d d");}
%%
int main()
{
yylex();
printf( "\n#Report: %d changes made!", count);
return 0;
}
int yywrap()
{
return 1;
}
After submitting a file with the rules of the input file and compile flex lex.yy.c, received an executable file. The input to the executable file we submit the following data stream
====================test.in============================
wefwe
f
weferg54gfwsfwe
fwef
wefwefwf
wefewf21321dsfredf
sdf
===========================================================
the output obtained
============================test.out==================
wefwe
f
wefergd dgfwsfwe
fwef
wefwefwf
wefewfd dd d1dsfredf
sdf
#Report: 3 changes made!
========================================================
my question is that what if i want to
In the given text to replace the sequence of symbols {consonant consonant} to {consonant «a» consonant} and calculate the number of replacements
I’m not sure what concept you’re stuck on. Perhaps it’s the idea of writing a character class to match consonants: let’s consider “Y” to be a consonant for this question, and only match lower case letters. Perhaps it’s the question of how to access individual characters of the matched input: one way to do this is to declare
yytextto be an%array. At any rate, the code you want is something like this: