views:

420

answers:

2

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

Any ideas?

+3  A: 

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.

Ira Baxter
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.
Peter Boughton
See EDIT to answer.
Ira Baxter
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).
Peter Boughton
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.
Peter Boughton
Quick update... after trying a couple more broken ANTLR Ecma/JS attempts, I've found an ANTLR ECMAScript 3 parser ( http://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. :/
Peter Boughton
Welcome to open source. What is it you actually want to do?
Ira Baxter
(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:http://stackoverflow.com/questions/1792716/antlr-parser-hanging-at-proxy-handshake-call
Peter Boughton
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 variants
Ira Baxter
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.)
Peter Boughton
+2  A: 

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:

http://stackoverflow.com/questions/334479/repository-of-bnf-grammars/334539#334539

Nosredna
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? :S
Peter Boughton
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.
Nosredna
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. :(
Peter Boughton
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.
Nosredna