Created
October 15, 2017 06:27
-
-
Save sh-cho/4bd00f487eedd680fd9b41cb6cf9a6f4 to your computer and use it in GitHub Desktop.
flex wordcount example
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
/* declaration and option settings */ | |
%{ | |
void addWord(char *text); | |
void addNewLine(void); | |
void addChar(void); | |
int chars = 0; | |
int words = 0; | |
int lines = 0; | |
%} | |
%% | |
/* a list of patterns and actions */ | |
[a-zA-Z]+ addWord(yytext); | |
\n addNewLine(); | |
. addChar(); | |
%% | |
/* C code */ | |
void addWord(char *text) { | |
++words; | |
chars += strlen(text); | |
} | |
void addNewLine(void) { | |
++chars; | |
++lines; | |
} | |
void addChar(void) { | |
++chars; | |
} | |
int main(void) { | |
yylex(); | |
printf("%8d%8d%8d\n", lines, words, chars); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment