Skip to content

Instantly share code, notes, and snippets.

@wsgac
Last active April 25, 2019 15:13
Show Gist options
  • Select an option

  • Save wsgac/8dcbc0e55f20479bd4679e5bddfd4091 to your computer and use it in GitHub Desktop.

Select an option

Save wsgac/8dcbc0e55f20479bd4679e5bddfd4091 to your computer and use it in GitHub Desktop.
Implementation of wc in Flex
/* -*-bison-*- */
/* Simple wc implementation in Flex */
%option noyywrap
int rows = 0, words = 0, chars = 0;
%%
\n rows++;
[A-Za-z0-9]+ words++; chars += strlen(yytext);
. chars++;
%%
int main() {
yylex();
printf("\t%d\t%d\t%d\n", rows, words, chars);
return 0;
}
@wsgac
Copy link
Copy Markdown
Author

wsgac commented Apr 25, 2019

Previous revision attempted to emulate the workings of the Unix wc program by (incorrectly) assuming that rules in Flex are pass-through rules. This revision works better, but it does not use pass-through. Instead, it increments chars by the calculated length of the matched token.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment