This is a live demo of a CFG parser. It parses CFG's written in the BNF. You're seeing the output. Yeah, good luck with those TLA's ;)

By Peter van der Zee, September 2010

The example here is the ECMAScript specification, version 5. It is the exact CFG as used by my parser and the same CFG as it occurs in the formal specificiations, with minor edits to accomodate my parser ;) View the source of this page to see the source CFG used to generate it.

The script can generate a string containing html, you should probably see that below right now. The productions are clickable (anchors) which should make navigation easier. Elements that are not found as productions are striked through. The script can tell you which productions have not been seen as part of another productions (which is probably at least one rule; your root). If you want that information just create a "debug" function in global scope :)

Script not working for you? Check the pre-generated static html output :)

I've also written a blog post about it, in case you want to read more...


A1 Lexical Grammar

: SourceCharacter : InputElementDiv WhiteSpace LineTerminator Comment Token DivPunctuator : InputElementRegExp WhiteSpace LineTerminator Comment Token RegularExpressionLiteral : TAB '0009' : VT '000B' : FF '000C' : SP '0020' : NBSP '00A0' : BOM 'FEFF' : USP : WhiteSpace TAB VT FF SP NBSP BOM USP : LF '000A' : CR '000D' : LS '2028' : PS '2029' : LineTerminator LF CR LS PS : LineTerminatorSequence LF CR no{LF}here LS PS CR LF : Comment MultiLineComment SingleLineComment : MultiLineComment "/*" optional{MultiLineCommentChars} "*/" : MultiLineCommentChars MultiLineNotAsteriskChar optional{MultiLineCommentChars} "*" no{"/"}here optional{PostAsteriskCommentChars} : PostAsteriskCommentChars MultiLineNotForwardSlashOrAsteriskChar optional{MultiLineCommentChars} "*" no{"/"}here optional{PostAsteriskCommentChars} : MultiLineNotAsteriskChar SourceCharacter except{"*"} : MultiLineNotForwardSlashOrAsteriskChar SourceCharacter except{any{"/" "*"}} : SingleLineComment "//" optional{SingleLineCommentChars} : SingleLineCommentChars SingleLineCommentChar optional{SingleLineCommentChars} : SingleLineCommentChar SourceCharacter except{LineTerminator} : Token IdentifierName Punctuator NumericLiteral StringLiteral : Identifier IdentifierName except{ReservedKeyword} : IdentifierName IdentifierStart IdentifierName IdentifierPart : IdentifierStart UnicodeLetter "$" "_" "\" UnicodeEscapeSequence : ZWNJ '200C' : ZWJ '200D' : IdentifierPart IdentifierStart UnicodeCombiningMark UnicodeDigit UnicodeConnectorPunctuation ZWNJ ZWJ : UnicodeLetter : UnicodeCombiningMark : UnicodeDigit : UnicodeConnectorPunctuation : ReservedKeyword Keyword FutureReservedKeyword NullLiteral BooleanLiteral : Keyword any{"break" "case" "catch" "continue" "debugger" "default" "delete" "do" "else" "finally" "for" "function" "if" "in" "instanceof" "new" "return" "switch" "this" "throw" "try" "typeof" "var" "void" "while" "with"} : FutureReservedKeyword any{"class" "const" "enum" "export" "extends" "import" "super"} : Punctuator any{"{" "}" "(" ")" "[" "]" "." ";" "," "<" ">" "<=" ">=" "==" "!=" "===" "!==" "+" "-" "*" "%" "++" "--" "|" "^" "<<" ">>" ">>>" "&" "|" "^" "!" "~" "&&" "||" "?" ":" "=" "+=" "-=" "*=" "%=" "<<=" ">>=" ">>>=" "&=" "|=" "^="} : DivPunctuator any{"/" "/="} : Literal NullLiteral BooleanLiteral NumericLiteral StringLiteral RegularExpressionLiteral : NullLiteral "null" : BooleanLiteral "true" "false" : NumericLiteral DecimalLiteral HexIntegerLiteral : DecimalLiteral DecimalIntegerLiteral "." optional{DecimalDigits} optional{ExponentPart} "." DecimalDigits optional{ExponentPart} DecimalIntegerLiteral optional{ExponentPart} : DecimalIntegerLiteral "0" NonZeroDigit optional{DecimalDigits} : DecimalDigits DecimalDigit DecimalDigits DecimalDigit : DecimalDigit any{"0" "1" "2" "3" "4" "5" "6" "7" "8" "9"} : NonZeroDigit any{"1" "2" "3" "4" "5" "6" "7" "8" "9"} : ExponentPart ExponentIndicator SignedInteger : ExponentIndicator any{"e" "E"} : SignedInteger DecimalDigits "+" DecimalDigits "-" DecimalDigits : HexIntegerLiteral "0x" HexDigit "0X" HexDigit HexIntegerLiteral HexDigit : HexDigit any{"0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F" "a" "b" "c" "d" "e" "f"} : StringLiteral """ optional{DoubleStringCharacters} """ "'" optional{SingleStringCharacters} "'" : DoubleStringCharacters DoubleStringCharacter optional{DoubleStringCharacters} : SingleStringCharacters SingleStringCharacter optional{SingleStringCharacters} : DoubleStringCharacter SourceCharacter except{any{""" "\" LineTerminator}} "\" EscapeSequence LineContinuation : SingleStringCharacter SourceCharacter except{any{"'" "\" LineTerminator}} "\" EscapeSequence LineContinuation : LineContinuation "\" LineTerminatorSequence : EscapeSequence CharacterEscapeSequence "0" no{DecimalDigit}here HexEscapeSequence UnicodeEscapeSequence : CharacterEscapeSequence SingleEscapeCharacter NonEscapeCharacter : SingleEscapeCharacter any{"'" """ "\" "b" "f" "n" "r" "t" "v"} : NonEscapeCharacter SourceCharacter except{any{EscapeCharacter LineTerminator}} : EscapeCharacter SingleEscapeCharacter DecimalDigit "x" "u" : HexEscapeSequence "x" HexDigit HexDigit : UnicodeEscapeSequence "u" HexDigit HexDigit HexDigit HexDigit : RegularExpressionLiteral "/" RegularExpressionBody "/" optional{RegularExpressionFlags} : RegularExpressionBody RegularExpressionFirstChar RegularExpressionChars : RegularExpressionChars "[empty]" RegularExpressionChars RegularExpressionChar : RegularExpressionFirstChar RegularExpressionNonTerminator except{any{"*" "\" "/" "["}} RegularExpressionBackslashSequence RegularExpressionClass : RegularExpressionChar RegularExpressionNonTerminator except{any{"\" "/" "["}} RegularExpressionBackslashSequence RegularExpressionClass : RegularExpressionBackslashSequence "\" RegularExpressionNonTerminator : RegularExpressionNonTerminator SourceCharacter except{LineTerminator} : RegularExpressionClass "[" RegularExpressionClassChars "]" : RegularExpressionClassChars "[empty]" RegularExpressionClassChars RegularExpressionClassChar : RegularExpressionClassChar RegularExpressionNonTerminator except{any{"]" "\"}} RegularExpressionBackslashSequence

A2 Number conversions

: RegularExpressionFlags IdentifierPart RegularExpressionFlags IdentifierPart : StringNumericLiteral optional{StrWhiteSpace} optional{StrWhiteSpace} StrNumericLiteral optional{StrWhiteSpace} : StrWhiteSpace StrWhiteSpaceChar optional{StrWhiteSpace} : StrWhiteSpaceChar WhiteSpace LineTerminator : StrNumericLiteral StrDecimalLiteral HexIntegerLiteral : StrDecimalLiteral StrUnsignedDecimalLiteral "+" StrUnsignedDecimalLiteral "-" StrUnsignedDecimalLiteral

A3 Expressions

: StrUnsignedDecimalLiteral "Infinity" DecimalDigits "." optional{DecimalDigits} optional{ExponentPart} "." DecimalDigits optional{ExponentPart} DecimalDigits optional{ExponentPart} : PrimaryExpression "this" Identifier Literal ArrayLiteral ObjectLiteral "(" Expression ")" : ArrayLiteral "[" optional{Elision} "]" "[" ElementList "]" "[" ElementList "," optional{Elision} "]" : ElementList optional{Elision} AssignmentExpression ElementList "," optional{Elision} AssignmentExpression : Elision "," Elision "," : ObjectLiteral "{" "}" "{" PropertyNameAndValueList "}" "{" PropertyNameAndValueList "," "}" : PropertyNameAndValueList PropertyAssignment PropertyNameAndValueList "," PropertyAssignment : PropertyAssignment PropertyName ":" AssignmentExpression "get" PropertyName "(" ")" "{" FunctionBody "}" "set" PropertyName "(" PropertySetParameterList ")" "{" FunctionBody "}" : PropertyName IdentifierName StringLiteral NumericLiteral : PropertySetParameterList Identifier : MemberExpression PrimaryExpression FunctionExpression MemberExpression "[" Expression "]" MemberExpression "." IdentifierName "new" MemberExpression Arguments : NewExpression MemberExpression "new" NewExpression : CallExpression MemberExpression Arguments CallExpression Arguments CallExpression "[" Expression "]" CallExpression "." IdentifierName : Arguments "(" ")" "(" ArgumentList ")" : ArgumentList AssignmentExpression ArgumentList "," AssignmentExpression : LeftHandSideExpression NewExpression CallExpression : PostfixExpression LeftHandSideExpression LeftHandSideExpression no{LineTerminator}here "++" LeftHandSideExpression no{LineTerminator}here "--" : UnaryExpression PostfixExpression "delete" UnaryExpression "void" UnaryExpression "typeof" UnaryExpression "++" UnaryExpression "--" UnaryExpression "+" UnaryExpression "-" UnaryExpression "~" UnaryExpression "!" UnaryExpression : MultiplicativeExpression UnaryExpression MultiplicativeExpression "*" UnaryExpression MultiplicativeExpression "/" UnaryExpression MultiplicativeExpression "%" UnaryExpression : AdditiveExpression MultiplicativeExpression AdditiveExpression "+" MultiplicativeExpression AdditiveExpression "-" MultiplicativeExpression : ShiftExpression AdditiveExpression ShiftExpression "<<" AdditiveExpression ShiftExpression ">>" AdditiveExpression ShiftExpression ">>>" AdditiveExpression : RelationalExpression ShiftExpression RelationalExpression "<" ShiftExpression RelationalExpression ">" ShiftExpression RelationalExpression "<=" ShiftExpression RelationalExpression ">=" ShiftExpression RelationalExpression "instanceof" ShiftExpression RelationalExpression "in" ShiftExpression : RelationalExpressionNoIn ShiftExpression RelationalExpressionNoIn "<" ShiftExpression RelationalExpressionNoIn ">" ShiftExpression RelationalExpressionNoIn "<=" ShiftExpression RelationalExpressionNoIn ">=" ShiftExpression RelationalExpressionNoIn "instanceof" ShiftExpression : EqualityExpression RelationalExpression EqualityExpression "==" RelationalExpression EqualityExpression "!=" RelationalExpression EqualityExpression "===" RelationalExpression EqualityExpression "!==" RelationalExpression : EqualityExpressionNoIn RelationalExpressionNoIn EqualityExpressionNoIn "==" RelationalExpressionNoIn EqualityExpressionNoIn "!=" RelationalExpressionNoIn EqualityExpressionNoIn "===" RelationalExpressionNoIn EqualityExpressionNoIn "!==" RelationalExpressionNoIn : BitwiseANDExpression EqualityExpression BitwiseANDExpression "&" EqualityExpression : BitwiseANDExpressionNoIn EqualityExpressionNoIn BitwiseANDExpressionNoIn "&" EqualityExpressionNoIn : BitwiseXORExpression BitwiseANDExpression BitwiseXORExpression "^" BitwiseANDExpression : BitwiseXORExpressionNoIn BitwiseANDExpressionNoIn BitwiseXORExpressionNoIn "^" BitwiseANDExpressionNoIn : BitwiseORExpression BitwiseXORExpression BitwiseORExpression "|" BitwiseXORExpression : BitwiseORExpressionNoIn BitwiseXORExpressionNoIn BitwiseORExpressionNoIn "|" BitwiseXORExpressionNoIn : LogicalANDExpression BitwiseORExpression LogicalANDExpression "&&" BitwiseORExpression : LogicalANDExpressionNoIn BitwiseORExpressionNoIn LogicalANDExpressionNoIn "&&" BitwiseORExpressionNoIn : LogicalORExpression LogicalANDExpression LogicalORExpression "||" LogicalANDExpression : LogicalORExpressionNoIn LogicalANDExpressionNoIn LogicalORExpressionNoIn "||" LogicalANDExpressionNoIn : ConditionalExpression LogicalORExpression LogicalORExpression "?" AssignmentExpression ":" AssignmentExpression : ConditionalExpressionNoIn LogicalORExpressionNoIn LogicalORExpressionNoIn "?" AssignmentExpressionNoIn ":" AssignmentExpressionNoIn : AssignmentExpression ConditionalExpression LeftHandSideExpression AssignmentOperator AssignmentExpression : AssignmentExpressionNoIn ConditionalExpressionNoIn LeftHandSideExpression AssignmentOperator AssignmentExpressionNoIn : AssignmentOperator any{"=" "*=" "/=" "%=" "+=" "-=" "<<=" ">>=" ">>>=" "&=" "^=" "|="} : Expression AssignmentExpression Expression "," AssignmentExpression

A4 Statements

: ExpressionNoIn AssignmentExpressionNoIn ExpressionNoIn "," AssignmentExpressionNoIn : Statement Block VariableStatement EmptyStatement ExpressionStatement IfStatement IterationStatement ContinueStatement BreakStatement ReturnStatement WithStatement LabelledStatement SwitchStatement ThrowStatement TryStatement DebuggerStatement : Block "{" optional{StatementList} "}" : StatementList Statement StatementList Statement : VariableStatement "var" VariableDeclarationList ";" : VariableDeclarationList VariableDeclaration VariableDeclarationList "," VariableDeclaration : VariableDeclarationListNoIn VariableDeclarationNoIn VariableDeclarationListNoIn "," VariableDeclarationNoIn : VariableDeclaration Identifier optional{Initialiser} : VariableDeclarationNoIn Identifier optional{InitialiserNoIn} : Initialiser "=" AssignmentExpression : InitialiserNoIn "=" AssignmentExpressionNoIn : EmptyStatement ";" : ExpressionStatement no{any{"{" "function"}}here Expression ";" : IfStatement "if" "(" Expression ")" Statement "else" Statement "if" "(" Expression ")" Statement : IterationStatement "do" Statement "while" "(" Expression ")" ";" "while" "(" Expression ")" Statement "for" "(" optional{ExpressionNoIn} ";" optional{Expression} ";" optional{Expression} ")" Statement "for" "(" "var" VariableDeclarationListNoIn ";" optional{Expression} ";" optional{Expression} ")" Statement "for" "(" LeftHandSideExpression "in" Expression ")" Statement "for" "(" "var" VariableDeclarationNoIn "in" Expression ")" Statement : ContinueStatement "continue" no{LineTerminator}here optional{Identifier} ";" "continue" LineTerminator ";" : BreakStatement "break" no{LineTerminator}here optional{Identifier} ";" "break" LineTerminator ";" : ReturnStatement "return" no{LineTerminator}here optional{Expression} ";" "return" LineTerminator ";" : WithStatement "with" "(" Expression ")" Statement : SwitchStatement "switch" "(" Expression ")" CaseBlock : CaseBlock "{" optional{CaseClauses} "}" "{" optional{CaseClauses} DefaultClause optional{CaseClauses} "}" : CaseClauses CaseClause CaseClauses CaseClause : CaseClause "case" Expression ":" optional{StatementList} : DefaultClause "default" ":" optional{StatementList} : LabelledStatement Identifier ":" Statement : ThrowStatement "throw" no{LineTerminator}here Expression ";" : TryStatement "try" Block Catch "try" Block Finally "try" Block Catch Finally : Catch "catch" "(" Identifier ")" Block : Finally "finally" Block

A5 Functions and Programs

: DebuggerStatement "debugger" ";" : FunctionDeclaration "function" Identifier "(" optional{FormalParameterList} ")" "{" optional{FunctionBody} "}" : FunctionExpression "function" optional{Identifier} "(" optional{FormalParameterList} ")" "{" optional{FunctionBody} "}" : FormalParameterList Identifier FormalParameterList "," Identifier : FunctionBody optional{SourceElements} : Program optional{SourceElements} : SourceElements SourceElement SourceElements SourceElement

A6 Universal Resource Identifier Character Classes

: SourceElement Statement FunctionDeclaration : uri optional{uriCharacters} : uriCharacters uriCharacter optional{uriCharacters} : uriCharacter uriReserved uriUnescaped uriEscaped : uriReserved any{";" "/" "?" ":" "@" "&" "=" "+" "$" ","} : uriUnescaped uriAlpha DecimalDigit uriMark : uriEscaped "%" HexDigit HexDigit : uriAlpha any{"a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"}

A7 Regular Expressions

: uriMark any{"-" "_" "." "!" "~" "*" "" "(" ")"} : Pattern Disjunction : Disjunction Alternative Alternative "|" Disjunction : Alternative "[empty]" Alternative Term : Term Assertion Atom Atom Quantifier : Assertion "^" "$" "\" "b" "\" "B" "(" "?=" Disjunction ")" "(" "?!" Disjunction ")" : Quantifier QuantifierPrefix QuantifierPrefix "?" : QuantifierPrefix "*" "+" "?" "{" DecimalDigits "}" "{" DecimalDigits "," "}" "{" DecimalDigits "," DecimalDigits "}" : Atom PatternCharacter "." "\" AtomEscape CharacterClass "(" Disjunction ")" "(" "?" ":" Disjunction ")" : PatternCharacter SourceCharacter except{any{"^" "$" "\" "." "*" "+" "?" "(" ")" "[" "]" "{" "}" "|"}} : AtomEscape DecimalEscape CharacterEscape CharacterClassEscape : CharacterEscape ControlEscape "c" ControlLetter HexEscapeSequence UnicodeEscapeSequence IdentityEscape : ControlEscape any{"f" "n" "r" "t" "v"} : ControlLetter any{"a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"} : IdentityEscape SourceCharacter except{any{IdentifierPart ZWJ ZWNJ}} : DecimalEscape DecimalIntegerLiteral no{DecimalDigit}here : CharacterClassEscape any{"d" "D" "s" "S" "w" "W"} : CharacterClass // meh. the ^ is optional. "[" no{"^"}here ClassRanges "]" "[" "^" ClassRanges "]" : ClassRanges "[empty]" NonemptyClassRanges : NonemptyClassRanges ClassAtom ClassAtom NonemptyClassRangesNoDash ClassAtom "" ClassAtom ClassRanges : NonemptyClassRangesNoDash ClassAtom ClassAtomNoDash NonemptyClassRangesNoDash ClassAtomNoDash "" ClassAtom ClassRanges : ClassAtom "-" ClassAtomNoDash : ClassAtomNoDash SourceCharacter except{any{"\" "]" "-"}} "\" ClassEscape

