Hasty Briefsbeta

Bilingual

Practical parsing with PEG and cpp-peglib - Bert Hubert's writings

a day ago
  • Parsing Expression Grammars (PEG) are simple to write and use, especially with the cpp-peglib library.
  • cpp-peglib is a single-include C++ library that requires no preprocessing or external tools.
  • Example: parsing a vector format using PEG grammar and attaching functions to extract numbers.
  • Example: handling escaped characters in quoted strings with the choice operator and token manipulation.
  • Example: building a full Prometheus metrics parser including labels, comments, and special values (NaN, +Inf, -Inf).
  • Regular expressions in PEG do not need to be perfect; validation can be delegated to parsing functions.
  • cpp-peglib supports Unicode, AST generation, and provides online tools for grammar development.
  • PEG with cpp-peglib enables writing robust and secure parsers with less code compared to hand-crafted or regex-based approaches.