Skip to content

Superfluous rule needed to make parser work (4.7.2, Java target) #2689

Description

@andreasabel

The parser generated from the following grammar fails, unless I activate the commented-out superfluous rule:

parser grammar RightRecParser;
options { tokenVocab = RightRecLexer; }

listInteger returns [ int result ]
  : /* empty */
      { $result = 0; }

  | n=INTEGER m=listInteger
      { $result = Integer.parseInt($n.getText()) + $m.result; }
;

// // The following irrelevant rule makes the parser succeed:
// foo returns [ int result ]
//   : m=listInteger
//       { $result = $m.result; }
// ;

Behavior without foo:

echo "1 2" | java -cp .:/Users/abel/bnfc/testing/data/antlr-4.7.2-complete.jar RightRec.Test
line 1:0 no viable alternative at input '1'

Behavior with foo:

echo "1 2" | java -cp .:/Users/abel/bnfc/testing/data/antlr-4.7.2-complete.jar RightRec.Test
3

I would expect that an unused rule is not needed to generate a working parser. These grammars are originally auto-generated by the BNFC tool, --antlr backend. Adding redundant rules by hand is thus not a viable workaround.

Lexer is unsurprisingly:

lexer grammar RightRecLexer;

// Whitespace
WS             : (' ' | '\r' | '\t' | '\n' | '\f')+ ->  skip;

// Numbers
fragment DIGIT : [0-9] ;
INTEGER        : DIGIT+;

The full MWE is attached (including Makefile and Test wrapper).
bug.zip

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions