Creatively writing Language Grammars

Writing grammars in BNF is actually kind of fun! Conforming to specifications is not. Let me explain: It’s possible to write grammars with trees of rules, some of which don’t actually do much other than to eventually create grouping nodes in the parse tree. ECMA-116 is just one such rabbit-hole grammar, I’d say:

That’s very many types of statements! Contrast it with my prototype grammar:

I waver between choosing names for my grammar rules: stick with the wisdom of the specification or simplify it and make it more compact. “Beware discarding wisdom of the elders” vs “Let’s clean this up and modernize!” I do see why ECMA-116 was so verbose: lexing and parsing can be very tricky and since they were doing it in Assembly, being very unambiguous was a supreme virtue. Now, there are ways of course, of proving the equivalency of two grammars mathematically. I’m not sure I’ll go Full Turing praise Saint Baukus with a multipage LaTeX proof, but it sure sounds like a good way to procrastinate :-)

But seriously, I need to leave a bit of my own contribution to the grammar here instead of dully copying the spec. Thus, I will simplify it to mostly match my prototype’s grammar above, but with some inspiration from the rigorous unambiguous ruleset of the spec.


Leave a comment