[antlr-interest] Implicit Parser Rule Return Types

Randall R Schulz rschulz at sonic.net
Tue Nov 21 10:52:32 PST 2006


Hi,

I am seeing what appears to be inconsistent behavior in the return type
 for the methods corresponding to parser rules.

Specifically, I have these two rules:

-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-

binaryConnective
  : '<=>'
  | '=>'
  | '<='
  | '<~>'
  | '~|'
  | '~&'
  ;


This rule generates the following code:

  // $ANTLR start binaryConnective
  // TSTP.g:518:1: binaryConnective : ('<=>'|'=>'|'<='|'<~>'|'~|'|'~&');
  public void binaryConnective() throws RecognitionException {
    try {
      // TSTP.g:524:2: ( ('<=>'|'=>'|'<='|'<~>'|'~|'|'~&'))
      // TSTP.g:524:4: ('<=>'|'=>'|'<='|'<~>'|'~|'|'~&')
      {
      if ( (input.LA(1)>=45 && input.LA(1)<=50) ) {
        input.consume();

      ...

-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-

name
  : LowerWord
  | SingleQuoted
  | UnsignedInteger
  ;


This generates:

  public static class name_return extends ParserRuleReturnScope {
  };

  // $ANTLR start name
  // TSTP.g:1232:1: name : (LowerWord|SingleQuoted|UnsignedInteger);
  public name_return name() throws RecognitionException {
    name_return retval = new name_return();
    retval.start = input.LT(1);

    try {
      // TSTP.g:1233:2: ( (LowerWord|SingleQuoted|UnsignedInteger))
      // TSTP.g:1233:4: (LowerWord|SingleQuoted|UnsignedInteger)
      {

        ...

-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-


So my question is this: Why does one rule return something when the
other does not even though both of them have terminals as their
right-hand-side?

In an attempt to make this rule more like the "name:" rule, I tried this
replacement for binaryConnective, but it did not help:

binaryConnective
  : BinaryConnective
  ;

BinaryConnective
  : '<=>'
  | '=>'
  | '<='
  | '<~>'
  | '~|'
  | '~&'
  ;


  // $ANTLR start binaryConnective
  // TSTP.g:518:1: binaryConnective : BinaryConnective ;
  public void binaryConnective() throws RecognitionException {
    try {
      // TSTP.g:519:4: ( BinaryConnective )
      // TSTP.g:519:4: BinaryConnective
      {
      match(input,BinaryConnective,FOLLOW_BinaryConnective_in_binaryConnective630);




Randall Schulz



More information about the antlr-interest mailing list