You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
public class ExpressionValidator {
public static void main(String[] args) throws Exception {
String input = "a(uptime,sum)";
CharStream charStream = CharStreams.fromString(input);
MQELexer lexer = new MQELexer(charStream);
lexer.addErrorListener(new ParseErrorListener());
CommonTokenStream tokens = new CommonTokenStream(lexer);
MQEParser parser = new MQEParser(tokens);
parser.addErrorListener(new ParseErrorListener());
try {
ParseTree tree = parser.expression();
System.out.println("Expression is valid and successfully parsed.");
System.out.println(tree.toStringTree(parser));
} catch (ParseCancellationException e) {
e.printStackTrace();
}
}
I got the ParseCancellationException
line 1:9 mismatched input 'SUM' expecting {',', '-', '+'}
org.antlr.v4.runtime.misc.ParseCancellationException: line 1:9 mismatched input 'SUM' expecting {',', '-', '+'}
at com.example.antlr4demo.util.ParseErrorListener.syntaxError(ParseErrorListener.java:55)
at org.antlr.v4.runtime.ProxyErrorListener.syntaxError(ProxyErrorListener.java:41)
at org.antlr.v4.runtime.Parser.notifyErrorListeners(Parser.java:544)
at org.antlr.v4.runtime.DefaultErrorStrategy.reportInputMismatch(DefaultErrorStrategy.java:327)
at org.antlr.v4.runtime.DefaultErrorStrategy.reportError(DefaultErrorStrategy.java:139)
at com.example.antlr4demo.grammer.demo.DParser.aFunc(DParser.java:841)
at com.example.antlr4demo.grammer.demo.DParser.expression(DParser.java:290)
at com.example.antlr4demo.grammer.demo.DParser.expression(DParser.java:234)
at com.example.antlr4demo.util.DemoExpressionValidator.main(DemoExpressionValidator.java:42)
The error message not about {'max','min'} but about {'+'.'-'}
I found that there is a conflict between the two expressions expression plusSub expression and a L_PAREN expression COMMA aFunc R_PAREN. It will firstly match the expression plusSub expression, but the input string nothing to do with '+' or '-'.
If I remove expression plusSub expression, the error message will be expecting {'max','min'} as expected.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Now, I want to make a rule to match
a(b(c,d))
, andd
is between(max,min)
caseSensitive. So I made a try belowLexer.g4:
Parser.g4:
And I did the test later:
I got the ParseCancellationException
The error message not about {'max','min'} but about {'+'.'-'}
I found that there is a conflict between the two expressions
expression plusSub expression
anda L_PAREN expression COMMA aFunc R_PAREN
. It will firstly match theexpression plusSub expression
, but the input string nothing to do with '+' or '-'.If I remove
expression plusSub expression
, the error message will beexpecting {'max','min'}
as expected.How can I compatible with both checks?
Beta Was this translation helpful? Give feedback.
All reactions