Repository navigation
How to use variable 'a' when keyword 'a' exists in case-insensitive ANTLR4 grammar? #4930
Replies: 3 comments
|
ANTLR operates in 2 passes: lexer then parser. The lexer pass is not contextual i.e. it doesn't know for which parser rule it is producing tokens. Given the lexer rules in your grammar, input bouclePour : POUR IDENTIFIANT DE expression finDeBoucle; |
|
The usual way languages use keywords as identifiers with Antlr4 is to just declare so: This grammar parses your input as is, but you should go through and verify the parse tree is correct. Some of the other changes I made:
|
I didn't studied your description deeply, but you can disable/enable options { caseInsensitive=true; }
...
A_UPPER options { caseInsensitive=false; } : 'A'; // Matches strictly 'A'
A_LOWER options { caseInsensitive=false; } : 'a'; // Matches strictly 'a'
B : 'b'; // Matches 'b' or 'B'It's described in ANTLR doc. |
Uh oh!
There was an error while loading. Please reload this page.
I'm writing a French educational algorithm language with ANTLR4. The grammar needs to be case-insensitive (
caseInsensitive=true) so students can write "DEBUT", "Debut", or "debut" interchangeably.However, I have a lexical conflict with the single-letter variable
a.Simplified grammar:
Test case that fails:
Error message:
What I've already tried (without success):
'a'directly in the ruleEnvironment:
Question: How can I use
aas a variable name while keepingcaseInsensitive=trueand without breaking the loop syntax?All reactions