Skip to content

Instantly share code, notes, and snippets.

@sh-cho
Created October 15, 2017 06:27
Show Gist options
  • Save sh-cho/4bd00f487eedd680fd9b41cb6cf9a6f4 to your computer and use it in GitHub Desktop.
Save sh-cho/4bd00f487eedd680fd9b41cb6cf9a6f4 to your computer and use it in GitHub Desktop.
flex wordcount example
/* 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