22

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

Any ideas?

Shog9
  • 156,901
  • 35
  • 231
  • 235
Peter Boughton
  • 110,170
  • 32
  • 120
  • 176

4 Answers4

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.

Asrail
  • 144
  • 1
  • 7
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • 3
    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 Nov 23 '09 at 23:05
  • 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 Nov 24 '09 at 09:30
  • 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 Nov 24 '09 at 18:51
  • 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 Nov 24 '09 at 19:49
  • 2
    Welcome to open source. What is it you actually want to do? – Ira Baxter Nov 24 '09 at 20:39
  • (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 Nov 26 '09 at 23:49
  • 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 Nov 27 '09 at 04:36
  • 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 Nov 27 '09 at 19:28
  • @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. – Ira Baxter Sep 03 '13 at 23:31
  • 1
    Link to script in answer is dead (March, 2014) - new link is http://www.antlr3.org/grammar/1153976512034/ecmascriptA3.g – CarlH Mar 24 '14 at 20:12
  • 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. – martian17 Apr 14 '19 at 14:28
  • Note: any errors in that grammar are not mine. I just provided a pointer to somebody else's work. – Ira Baxter Apr 15 '19 at 14:27
  • 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: http://www.semanticdesigns.com/Products/FrontEnds/ColdFusionFrontEnd.html – Ira Baxter Mar 28 '23 at 09:51
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?

Community
  • 1
  • 1
Nosredna
  • 83,000
  • 15
  • 95
  • 122
  • 1
    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 Nov 24 '09 at 09:20
  • 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 Nov 25 '09 at 16:31
  • 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 Nov 26 '09 at 23:55
  • 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 Nov 27 '09 at 20:01
4

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. :)

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • 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. :) – Peter Boughton Mar 02 '15 at 22:38
2

try this link ecmascriptA3 it has the BNF of ecmascript

zeacuss
  • 2,563
  • 2
  • 28
  • 32