Skip to content

Instantly share code, notes, and snippets.

@yxy
Created February 29, 2016 07:42
Show Gist options
  • Save yxy/7e10ed5af23c36b1a9d0 to your computer and use it in GitHub Desktop.
Save yxy/7e10ed5af23c36b1a9d0 to your computer and use it in GitHub Desktop.
unix tool wc written in lex
%{
int nchar, nword, nline;
%}
%%
\n { nline++; nchar++ ;}
[^ \t\n]+ { nword++; nchar += yyleng; }
. { nchar++; }
%%
int main(int argc, char *argv[]) {
yyin = fopen(argv[1], "r");
yylex();
printf("%d\t%d\t%d\n", nchar, nword, nline);
fclose(yyin);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment