[erlang-questions] BNF/EBNF Grammar for Erlang

Richard O'Keefe ok@REDACTED
Tue Nov 15 00:02:59 CET 2011

On 15/11/2011, at 4: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.

More information about the erlang-questions mailing list