Treetop (parser)
Treetop is a Ruby library that allows you to create parsers easily by describing them using a Parsing Expression Grammar (PEG). - A quick intro
see also
Home (still alive)
- performance are good compared to other
Doc / Tutorials
- A quick intro
- Turning a parse tree into an abstract syntax tree (AST)
- filtering parse tree to get an ast
- Treetop’s Confusing Behavior
- class vs module
- use rule ( subrule1 / subrule2 ) <class object> see 1
- treetop will inject several definition so using module is more general and adviced, see 2
- filtering parse tree to get an ast
In the case where “foo” matches “something_else”, the SyntaxNode for “something_else” will have two mixed-in modules (both “B” and “B_In_Foo”). Use parentheses around a list of alternates if you want to mix in the same module regardless of which possibility is matched.
- Treetop: Typical Errors
- error reporting à la clang
- Matching whitespaces
- prevent AST squashing: use 1..1
- Doc
- Semantic Interpretation
- Methods Available on Treetop::Runtime::SyntaxNode - elements elements of the matched sequence
- labels: for subexpression, like citrus
- Syntactic Recognition
- c like comment grammar
- keywords
- Striping spaces in a SQL parser
- keywords must not be immediately followed by a symbol character. I use negative lookahead for this.
- The top rule consumes leading whitespace, then almost every rule consumes following whitespace (this is my policy). You should test your grammar with minimum and maximum whitespace.
- Advanced Techniques
- Matching a Keyword But Not Words Prefixed Therewith
- Semantic Interpretation
- Debugging
- using in ruby
- parser options: start from a different rule / continue
- debugging option
- AST guideline
- Advanced Grammar debugging
- Generating grammar, and instrumenting code
- using in ruby
- google groups (2018)
- custom data for syntax node => his is a significant weakness in the design of Treetop. I made a custom proxy for the “input” and attached my Context to it. You might get away with doing something similar
- Ruby Conference 2007 Treetop: Syntactic Analysis with Ruby by Nathan Sobo
Some treetop example
- Clifford Heath CQL
- my sql grammar POC sql
- A “grue”some look at Statemachine and Treetop
Alternatives
Written on July 16, 2020, Last update on August 30, 2024
ruby
parser
AST