Skip to content

Instantly share code, notes, and snippets.

@FedericoPonzi
Created June 16, 2018 10:10
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 FedericoPonzi/fb5c968759f965a2faeb9e763ac3fda6 to your computer and use it in GitHub Desktop.
Save FedericoPonzi/fb5c968759f965a2faeb9e763ac3fda6 to your computer and use it in GitHub Desktop.
Simple lex example to count lines and chars.
%{
#include <stdio.h>
int num_lines = 0, num_chars = 0;
%}
%%
\n ++num_lines; ++num_chars;
. ++num_chars;
%%
main() {
yylex();
printf("# of lines = %d, # of chars = %d\n", num_lines, num_chars);
}
@FedericoPonzi
Copy link
Author

Compile with:

lex counter.lex
gcc lex.yy.c -ll -o counter

and run with:

./counter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment