-
-
Save wsgac/8dcbc0e55f20479bd4679e5bddfd4091 to your computer and use it in GitHub Desktop.
Implementation of wc in Flex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* -*-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; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Previous revision attempted to emulate the workings of the Unix
wcprogram by (incorrectly) assuming that rules in Flex are pass-through rules. This revision works better, but it does not use pass-through. Instead, it incrementscharsby the calculated length of the matched token.