Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Compiler Tools JavaCC > Re: Single prod...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 6 Topic 481 of 559
Post > Topic >>

Re: Single production vs recursive grammar structure

by AC <user@[EMAIL PROTECTED] > Oct 11, 2007 at 08:23 AM

As you described, operator precedence is implemented with operator
production A calling the next more higher precedence operator
production B, followed optionally by the operator symbol and the next
higher precedence operator production B again.

It is not possible for a test to call the operator production A and
force its optional branch to be taken.

   (regarding (1): you can test a single production, but cannot force an
   optional branch inside the production to be taken.)

   (regarding (3): rewriting the grammar to separate the two cases
   would make the grammar slower and maybe harder to understand.
   For example

     void RelationalExpression() : { } {
       AdditiveExpression() [ <RELATIONAL_OP> AdditiveExpression() ]
     }

   could become something like

     void RelationalExpression() : { } {
       LOOKAHEAD(AdditiveExpression() <RELATIONAL_OP>)
       RelationalExpression2()
       |
       AdditiveExpression()
     }
     void RelationalExpression2() : { } {
       AdditiveExpression() <RELATIONAL_OP> AdditiveExpression()
     }

   This would allow tests to call RelationalExpression2 to require the
   <RELATIONAL_OP> and 2nd parameter, but it would also mean that before
   the parser can decide which branch to take, it has to do extra work to
   look ahead past the entire additive expression [which could be a
   complicated expression with many terms].)

What you can do is check the effects of calling the production.

The simplest check is to check what tokens were consumed.

   (regarding (2): one way is to collect the tokens that were consumed,
    though that may require modifying the parser if it does not already
    to this.

    Another way might be to design the tests so a unique next token
    remains after the test case input is parsed.  For example on input
    "a+b<c", after parsing AdditiveExpression a+b, the next token
    should be "<".)

(For parsers that create a parse tree [such as using JJTree], the test
could check the type of the created parse node to see if it was a
RelationalExpressionNode or an AdditiveExpressionNode, etc.)

(If you think the productions are confusingly named since a
RelationalExpression does not always consume a relational operator,
think of them as shorthand for something longer like
  RelationalExpression abbreviates RelationalOrHigherPrecedenceExpression
  AdditiveExpression abbreviates AdditiveOrHigherPrecedenceExpression
)

Hope this helps.
 




 6 Posts in Topic:
Single production vs recursive grammar structure
Cesare Zecca <Cesare.Z  2007-10-09 14:38:03 
Re: Single production vs recursive grammar structure
AC <user@[EMAIL PROTEC  2007-10-11 08:23:51 
Re: Single production vs recursive grammar structure
AC <user@[EMAIL PROTEC  2007-10-12 08:27:05 
Re: Single production vs recursive grammar structure
Esmond Pitt <esmond.pi  2007-10-12 03:13:47 
Re: Single production vs recursive grammar structure
AC <user@[EMAIL PROTEC  2007-10-12 08:31:11 
Re: Single production vs recursive grammar structure
Cesare Zecca <Cesare.Z  2007-10-12 14:48:05 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Nov 22 8:27:55 CST 2008.