EBNF for ECMAScript?
Asked Answered
G

3

23

I'm trying to find a good EBNF description of ECMAScript, but so far I've not found anything complete.

Any ideas?

Guyguyana answered 23/11, 2009 at 22:38 Comment(0)
M
16

How about the ECMAScript standard? Complete by definition :-}

EDIT: If you want an existing grammar, try one of the grammar generator tools sites. For ANTLR, here's the ECMAScript grammar. I know nothing of its quality but the ANTLR can produce good parsers if the grammer is constructed with care. You'll probably find the grammar also interwoven with bunch of ANTLR stuff, so it may suffer from some of the same problem as the standard from your point of view. At least you can delete all that stuff out.

Mahmoud answered 23/11, 2009 at 22:53 Comment(13)
Precise maybe, but it's a PDF that spreads segments of the syntax throughout the document and uses bold for literal tokens - meaning that (automatic or manual) extraction of a complete EBNF out of it would be a massively long-winded pain in the arse.Guyguyana
Yeah, since the ANTLR one is actually a grammar I should be able to create a script to convert it. Will have a go at that later and see where I get to. (Not got on too well with ANTLR itself, so I'm experimenting with assorted other tools to see what I prefer).Guyguyana
Heh, so just testing that ANTLR script to make sure it works, and I get 171 warnings / 28 errors / BUILD FAIL. :/ The first error is highlighting the apparently trivial charVocabulary in options, so not sure what's up there. :( I really hope I wont end up forced to trek through almost two hundred PDF pages in order to manually compile a working grammar.Guyguyana
Quick update... after trying a couple more broken ANTLR Ecma/JS attempts, I've found an ANTLR ECMAScript 3 parser ( research.xebic.com/es3 ) which might actually be complete (to v3 at least), though it's got so much custom stuff in there I'm not going to get a generic BNF out of it... so looks like I'm back to wrestling with ANTLR yet again. :/Guyguyana
Welcome to open source. What is it you actually want to do?Mahmoud
(I do wish StackOverflow would notify me of comments like this!) Anyway, what I'm trying to achieve is firstly a working ECMA parser, and then I'll be modifying the grammar to support a very similar language (CFScript) and then again for its parent language (CFML), and from that I'll be producing a 'modeller' that scans entire applications and provides assorted useful information/features. I've got another question covering my latest problem: #1793216Guyguyana
So you wantto build and manage a variaety of ECMAScript dialects? Check out www.semanticdesigns.com/Products/FrontEnds/ECMASciptFrontEnd.html. This is a well-tested ECMAScript parser with ability to handle dialect variantsMahmoud
Kinda. CFScript is close enough to an ECMAScript dialect, but CFML is tag-based (but not actually markup/SGML) and can 'host' CFScript within it. That link looks interesting, but unfortunately the tools are Windows-only, and I need cross-platform. (There's quite a few Mac users in the CF community.)Guyguyana
@PeterBoughton: September 2012: Tripped over this old answer. Update: re cross-platform: DMS runs on Windows, and nicely IMHO on Wine under Linux and MacOS.Mahmoud
Link to script in answer is dead (March, 2014) - new link is antlr3.org/grammar/1153976512034/ecmascriptA3.gWalworth
About the grammar in the answer edit, it's almost correct, but there are some minor mistakes like "return" being allowed in global scope. I guess it wouldn't matter if you set up a verifier after this though.Franklyn
Note: any errors in that grammar are not mine. I just provided a pointer to somebody else's work.Mahmoud
Only 13 years later.... it sounds likewhat you really want is to build a ColdFusion parser. ECMAScript is overkill for the CFScript language; a small custom grammar works for that. How do I know? I built a complete ColdFusion parser for DMS: semanticdesigns.com/Products/FrontEnds/ColdFusionFrontEnd.htmlMahmoud
C
9

Chapter 2 of Crockford's JavaScript: The Good Parts diagrams (you guessed it) the good parts.

Here are a couple stabs at BNF for JavaScript:

from this earlier SO question:

Repository of BNF Grammars?

Claycomb answered 24/11, 2009 at 2:46 Comment(4)
The Tom Copeland one is incomplete (no tokens or terminals) - it's generated from a JavaCC script, and even when I tried using the original script I kept getting errors. For the other dherman/ClassicJavascript link I haven't got a clue what it's about? :SGuyguyana
Yeah. I just think no one is very interested in EBNF for scripting languages. Dynamic languages don't lend themselves to recursive descent compilation. What good is a traditional compiler when you can build functions out of strings on the fly? Speeding up JS is all about Forth-like TILs, runtime analysis of common pathways, tokenization, etc.Claycomb
I do have some vague ideas about how I might approach the dynamic stuff - but so far I haven't even got that far! Can you provide any explain (or provide links) what "TILs" is/are - too much hay for my searches to return anything. :(Guyguyana
Threaded Interpreted Languages (languages like Forth). You should ready up on Python compilers. The question is how much runtime do you bring along. The Microsoft DLR is pretty interesting, too.Claycomb
G
5

I am working on -based parser for ECMAScript. Here's my grammar so far:

See also Tom Copeland's BNF for EcmaScript:

As well as "Yet Another JavaScript Interpreter":

As well as Dojo Toolkit's Grammar (probably based on YAJI):

From my point of view, YAJI or Dojo Toolkit's are the best and the most complete (to the best of my knowledge). I'm basing my work on those but want to make it even more complete (5.1/6) and standard-conform yet practical.

ECMAScript grammar is very tricky. It has a few huge caveats:

  • Regular Expression Literals vs. Division (you can't distinguishing them on the lexer level)
  • Automatic Semicolon Insertion (ASI)

So please be aware of that. Implementing those is very tricky.

If your target platform Java and you don't mind JavaCC, I'd be glad if you join my project. My grammar is actually ready (builds/compiles without warnings). I'm now working on test suites to cover each and every feature and production. I anticipate some problems with regex literals and ASIs though.

ps. I've just noticed that the question is from 09 so my invitation is probably too late. :)

Gloat answered 14/2, 2015 at 9:27 Comment(1)
"ps. I've just noticed that the question is from 09 so my invitation is probably too late." Nope - the project I wanted this for has been sitting on the shelf for quite a while, though unfortunately I don't have the time to resurrect it at the moment, but I do still have vague plans to do so at some point. I've bookmarked your project for when I get a chance again. Thanks. :)Guyguyana

© 2022 - 2024 — McMap. All rights reserved.