A81 JSON Lexical Grammar

: ClassEscape DecimalEscape "b" CharacterEscape CharacterClassEscape : JSONWhiteSpace TAB CR LF SP : JSONString """ optional{JSONStringCharacters} """ : JSONStringCharacters JSONStringCharacter optional{JSONStringCharacters} : JSONStringCharacter SourceCharacter except{any{""" "\" '0000' '0001' '0002' '0003' '0004' '0005' '0006' '0007' '0008' '0009' '000A' '000B' '000C' '000D' '000E' '000F' '0010' '0011' '0012' '0013' '0014' '0015' '0016' '0017' '0018' '0019' '001A' '001B' '001C' '001D' '001E' '001F'}} "\" JSONEscapeSequence : JSONEscapeSequence JSONEscapeCharacter UnicodeEscapeSequence : JSONEscapeCharacter any{""" "/" "\" "b" "f" "n" "r" "t"} : JSONNumber optional{"-"} DecimalIntegerLiteral optional{JSONFraction} optional{ExponentPart} : JSONFraction "." DecimalDigits : JSONNullLiteral NullLiteral

A82 JSON Syntactic Grammar

: JSONBooleanLiteral BooleanLiteral : JSONText JSONValue : JSONValue JSONNullLiteral JSONBooleanLiteral JSONObject JSONArray JSONString JSONNumber : JSONObject "{" "}" "{" JSONMemberList "}" : JSONMember JSONString ":" JSONValue : JSONMemberList JSONMember JSONMemberList "," JSONMember : JSONArray "[" "]" "[" JSONElementList "]" : JSONElementList JSONValue JSONElementList "," JSONValue