A collection of 15 small Lex/Flex programs, covering the lexical-analysis topics that usually come up in a compiler design course and lab.
sudo apt install flex gcc
make # builds everything into bin/
make clean
Or build one by hand: flex 01_token_recognizer.l gcc lex.yy.c -o tokens
| # | File | What it does | Try it with |
|---|---|---|---|
| 01 | 01_token_recognizer.l | Tokenizes C-like code (keywords, ids, numbers, ops) | tests/sample.c |
| 02 | 02_word_count.l | Counts lines, words, characters (mini wc) |
any text file |
| 03 | 03_remove_comments.l | Strips // and /* */ comments |
tests/sample.c |
| 04 | 04_numbers.l | Classifies int/float/scientific/hex/octal numbers | tests/numbers.txt |
| 05 | 05_identifier_check.l | Valid identifier / keyword / invalid, per line | tests/identifiers.txt |
| 06 | 06_email_url_validator.l | Finds emails and URLs in text | tests/text.txt |
| 07 | 07_symbol_table.l | Builds a symbol table with counts and first line | tests/sample.c |
| 08 | 08_error_reporter.l | Lexical errors with line/column numbers | tests/errors.txt |
| 09 | 09_html_tags.l | Extracts HTML tags, skips comments | tests/page.html |
| 10 | 10_string_literals.l | String literals with escapes, unterminated strings | tests/strings.txt |
| 11 | 11_vowel_consonant.l | Counts vowels, consonants, digits, symbols | type some text |
| 12 | 12_keyword_counter.l | Token-category statistics for a C file | tests/sample.c |
| 13 | 13_infix_tokenizer.l | Expression tokenizer + parenthesis check | tests/expr.txt |
| 14 | 14_number_base_converter.l | Binary/octal/decimal/hex conversion | tests/bases.txt |
| 15 | 15_mini_language_lexer.l | Pascal-like language lexer with token codes | tests/program.pas |
make
./bin/01_token_recognizer < tests/sample.c
./bin/12_keyword_counter tests/sample.c
Regular definitions, longest-match and rule priority, start conditions
(%x), yywrap, yyin, yytext/yyleng, YY_USER_ACTION, <<EOF>>,
%option, manual symbol tables and error recovery.