Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ANTLR matches incomplete rule #4696

Open
diyan-gochev-at-ft-dot-com opened this issue Sep 15, 2024 · 4 comments
Open

ANTLR matches incomplete rule #4696

diyan-gochev-at-ft-dot-com opened this issue Sep 15, 2024 · 4 comments

Comments

@diyan-gochev-at-ft-dot-com

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?

@diyan-gochev-at-ft-dot-com diyan-gochev-at-ft-dot-com changed the title ANTLR matches ANTLR matches incomplete rule Sep 15, 2024
@kaby76
Copy link
Contributor

kaby76 commented Sep 15, 2024

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
Copy link
Author

diyan-gochev-at-ft-dot-com commented Sep 18, 2024

@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
Copy link
Author

diyan-gochev-at-ft-dot-com commented Sep 18, 2024

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

@cardillan
Copy link

I'm not an ANTLR developer, so maybe take this with a grain of salt, but:

  • This isn't a minimum reproducible example. What is needed is an entire grammar, simplified as much as possible while still displaying the problematic behavior, so that someone could run it and see for themselves.
  • I might be mistaken, but I'm not sure the logs prove the functionsReturningNumerics rule has succeeded. When the input can't be matched, the parser runs in an error recovery mode - it tries to make sense of the input to be able to report other errors. Here it appears it assumed ( instead of , to be able to continue parsing (judging from the error message produced).
  • From the information you've shared I don't see how the pathExpression rule could be matched. It uses . (dots) as separators, while your input uses , (commas) as separators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants