the + in the +2 is being treated as the SIGN for the SCIENTIFIC_NUMBER.
be sure to dump the Token stream between your lexing and parsing phases in order to check that you are recognizing the tokens in the manner you desire.
i also think SCIENTIFIC_NUMBER has an issue with the exponent portion of the number. both the NUMBER and UNSIGNED_INTEGER fragments will recognize the same whole number input, ANTLR will disambiguate this collision by choosing the first matching rule, e.g. NUMBER.
i have not tried this but maybe changing NUMBER to be something like:
NUMBER: UNSIGNED_INTEGER ('.' UNSIGNED_INTEGER)? ;
(so that the digit range only appears in a single rule....)
Hope this helps
-jbb
I took this grammar from the web and modified it slightly.Attached is the version I have now.It's supposed to be a simple grammar for arithmetic expressions and equalities/inequalities.
I am using the JavaScript ANTLR v4 runtime.
antlr-javascript-runtime-4.7.2.zip
Now... I am running into something curious.
(1+2)^3 is NOT recognized as an expressionbut(1 + 2)^3 is recognized as an expressionIn the grammar I do have this lexer rule:
WS: [ \r\n\t] + -> skip;
So why does it behave differently on the 2 inputs?I have no idea currently why...The only difference between them are the whitespaces.
Any help would be much appreciated.
--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/antlr-discussion/09ee31ba-4fbe-4fc3-aea2-d1bab3da13d7%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-di...@googlegroups.com.
OK... So you think the problem is in the grammar?
But that does not explain why the spaces in the input fix the issue, or does it...?
When you look closer at the last parse tree you will see that there's never a plus or minus token. All +/- are matched as sign and it looks as if only the unary op was matched (which is not really the case, but it looks so)
When you look closer at the last parse tree you will see that there's never a plus or minus token. All +/- are matched as sign and it looks as if only the unary op was matched (which is not really the case, but it looks so)I just realized it doesn't only look so, but indeed is that case. The binary op alt uses `PLUS` and `MINUS` and since these tokens are not created (due to the dominant `SIGN` rule) this alt can never match.
I was able to install your VS Code plugin and play with the grammar and the trees produced on various inputs.You should have pointed me to this plugin, I just found it by chance :)
I still don't get this part though which you wrote here...
"It does. Look at the SCINTIFIC_NUMBER definition. It includes a sign. This conflicts with the PLUS/MINUS rules. ANTLR4 chooses the rule which matches the longest input sequence. Hence a `+2` matches the SCINTIFIC_NUMBER rule (which does not include optional whitespaces between + and 2, hence this rule is only used if there is no whitespace). But if you use a space between + and 2 then the plus is matched as individual token (because SCINTIFIC_NUMBER does not match in this case). Hence with a space you get two tokens (one for the plus and one for the digit)."
I don't get the whitespaces part.Where does the grammar say that in SCIENTIFIC_NUMBER there are no spaces?
Where does it deal with spaces really?Or... are spaces assumed as some default separators regardless of what the grammar says about them.My idea of this is wrong it seems... I thought before lexing/parsing all spaces are removed due to this rule
WS: [ \r\n\t] + -> skip;
and only then ANTLR does lexing i.e. tokenizing and finally parsing.But apparently that's not how it works.Could you elaborate on this a bit? The rest I think I understand in full now.Once again thanks a lot.Regards,PeterOn Mon, Dec 30, 2019 at 10:11 AM Peter Petrov <p.a.p...@gmail.com> wrote:I can't say how much I thank you for this detailed investigation which you did for all the advices you provided.Apparently you did many edits and tried many things.I think I follow most of your notes, I will reread them 2-3 more times.Still... in the last version which you have of the grammar, what happens with the SIGN?You said it's not needed. But what do we put in the SCIENTIFIC_NUMBER definition?Just (PLUS | MINUS)?Could you send me the last version which you have of the g4 file, just want to visually compare it with mine to better understand the final version?Many thanks once again!Peter
<parsetree5.png>
--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/antlr-discussion/84AEADD6-1F5A-4ADE-8030-E4CD76540955%40googlemail.com.
I can't say how much I thank you for this detailed investigation which you did for all the advices you provided.Apparently you did many edits and tried many things.I think I follow most of your notes, I will reread them 2-3 more times.Still... in the last version which you have of the grammar, what happens with the SIGN?You said it's not needed. But what do we put in the SCIENTIFIC_NUMBER definition?Just (PLUS | MINUS)?Could you send me the last version which you have of the g4 file, just want to visually compare it with mine to better understand the final version?Many thanks once again!Peter
On Thu, Dec 26, 2019 at 6:48 AM 'Mike Lischke' via antlr-discussion <antlr-di...@googlegroups.com> wrote:
--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/antlr-discussion/84AEADD6-1F5A-4ADE-8030-E4CD76540955%40googlemail.com.
--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/antlr-discussion/84AEADD6-1F5A-4ADE-8030-E4CD76540955%40googlemail.com.