Cheatography
https://cheatography.com
Cheatsheet for the Lark parsing toolkit
Lark Options
|
Earley - default |
|
LALR(1) |
|
Enable debug prints |
|
Revert to simple lexer |
|
Return all derivations for Earley |
|
Set starting rule |
|
Enable grammar caching |
|
Apply transformer to tree (for LALR) |
|
Fill tree instances with line number information |
|
[]
returns None
when not matched |
|
Don't remove unnamed terminals |
|
Provide a wrapper for the lexer |
|
Provide an alternative for Tree
|
|
|
Tree Reference
|
Rule name |
|
Rule matches |
|
Positional information, if enabled |
|
|
Iterate all subtrees |
|
Find by rule |
|
Find by predicate |
|
Token Reference
|
Terminal name |
|
Matched text |
|
Index in source text |
|
|
|
|
|
|
Tokens inherit from str
, so all string operations are valid (such as token.upper()
).
|
|
Grammar Definitions
|
Define a rule |
|
Define a terminal |
|
Rule with priority n |
|
Terminal with priority n |
|
Comment |
|
Ignore terminal in input |
|
Import terminal from file |
|
Declare a terminal without a pattern (used for postlex) |
|
Define template |
|
Use template |
Rules consist of values, other rules and terminals.
Terminals only consist of values and other terminals.
Grammar Patterns
|
Match sequence |
|
Group together (for operations) |
|
Match one or the other |
|
Match 0 or 1 instances |
|
Match 0 or 1 instances |
|
Match 0 or more instances |
|
Match 1 or more instances |
|
Match exactly 3 instances |
|
Match between 3 to 5 instances |
Terminal Atoms
|
String to match |
|
Case-insensitive string |
|
Regular Expression |
|
Regular Expression with flags |
|
Literal range |
Tree Shaping
|
"foo" will be filtered out |
|
"foo" will be kept |
|
/foo/ will be kept |
|
Filter out this terminal |
|
Always inline this rule |
|
Inline if matched 1 child |
|
Rename this derivation |
Rules are a branch (node) in the resulting tree, and its children are its matches, in the order of matching.
Terminals (tokens) are always values in the tree, never branches.
Inlining rules means removing their branch and replacing it with their children.
Examples
// Define template for comma-separated list
cs_list{item}: item ("," item)*
// Use template to make a list of numbers
number_list: cs_list{ number }
// Example of a terminal for a Python comment
PY_COMMENT: /#[^\n]*/
// Example of a terminal for C comment
C_COMMENT: "/" /.?/s "*/"
|
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets