Skip to content

Grammar proc macro

dthc

A proc macro that turns a context-free grammar into production rules at compile time: the start of a transpiler generator.

cf!(
  expr   -> term | expr plus term.
  term   -> factor | term mul factor.
  factor -> ident | lparen expr rparen.
  ident  -> alpha (alpha | digit)*.
  alpha  -> 'a'..='z' | 'A'..='Z'.
  digit  -> '0'..'9'.
)

cf! takes a context-free grammar written in a compact notation (| for alternation, * for repetition, character ranges like 'a'..='z') and parses it at compile time into a tree of production rule structs.

The point is grammar-driven transpiler development: change the grammar and the scanner, parser and code generator follow. NFA/DFA construction from the scanner rules was the next step when I moved on; the ideas carried into DSCH and now kolang.