Tuesday, March 1, 2016

LEX program that replicates the input on screen but filters out everything that is neither letter nor digit.




%{
/* a simple LEX program that replicates the input
   on screen but filters out everything that is
   neither letter nor digit.
*/
#include <stdio.h>
/* letter and digit counters */
int letters = 0, digits = 0;
%}
letter [a-zA-Z]
digit [0-9]
%%
{letter} { ECHO; letters++; }
{digit} { ECHO; digits++; }
[^{letter}{digit}] ;
%%
int main(int argc, char *argv[]) {
FILE *inFile;
if (argc == 2 && (inFile = fopen(argv[1], "r")))
yyin = inFile;
yylex();
if (inFile) fclose(inFile);
printf("\n\n        Count\nLetters %d\n Digits %d\n", letters, digits);
return 0;
}

No comments:

Post a Comment