antlr / antlr/antlr4

Using explicit rule for empty production seems to cause parse fail in some cases.

Open
#3,196 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
19k
Forks
3.5k
PR merge metrics
No merged PRs in 30d

Description

Mostly copied from .

(github won't allow preview or text alterations (markdown?) so slight cleanup and hope for the best)

Grammar is at end. It's hugely minimised from an original sql-like language.

Essentially a 'select' statement takes an optional scaffold part,
followed by an optional 'with' statement,
followed by the 'select' statement itself.
These have been reduced down to single lexemes here for simplicity.

Optionality is done with an "empty_production" rule, which I prefer the explicitness of over just '... | ;'. It should not matter.

So with this grammar, with this:

scaffold
select
;

It parses ok, but comment out the 'scaffold':

--scaffold
select
;

and you get:

line 2:0 mismatched input 'select' expecting {'with', 'scaffold'}
Stack overflow.

Scaffold is optional, so why the problem?

Odder yet (to me), if you add a statement with a scaffold above that statement without a scaffold (which failed previously), it then succeeds overall:

scaffold
select
;

--scaffold <<< this failed before
select
;

Which parses ok.

From playing about it seems the optionality of the 'with' statement interferes with the optionality of the 'scaffold' statement, but that's just an impression.
AIUI they should not interfere because there's no actual ambiguity in the grammar.

I'm using the AntlrVSIX plugin for visual studio, version 8.3, which reports the Antlr parser version as 4.9.

grammar LDB;

start_parse returns [LDBitems ldbis] :
siX = ldb_items
EOF
;

ldb_items :
(
sisX += select_statement
SEMICOLON
) +
;

select_statement :
sns = opt_set_name_scaffold
wctec = opt_with_CTEs_clause
qe = query_expression
;

opt_set_name_scaffold :
SCAFFOLD
|
empty_production
;

opt_with_CTEs_clause :
WITH
|
empty_production
;

query_expression :
SELECT
;

empty_production : ;

SELECT : 'select' ;
WITH : 'with' ;
SCAFFOLD : 'scaffold' ;

SEMICOLON : ';' ;

fragment WUnl : ( '\r' ? ) '\n' ;

SLCOMMENT : ( '--' .*? WUnl ) -> skip ;

fragment ALLWSes : [ \t\r\n]+ ;

SKIPWS : ALLWSes -> skip ;

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the reported parse failure with the grammar included in the issue and the stated ANTLR 4.9 environment. Compare inputs with and without the scaffold statement, then determine the parser behavior that should be corrected; done means the optional grammar cases parse without the reported mismatch or stack overflow.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.