Hi, I am struggling with this program in my Programming Languages class. I also
ID: 3695061 • Letter: H
Question
Hi, I am struggling with this program in my Programming Languages class.
I also can't figure out how to read my test file into the program. The test file is just called "Input.txt"
We are supposed to write a Lex input file that will produce a program that counts characters, words, and lines in a text file and reports the counts. Define a word to be any sequence of letters and/or digits, without punctuation or spaces. Punctuation and white space do not count as words. A sample output of the program is given :
Number of characters: 35747
Number of words: 5978
Number of lines: 150
Explanation / Answer
%{
#include<stdio.h>
int lines=0, words=0,s_letters=0,c_letters=0, num=0, spl_char=0,total=0;
%}
%%
{ lines++; words++;}
[ ' '] words++;
[A-Z] c_letters++;
[a-z] s_letters++;
[0-9] num++;
%%
main(void)
{
yyin= fopen("myfile.txt","r");
yylex();
total=s_letters+c_letters+num+spl_char;
printf(" This File contains ...");
printf(" %d lines", lines);
printf(" %d words",words);
printf(" %d small letters", s_letters);
printf(" %d capital letters",c_letters);
printf(" %d digits", num);
printf(" In total %d characters. ",total);
}
int yywrap()
{
return(1);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.