agentleFS
rainoftimerainoftime/pl-skills/lexer-generator/SKILL.md

lexer-generator

Skill17 starsChanged 9 months agoApache-2.0View on GitHub

Generates lexical analyzers from regex specifications. Use when: (1)

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 2 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Consider lookahead for better error recovery

    line 54
  2. Distinctive:

    For simple formats: Use existing parsers (JSON, CSV)

    line 93

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Lexer Generator

Generates lexical analyzers (tokenizers) from regex specifications.

When to Use

  • Building compilers or interpreters
  • Creating lexers for DSLs
  • Parsing structured text formats
  • Implementing programming languages

What This Skill Does

  1. Parses regex specs - Converts regex to NFA/DFA
  2. Generates lexers - Produces executable tokenizer code
  3. Handles conflicts - Resolves longest-match ambiguities
  4. Optimizes - Minimizes DFA states

Key Concepts

ConceptDescription
TokenLexical unit (keyword, identifier, number)
LexemeActual source text matched
PositionLocation info for error reporting
Longest matchPrefer longest matching lexeme
PriorityFirst matching rule wins (tie-break)

Tips

  • Handle unicode properly (use Unicode flag)
  • Track line/column for error messages
  • Skip comments and whitespace
  • Handle reserved words (keyword vs identifier)
  • Consider lookahead for better error recovery

Common Issues

IssueSolution
Ambiguous patternsUse longest match + priority
PerformanceUse DFA, not backtracking
UnicodeUse re.UNICODE flag
String literalsUse greedy matching carefully

Related Skills

  • parser-generator - Generate parsers
  • program-transformer - Build ASTs
  • lambda-calculus-interpreter - Execute programs

Canonical References

ReferenceWhy It Matters
Aho et al., "Compilers: Principles, Techniques, and Tools" (1986)Dragon Book; chapters 3-4 on lexical analysis
Thompson, "Programming Techniques: Regular Expression Search Algorithm" (1968)Original NFA construction
McNaughton & Yamada, "Regular Expressions and State Graphs" (1960)DFA construction
Fischer & LeBlanc, "Crafting a Compiler" (1988)Practical lexer design
Russ Cox, "Regular Expression Matching Can Be Simple and Fast" (2007)Modern regex implementation

Tradeoffs and Limitations

Lexer Algorithm Tradeoffs

ApproachProsCons
Regex-basedEasy to writeMay backtrack
NFA-basedSimple constructionLarge states
DFA-basedFast matchingLarge table

When NOT to Build a Custom Lexer

  • For simple formats: Use existing parsers (JSON, CSV)
  • For prototyping: String splitting may suffice
  • For existing tools: Lex/Flex generate efficient lexers

Complexity Considerations

  • DFA construction: O(n²) worst case in states
  • Matching: O(1) per character with DFA
  • NFA simulation: O(n) per character

Limitations

  • Ambiguity: Keywords vs identifiers, longest match rule
  • Context sensitivity: Can't handle indentation (use parser)
  • Unicode: Complex character classes
  • Error recovery: Hard to provide good error messages
  • Performance: Backtracking regex can be exponential

Research Tools & Artifacts

Lexer generators and tools:

ToolLanguageWhat to Learn
FlexCFast lexer generation
ANTLR lexerJavaIntegrated lexing/parsing
PLYPythonLex in Python
rustlexRustSafe lexer generation
JFlexJavaJava lexer generation

Regex Implementations

  • RE2 (Google) - DFA-based, linear time
  • Hyperscan (Intel) - Pattern matching
  • PCRE2 - Backtracking with optimizations

Research Frontiers

1. Unicode Lexing

  • Challenge: Character classes across scripts
  • Approach: Unicode property classes, grapheme clusters
  • Papers: "Unicode Support in Parsing" (various)

2. Incremental Lexing

  • Challenge: Edit-based lexing for IDEs
  • Approach: Cache tokens, re-lex only affected regions
  • Tools: Tree-sitter (incremental)

3. Scannerless Parsing

  • Challenge: Lexer/parser coupling
  • Approach: Combine into single phase
  • Tools: Scannerless GLR parsers

Implementation Pitfalls

PitfallReal ConsequenceSolution
Longest match"=" vs "==" ambiguityUse longest match
Keyword vs identifier"if" recognized as identifierReserve keywords
Greedy matching"//" parsed as "/" + "/"Careful regex
UnicodeWrong character classesUse Unicode properties

The "Keyword vs Identifier" Problem

Simple lexer issue:

if  -> KEYWORD
iff -> IDENTIFIER

Solution: Check keywords after matching identifier:

def tokenize(self, source):
    match = self.longest_match(source)
    if match.type == 'IDENT' and match.text in self.keywords:
        match.type = 'KEYWORD'
    return match

This is why lexers need reservation!

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

Reviews can't be read right now.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.