antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17.28k stars 3.3k forks source link

ANTLR matches incomplete rule #4696

Open diyan-gochev-at-ft-dot-com opened 2 months ago

diyan-gochev-at-ft-dot-com commented 2 months ago

I am using ANTLR 4.13.2

functionsReturningNumerics
    : 'LENGTH' '(' stringPrimary ')'

........

passing just 'LENGTH' without parentheses, ANTLR matches the expression. Is it an expected behaviour or it's bug?

kaby76 commented 2 months ago

It could be as designed. But, we need to actually see a minimum reproducible example (grammar and input). Check whether your start rule is a proper EOF-terminated rule. It should be.

diyan-gochev-at-ft-dot-com commented 2 months ago

@kaby76 Thank you for your feedback. The case is very simple, I have 2 rules:

pathExpression
    : IDENTIFICATION_VARIABLE ('.' IDENTIFICATION_VARIABLE)*
    ;

AND

functionsReturningNumerics
    : 'LENGTH' '(' stringPrimary ')'
    | 'LOCATE' '(' ....

with options:

options { caseInsensitive = true; }

The string to parse (the subject) is the string:

 id,name,type,palletType,length,width,height

(does not contain parentheses at all).

I expect functionsReturningNumerics rule to fail to match because of the missing (but required parentheses) and continue trying to match the other rules, and eventually match "pathExpression", but instead, ANTLR logs

line 1:30 mismatched input ',' expecting '(' 

and even though the parentheses are missing, the parser successfully matches functionsReturningNumerics. Here is the log, showing that "functionsReturningNumerics" rule succeeds:

2024-09-18T04:38:46.974+03:00  INFO 62114 --- [nio-8080-exec-2] c.etradesoft.aql.parser.AQLListenerImpl  : visitTerminal(,)
2024-09-18T04:38:46.974+03:00  INFO 62114 --- [nio-8080-exec-2] c.etradesoft.aql.parser.AQLListenerImpl  : visitTerminal(length)
2024-09-18T04:38:46.974+03:00  INFO 62114 --- [nio-8080-exec-2] c.etradesoft.aql.parser.AQLListenerImpl  : exitFunctionsReturningNumerics(length)
2024-09-18T04:38:46.974+03:00  INFO 62114 --- [nio-8080-exec-2] c.etradesoft.aql.parser.AQLListenerImpl  : exitArithmeticPrimary(length)
2024-09-18T04:38:46.974+03:00  INFO 62114 --- [nio-8080-exec-2] c.etradesoft.aql.parser.AQLListenerImpl  : exitArithmeticFactor(length)
2024-09-18T04:38:46.974+03:00  INFO 62114 --- [nio-8080-exec-2] c.etradesoft.aql.parser.AQLListenerImpl  : exitArithmeticTerm(length)
2024-09-18T04:38:46.974+03:00  INFO 62114 --- [nio-8080-exec-2] c.etradesoft.aql.parser.AQLListenerImpl  : exitSimpleArithmeticExpression(length)
2024-09-18T04:38:46.974+03:00  INFO 62114 --- [nio-8080-exec-2] c.etradesoft.aql.parser.AQLListenerImpl  : exitArithmeticExpression(length)
2024-09-18T04:38:46.974+03:00  INFO 62114 --- [nio-8080-exec-2] c.etradesoft.aql.parser.AQLListenerImpl  : exitExpression(length)
2024-09-18T04:38:46.974+03:00  INFO 62114 --- [nio-8080-exec-2] c.etradesoft.aql.parser.AQLListenerImpl  : exitSelectExpressionWithAlias(length)
2024-09-18T04:38:46.974+03:00  INFO 62114 --- [nio-8080-exec-2] c.etradesoft.aql.parser.AQLListenerImpl  : visitTerminal(,)

The problem is that functionsReturningNumerics rule succeeds without the required parentheses.

diyan-gochev-at-ft-dot-com commented 2 months ago

The comment has been updated, the logs provided. Looks like a bug.