-
-
Save celestialphineas/2ab84ca3df254bce0f79d61bdfa9edb0 to your computer and use it in GitHub Desktop.
Lex program for generating a lexer implementing word counting.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| %{ | |
| /* Example of Lex program to count words in a file. */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| /* Numbers of characters, words and lines */ | |
| int nchar = 0; | |
| int nword = 0; | |
| int nline = 0; | |
| %} | |
| %% | |
| [a-zA-Z0-9]+ { nchar += strlen(yytext); nword++; } | |
| \n { nchar++; nline++; } | |
| . { nchar++; } | |
| %% | |
| int main(void) | |
| { | |
| yylex(); | |
| printf("%d chars, %d words, %d lines\n", nchar, nword, nline); | |
| return 0; | |
| } |
Author
celestialphineas
commented
Mar 12, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment