Click here to Skip to main content
15,921,606 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Write a LEX specification files to:
i). Count the number of words in a file and their total size
ii). Accepts the English language words (without bothering for the meaning) and
replaces each occurrence of the string “abc” in it to “ABC”.

What I have tried:

I am having trouble with compiling my code. A few errors which I am unable to figure out how to solve them

My code was as follows:
C
/*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; 
} 

%{
#include<stdio.h>
int lines=0, words=0,s_letters=0,c_letters=0, num=0, spl_char=0,total=0;
%}
%%

\n { lines++; words++;}
[\t ' '] words++;
[A-Z] c_letters++;
[a-z] s_letters++;
[0-9] num++;
. spl_char++;
%%

main(void)
{
yyin= fopen("myfile.txt","r");
yylex();
total=s_letters+c_letters+num+spl_char;
printf(" This File contains ...");
printf("\n\t%d lines", lines);
printf("\n\t%d words",words);
printf("\n\t%d small letters", s_letters);
printf("\n\t%d capital letters",c_letters);
printf("\n\t%d digits", num);
printf("\n\t%d special characters",spl_char);
printf("\n\tIn total %d characters.\n",total);
}
 
int yywrap()
{
return(1);
}
Posted
Updated 11-Jul-21 3:17am
v3
Comments
Greg Utas 11-Jul-21 8:08am    
If you want help, post your code and compile errors. You'll probably get help with the compile errors and obvious logic errors, but it's unlikely that anyone here will debug your code for you once it compiles.
Patrice T 11-Jul-21 9:24am    
Try to show us the error messages.
Greg Utas 11-Jul-21 9:25am    
Thanks for posting your code. I have added a couple of lines of HTML so it displays better, but you may want to improve its formatting some more.

I don't know LEX, so I can't help. It looks like it's some kind of extension to C that gets preprocessed (these % characters, for example). Maybe someone on the site is familiar with LEX and can help.

But I see nothing that looks for "abc" and changes it to "ABC". I would assume that you have to read each word, change it, and then write it to an output file to satisfy that part of the specification.

It also looks like the function yywrap is defined twice. The first definition has empty code (the {}), so unless it is preprocessed in some way, it will cause a compile error because it doesn't return an int like it's supposed to. The second definition will cause a compile error because yywrap was previously defined.

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

Just dumping your homework question on us and saying "Kindly write the code of the progam in the answer" isn't going to get you anywhere at all.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900