Ryan
From: Max Lapshin
Sent: 11/11/2011 9:03 AM
To: Ryan Molden
Cc: erlang-q...@erlang.org
Subject: Re: [erlang-questions] BNF/EBNF Grammar for Erlang
On Nov 11, 2011 10:38 AM, "Ryan Molden" <ryanm...@gmail.com> wrote:
>
> Not clear how that helps, is there a grammar in there somewhere I am
> missing? I looked around a little on the github page for it with no
> success.
It uses standard PEG, like EBNF/BNF, but not ambiguous like those are, check both the range in the root directory, and check the extra directory for samples. As for PEG, even Wikipedia has a good detailed description, but if you know EBNF, you basically already know it except for one or two operators.
Hi,
You could look at
http://cedet.bzr.sourceforge.net/bzr/cedet/code/trunk/annotate/head%3A/semantic/bovine/erlang.by,
but it seems to not have been updated from 2005 so it's probably only
useful as a starting point.
regards,
Vlad
I don't quite grasp what you're looking for. If you can't understand
https://github.com/erlang/otp/blob/master/lib/stdlib/src/erl_parse.yrl,
(which is about as clear as it can get) how can you hope to understand
*any* complete BNF grammar for Erlang?
/Richard
tuple -> '{' '}' : {tuple,?line('$1'),[]}.tuple -> '{' exprs '}' : {tuple,?line('$1'),'$2'}.If you forget about the stuff after the ':' thisreadstuple -> '{' '}'tuple -> {' exprs '}'
ie a tuple is either {} or { exprs }The part after the ':' define the parse tree thatis returned if the expression is recognised.A production like:a -> b c d : {something, '$2'}means is we match an 'a' then the parsetree we want returned is {something, '$2'}'$1' is the parse tree of b, '$2' is the parse treeof c etc.and exprs (line 445/446) is definedexprs -> expr : ['$1'].exprs -> expr ',' exprs : ['$1' | '$3'].ie exprs is an expr or a comma separatedsequence of expr'sIn the original yacc this would be writtensomething likeexprs: expr {$$ = $1}| expr ',' exprs {$$ = [$1|$3]}The yecc manual is at http://www.erlang.org/doc/man/yecc.htmlthe command> erlc erlang.yecccompiles the grammar into a beam fileCheers/Joe
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
case_expr -> 'case' expr 'of' cr_clauses 'end' :{'case',?line('$1'),'$2','$4'}.
The last two are used in the language of type declarations that Erlang
has. The first is used to declare integer ranges, as in:
-type small_int() :: 1..42.
and the second one to declare non-empty lists of some type, as in:
-type sil() :: [small_int(),...].
Kostis
The ':-' rules are something I've never seen in the wild. I think they
were part of the ancient and obsolete Mnemosyne syntax for making Mnesia
lookups. Nowadays, QLCs are used instead. You can safely assume that you
can ignore everything to do with the rules syntax.
/Richard
> I'll assume that wasn't meant to be as condescending as it came off to me.
>
> >which is about as clear as it can get
>
> Yeah, something about
> binary_type -> '<<' '>>' : {type, ?line('$1'),binary,
> [abstract(0, ?line('$1')),
> abstract(0, ?line('$1'))]}.
>
> binary_type -> '<<' bin_base_type '>>' : {type, ?line('$1'),binary,
> ['$2', abstract(0, ?line('$1'))]}.
Read what's before the colon:
binary_type -> '<<' '>>'.
binary_type -> '<<' bin_base_type '>>'.
This *is* BNF, up to punctuation.
I've attached a copy of the R12B-5 grammar with the "what to build"
part stripped out. This really is about as clear as it can get.
I also processed the YRL file to just delete all the stuff to the right
of the :, but thanks for the attachment anyhow.
Ryan
From: Richard O'Keefe
Sent: 11/13/2011 4:45 PM
To: Ryan Molden
Cc: Richard Carlsson; erlang-q...@erlang.org
Subject: Re: [erlang-questions] BNF/EBNF Grammar for Erlang
On 12/11/2011, at 9:55 AM, Ryan Molden wrote:
> Yep, it is much more clear ignoring the RHS of the :, I just didn't
> realize that bit was meaningless to me and am not in the general habit
> of ignoring vast swaths of what appears in the grammar file.
Well, any grammar that's actually *used* as the front end of something
interesting, whether it's a Prolog DCG, or Yacc, or ML-Yacc, or Yecc,
or whatever is going to have *both* pure syntax *and* some semantics
in there. Here's one of the examples from the GENTLE kit:
'root' expression(-> X) print(X)
'nonterm' expression(-> INT)
'rule' expression(-> X ): expr2(-> X)
'rule' expression(-> X+Y): expression(-> X) "+" expr2(-> Y)
'rule' expression(-> X-Y): expression(-> X) "-" expr2(-> Y)
'nonterm' expr2(-> INT)
'rule' expr2(-> X ): expr3(-> X)
'rule' expr2(-> X*Y): expr2(-> X) "*" expr3(-> Y)
'rule' expr2(-> X/Y): expr2(-> X) "/" expr3(-> Y)
'nonterm' expr3(-> INT)
'rule' expr3(-> X ): Number(-> X)
'rule' expr3(-> - X): "-" expr3(-> X)
'rule' expr3(-> + X): "+" expr3(-> X)
'rule' expr3(-> X): "(" expression(-> X) ")"
'token' Number(-> INT)
If you ignore the (-> ...) parts, you have yet another minor variant
of BNF. The (-> ...) parts give you the semantics. Utterly commonplace.
Well, any grammar that's actually *used* as the front end of something
interesting, whether it's a Prolog DCG, or Yacc, or ML-Yacc, or Yecc,
or whatever is going to have *both* pure syntax *and* some semantics
in there. Here's one of the examples from the GENTLE kit:
'root' expression(-> X) print(X)
'nonterm' expression(-> INT)
'rule' expression(-> X ): expr2(-> X)
'rule' expression(-> X+Y): expression(-> X) "+" expr2(-> Y)
'rule' expression(-> X-Y): expression(-> X) "-" expr2(-> Y)
'nonterm' expr2(-> INT)
'rule' expr2(-> X ): expr3(-> X)
'rule' expr2(-> X*Y): expr2(-> X) "*" expr3(-> Y)
'rule' expr2(-> X/Y): expr2(-> X) "/" expr3(-> Y)
'nonterm' expr3(-> INT)
'rule' expr3(-> X ): Number(-> X)
'rule' expr3(-> - X): "-" expr3(-> X)
'rule' expr3(-> + X): "+" expr3(-> X)
'rule' expr3(-> X): "(" expression(-> X) ")"
'token' Number(-> INT)
If you ignore the (-> ...) parts, you have yet another minor variant
of BNF. The (-> ...) parts give you the semantics. Utterly commonplace.