Chris Collins

Tools · dev

Grammar and parser playground

Write a PEG grammar, parse input with it live, and inspect the result tree, JSON, tokens, and rule cross-references — then download the generated parser. Or see one expression parsed under five precedence conventions — PEMDAS, implied multiplication, Python, C and Excel — as trees.

Added · updated 14 October 2026

How to use it

The left pane is a grammar in Peggy syntax — parsing expression grammars with JavaScript actions. The right pane is input. Both re-parse as you type.

Start from an example: arithmetic with precedence that builds an AST, a complete JSON grammar, CSV with quoted fields, INI files, a tiny Lisp, and a G-code subset. Each is small enough to read in one sitting and a good base for your own little language.

  • Result tree shows whatever the start rule returns, as a collapsible tree; objects with a type field are labelled with it, which suits AST nodes.

  • JSON is the same value as text, ready to copy.

  • Tokens lists the leaf rule matches recorded by Peggy’s tracer — rules that matched text without any sub-rule matching — with line, column, and offsets. Whitespace rules named , _, ws, or EOL are hidden.

  • Rules lists every rule, its display name (Number "number" = …), the rules it uses and is used by, and warns about unused rules.

When the input does not match, the error names the line and column, lists what the parser expected there, and draws a caret under the spot. Grammar errors — undefined rules, possible infinite loops, syntax mistakes — are reported the same way against the grammar.

Pick a different Start rule to parse a fragment, such as just a Number. Download parser writes a standalone parser module (ES module, CommonJS, browser global, or UMD) with no runtime dependency; call parse(text) on it.

A quick PEG reminder: / is ordered choice (the first alternative that matches wins), , +, and ? are greedy and never backtrack, &e and !e look ahead without consuming, $e returns the matched text, label:e names a value for the action, and @e plucks one value out of a sequence. Left recursion is not allowed — write head:X tail:(op X) and fold.

Peggy (MIT licence) is loaded from jsDelivr the first time the page opens; your grammar and input stay in the browser.

Precedence conventions

Switch to Precedence conventions to see one expression parsed five ways, each drawn as a tree with its answer:

  • Strict PEMDAS — × and ÷, implied multiplication included, share one level, left to right: 8 ÷ 2(2 + 2) = 16.

  • Implied × binds tighter — writing 2(…) or 2x groups first, as many textbooks and physicists read it: 8 ÷ 2(2 + 2) = 1, and 1/2x = 1/(2x).

  • Python — for powers (binding tighter than a leading minus: -32 is −9), / always a float, and no implied multiplication (2(2+2) would try to call 2).

  • C — no power operator at all: ^ is bitwise XOR, below + and −, so -3^2 is −1; integer / truncates.

  • Excel — negation before ^ (=-3^2 is 9) and ^ left-associative (=232 is 64).

Type numbers, single letters (give them values, such as x = 3), + − × ÷ or * /, ^, or ², and brackets. For each language the card shows what you would actually have to type (* inserted where maths leaves multiplication implied, for Python) and explains what the language would do with the original.

Limits

The precedence view knows arithmetic only: no functions, comparisons or bitwise operators other than C’s reading of ^. Python and C results follow their integer and float rules, not overflow.

Companion to order of operations (chapter 1) and expression trees (chapter 4) of Mathematics, Reconnected.

Linking to an example

Share link copies a URL that opens this tool exactly as you have it. Hand-written links work too: ?grammar=…&input=…&start=rule&trace=0. Peggy grammars can contain JavaScript actions, so a grammar from a link waits for you to press Parse with this grammar (the built-in examples run straight away).

Parameter Meaning Example

grammar, input

a Peggy grammar and the text to parse

grammar=…&input=…

start

start rule

start=Number

trace

0 turns token tracing off

trace=0

expr

Precedence conventions: the expression

expr=8/2(2%2B2)

vars

values for letters

vars=x%3D4

example

a book example (below), or a built-in grammar: arith, json, csv, …

example=ch01-viral

Links with expr open the precedence view (#precedence). In expr a + typed by hand means plus — ?expr=8/2(2+2) — while %2B always means plus.

Examples from Mathematics, Reconnected (?example=<name>):

example= Shows

ch01-viral

8 ÷ 2(2 + 2): 16 under strict PEMDAS, 1 when implied multiplication binds tighter

ch01-neg-square

−3²: −9 in maths and Python, 9 in Excel, −1 in C (XOR)

ch01-power-tower

232: 512 (right-associative), but 64 in Excel

ch01-gradebook

the spreadsheet bug =A1+B1+C1/3 with 70, 80, 90: 180, not 80

ch01-half-x

1/2x with x = 4: 2 under PEMDAS, 1/8 with implied × first

ch04-neg-x-squared

−x² with x = 3: −9, except Excel’s =-A1^2, which gives 9

Runs entirely in your browser — network requests go only to the services named on this page.