CS 61B Lecture 37: Expression Parsing. Parsing expression grammar. Unlike CFGs, PEGs cannot be ambiguous; if a string parses, it has exactly one valid parse tree.
It is conjectured that there exist context-free languages that cannot be parsed by a PEG, but this is not yet proven. PEGs are well-suited to parsing computer languages, but not natural languages where their performance is comparable to general CFG algorithms such as the Earley algorithm. Definition JFlex - User's Manual. The Fast Lexical Analyser Generator Copyright ©1998-2014 by Gerwin Klein, Steve Rowe, and Régis Décamps.
JFlex User's Manual. Syntactic_recognition. Treetop grammars are written in a custom language based on parsing expression grammars.
Literature on the subject of parsing expression grammars (PEGs) is useful in writing Treetop grammars. PEGs have no separate lexical analyser (since the algorithm has the same time-complexity guarantees as the best lexical analysers) so all whitespace and other lexical niceties (like comments) must be explicitly handled in the grammar.
A further benefit is that multiple PEG grammars may be seamlessly composed into a single parser. Treetop grammars look like this: require "my_stuff" grammar GrammarName include Module::Submodule rule rule_name ... end rule rule_name ... end ... end. The If Works - Talk: Writing a language in 15 minutes. I gave a talk at London Ruby User Group yesterday, based on the work I’ve been doing on Heist, my Scheme interpreter project.
I wrote the core of a basic Scheme interpreter in about 15 minutes as a live-coded demo (well, kind of – the coding was pre-recorded so I could focus on talking), which seemed to go down pretty well. If you missed it (or if you were there and want to watch it again in slow motion), here’s the slides and the video (just code, no narrative (sorry)).