Skip to content

Instantly share code, notes, and snippets.

@nkanaev
Created June 10, 2019 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkanaev/e586573090f91fbcf00e518c3e50d1b4 to your computer and use it in GitHub Desktop.
Save nkanaev/e586573090f91fbcf00e518c3e50d1b4 to your computer and use it in GitHub Desktop.
%{
#include <stdio.h>
#include <stdlib.h>
int words = 0, lines = 0, chars = 0;
%}
%%
\n { chars += yyleng; lines += 1; };
[ \t]+ { chars += yyleng; };
[^ \t\n]+ { chars += yyleng; words += 1; };
%%
// lex -o wc.c wc.l && cc wc.c -o wc
int yywrap() {
return 1;
}
int main(int argc, char* argv[]) {
char *input = "";
if (argc == 2) {
input = argv[1];
yyin = fopen(input, "r");
}
yylex();
printf("%8d%8d%8d %s\n", lines, words, chars, input);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment