[antlr-interest] Specifying optional elements in a tree grammar.

Paul McKee paul.mckee at mckeetech.com
Wed Mar 25 10:22:13 PDT 2009


Thanks very much Jim - that's sorted it!

Regards

Paul
  ----- Original Message ----- 
  From: Jim Idle 
  Cc: antlr-interest at antlr.org 
  Sent: Wednesday, March 25, 2009 5:12 PM
  Subject: Re: [antlr-interest] Specifying optional elements in a tree grammar.


  Paul McKee wrote: 
    Hi:

    How do I go about specifying optional elements in a tree grammar?
    For example, in my AST generator, I have:

    querySpecification :
     selectClause 
     ( intoClause ) ?
     ( fromClause  )?
     ( (whereClause) =>whereClause )?
     ( (groupByClause) => groupByClause 
      ( havingClause  )?
     )? ->^(SELECTCLAUSE selectClause ^(INTOCLAUSE intoClause)? ^(FROMCLAUSE fromClause)? ^(WHERECLAUSE whereClause)?);

    Which generates fine, but in my tree walker, I have:

    querySpecification :
     ^(SELECTCLAUSE selectClause ^(INTOCLAUSE intoClause)? ^(FROMCLAUSE fromClause)? ^(WHERECLAUSE whereClause)?)
     ;

    Which causes the following:
    [16:57:30] error(100): TsqlWalker.g:284:54: syntax error: antlr: TsqlWalker.g:284:54: expecting RPAREN, found '?'
    [16:57:30] error(100): TsqlWalker.g:284:80: syntax error: antlr: TsqlWalker.g:284:80: unexpected token: ?
    [16:57:30] error(100): TsqlWalker.g:284:107: syntax error: antlr: TsqlWalker.g:284:107: expecting SEMI, found ')'

  Probaly better to put those embedded trees into separate rules  rulex? ruley? but you need:

   ^(SELECTCLAUSE selectClause (^(INTOCLAUSE intoClause))? (^(FROMCLAUSE fromClause))? (^(WHERECLAUSE whereClause))?)


  Because the ^( xxxxx) is a tree specification and needs to be enclosed to make it optional.

  Jim


  __________ Information from ESET NOD32 Antivirus, version of virus signature database 3962 (20090325) __________

  The message was checked by ESET NOD32 Antivirus.

  http://www.eset.com



------------------------------------------------------------------------------



  List: http://www.antlr.org/mailman/listinfo/antlr-interest
  Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address



  __________ Information from ESET NOD32 Antivirus, version of virus signature database 3962 (20090325) __________

  The message was checked by ESET NOD32 Antivirus.

  http://www.eset.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090325/0dc0975e/attachment.html 


More information about the antlr-interest mailing list