View on GitHub

CS374

Principles of Programming Languages

BNF/EBNF Grammar Tester

Type a grammar, then type a string, and the tester tells you whether the grammar can derive that string. When it can, you see the parse tree and a leftmost derivation written the way the labs write them. When it cannot, you see where it got stuck and what the grammar would have accepted there. It runs entirely in your browser: nothing is sent anywhere, and there is no account.

Two settings matter before you start:

Plain BNF (enables { }, [ ], ( ))

Loading the tester… If this message stays, JavaScript is turned off in your browser.

Course Notation Cheat Sheet

This is the notation table from the BNF Workshop. Everything in the top half works in plain BNF; the bottom half needs Allow EBNF.

You write It means Example
<name> ::= ... a production: the nonterminal on the left is defined by the right side <sign> ::= "+" | "-"
<name> a nonterminal, which must have its own production somewhere <digit>
"x" or 'x' a terminal, the literal text that appears in the string "("
| alternation: exactly one of the choices is used "+" | "-"
<empty> the empty string, the BNF way to let a recursive rule stop <exprs> ::= <expr> <exprs> | <empty>
(* ... *) a comment, ignored by the tester (* spaces ignored between atoms *)
{ X } (EBNF) zero or more copies of X { <digit> }
[ X ] (EBNF) X once or not at all [ <sign> ]
( X | Y ) (EBNF) grouping inside a larger rule ( "," | ";" )

A production may continue onto the next line, the way the labs line up long alternatives under the ::=.

About whitespace. With Ignore whitespace between terminals ticked, spaces, tabs and newlines may appear between any two terminals, which is what the labs mean by “spaces ignored between atoms.” The catch is that this also lets a space appear inside a token that your grammar spells one character at a time, so 1 2 is accepted as the number 12. To test spacing rules exactly, untick the box and write the whitespace into the grammar yourself, for example <ws> ::= " " <ws> | " ".

What the Results Tell You

The tester accepts every context-free grammar, including left-recursive and ambiguous ones, because it parses with Earley’s algorithm rather than with recursive descent. It checks your grammar; it does not grade it. An accepted string shows that your grammar can derive it, so also test strings that should be rejected, which is where most grammar mistakes hide.