%{
#include <stdio.h>
int token_count = 0; // Variable to hold the token count
%}
%%
[ \t\n]+ { /* Ignore whitespace (spaces, tabs, and newlines) */ }
[0-9]+ { token_count++; } // Match numbers
[A-Za-z_][A-Za-z0-9_]* { token_count++; } // Match identifiers (words)
[+\-*/=<>] { token_count++; } // Match operators (+, -, *, /, =, <, >)
. { token_count++; } // Match any other single character (like punctuation)
%%
int main()
{
yylex(); // Start the lexical analysis
printf
("Total
number of tokens
: %d\n", token_count); // Print the token count return 0;
}
JXsKI2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgdG9rZW5fY291bnQgPSAwOyAgLy8gVmFyaWFibGUgdG8gaG9sZCB0aGUgdG9rZW4gY291bnQKJX0KCiUlCgpbIFx0XG5dKyAgICAgIHsgLyogSWdub3JlIHdoaXRlc3BhY2UgKHNwYWNlcywgdGFicywgYW5kIG5ld2xpbmVzKSAqLyB9ClswLTldKyAgICAgICAgeyB0b2tlbl9jb3VudCsrOyB9ICAvLyBNYXRjaCBudW1iZXJzCltBLVphLXpfXVtBLVphLXowLTlfXSogeyB0b2tlbl9jb3VudCsrOyB9ICAvLyBNYXRjaCBpZGVudGlmaWVycyAod29yZHMpClsrXC0qLz08Pl0gICAgeyB0b2tlbl9jb3VudCsrOyB9ICAvLyBNYXRjaCBvcGVyYXRvcnMgKCssIC0sICosIC8sID0sIDwsID4pCi4gICAgICAgICAgICAgeyB0b2tlbl9jb3VudCsrOyB9ICAvLyBNYXRjaCBhbnkgb3RoZXIgc2luZ2xlIGNoYXJhY3RlciAobGlrZSBwdW5jdHVhdGlvbikKCiUlCgppbnQgbWFpbigpIAp7CiAgICB5eWxleCgpOyAgLy8gU3RhcnQgdGhlIGxleGljYWwgYW5hbHlzaXMKICAgIHByaW50ZigiVG90YWwgbnVtYmVyIG9mIHRva2VuczogJWRcbiIsIHRva2VuX2NvdW50KTsgIC8vIFByaW50IHRoZSB0b2tlbiBjb3VudAogICAgcmV0dXJuIDA7Cn0K