Playground
Lexer rules in P1, parser rules in P2, text in P3. Every edit rescans and reparses. With a parse, the text takes its rules' colours; hover a tree node to see the text it covers.
NEW HERE?
A stage is a ready-made example: a lexer grammar, a parser grammar and a text that work together. Pick one to see what each part does before you write your own.
NEW GAME clears the editors so you can start from scratch, and RESET puts the current stage back the way it was.
A lexer (or scanner) is the first step in reading code. It splits raw text into tokens: small, labelled pieces like a number, a name or a +. In 1 + 2 it finds the tokens NUMBER, PLUS and NUMBER.
Each line here is a rule: a token's NAME, then a regex (a regular expression, a pattern for the text it matches). NUMBER [0-9]+ means "one or more digits".
When several rules match, the longest match wins; on a tie, the rule higher up does. Lines starting with # are comments.
A parser takes the lexer's tokens and checks that they come in an order your grammar allows. If they do, it builds a parse tree that shows how the tokens group, for example that in 1 + 2 * 3 the 2 * 3 belongs together.
Each line is a rule: a name, ->, then what it's made of. expr -> expr PLUS term says "an expr can be an expr, then a PLUS token, then a term".
Names you define on the left, like expr, are non-terminals. Every other name is a terminal: the name of a P1 rule. Tokens your rules never mention, like whitespace, are skipped. Leave P2 empty if you only want tokens.
The input your grammars read, like a line of code. Type anything: after every edit, the lexer splits it into tokens again and the parser rebuilds the tree.
The results appear under the editors: the parse tree (or where the text stopped fitting your grammar), the parsing table the parser follows, every token with the rule that matched it, and your grammars as Python code. Click a section's title to open it.
Log in to save your grammars to your account. Each saved grammar gets its own link, so you can come back to it later or share it.