www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D doc BNF question

reply parabolis <parabolis softhome.net> writes:
An oversimplification of the D BNF expression section has 
expressions threaded. Here is a grossly simplified example:
----------------------------------------------------------------
   Expression:
       AddExpression

   AddExpression:
       MulExpression
       AddExpression + MulExpression
       AddExpression - MulExpression

   MulExpression:
       PrimaryExpression
       MulExpression * UnaryExpression
       MulExpression / UnaryExpression

   PrimaryExpression:
       Identifier
       NumericLiteral
----------------------------------------------------------------
The BNF seems to imply:

   All expressions are AddExpressions
   All AddExpressions are MulExpressions
   All MulExpressions are PrimaryExpressions

Which seems counter-intuitive to me. I am wondering why the 
recursive definition is used instead of something like this:
----------------------------------------------------------------
   Expression:
       PrimaryExpression
       MulExpression
       AddExpression

   PrimaryExpression:
       Identifier
       NumericLiteral

   MulExpression:
       Expression * Expression
       Expression / Expression

   AddExpression:
       Expression + Expression
       Expression - Expression
----------------------------------------------------------------
The same threaded style BNF is used in the BNF I found for both 
ANSI C and Java. I would like to understand why the first 
instead of the second. Thanks!

http://acm.zju.edu.cn/maxiao/major/compile/article/yaccforc.htm
http://www.cs.uiowa.edu/~fleck/JavaBNF.htm
Jan 04 2005
parent "Ben Hinkle" <ben.hinkle gmail.com> writes:
 ----------------------------------------------------------------
 The same threaded style BNF is used in the BNF I found for both ANSI C and 
 Java. I would like to understand why the first instead of the second. 
 Thanks!
unambiguous operator precedence. The expression a+b*c must parse as a+(b*c) and not (a+b)*c.
Jan 04 2005