[erlang-questions] BNF/EBNF Grammar for Erlang

Joe Armstrong erlang@REDACTED
Fri Nov 11 21:38:12 CET 2011


I don't think there are any BNF/EBNF grammars for erlang
there might be grammars for subsets of the language but not the
entire language.  The problem with NBF/EBNF/PEG grammars
is that decent error reporting is very difficult. In practice
virtual all languages use hand written recursive descent parsers
or LALR(1) parsers for which acceptable error recovery strategies exist.

The official grammar is: (official means this is the actual grammar used by
the erlang compiler and all tools)

https://github.com/erlang/otp/blob/master/lib/stdlib/src/erl_parse.yrl

It you look at the productions they are very similar to yacc
productions:

For example a tuple is defined like this (line 330,331)

tuple -> '{' '}'       : {tuple,?line('$1'),[]}.
tuple -> '{' exprs '}' : {tuple,?line('$1'),'$2'}.

If you forget about the stuff after the ':' this
reads

   tuple -> '{' '}'
   tuple -> {' exprs '}'

ie a tuple is either {} or { exprs }

The part after the ':' define the parse tree that
is returned if the expression is recognised.

A production like:

a -> b c d : {something, '$2'}

means is we match an 'a' then the parse
tree we want returned is {something, '$2'}

'$1' is the parse tree of b, '$2' is the parse tree
of c etc.

and exprs (line 445/446) is defined

exprs -> expr           : ['$1'].
exprs -> expr ',' exprs : ['$1' | '$3'].


ie exprs is an expr or a comma separated
sequence of expr's

In the original yacc this would be written
something like

exprs: expr {$$ = $1}
| expr ',' exprs {$$ = [$1|$3]}

The yecc manual is at http://www.erlang.org/doc/man/yecc.html

the command

> erlc erlang.yecc

compiles the grammar into a beam file

Cheers

/Joe




On Fri, Nov 11, 2011 at 5:08 PM, Ryan Molden <ryanmolden@REDACTED> wrote:

> Howdy fellow Erlangeers.  I was wondering if anyone knew of a BNF/EBNF
> grammar for Erlang?  Joe Armstrong pointed me at the YRL grammar at github;
> unfortunately, I am YRL illiterate so it looked mostly like gibberish to me.
>
> Ryan
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111111/cc56aa7a/attachment.htm>


More information about the erlang-questions mailing list