Replies: 1 comment
-
|
It is not an easy grammar to approach as your first grammar. Don't use 'xxx' in the parser, only define tokens in the lexer and use token names not literals, You don't include the actual grammar so that makes it difficult to infer what else you have wrong. However, I advise that you start with something simpler until you understand how it works; your errors are very fundamental and can easily be fixed by following through some tutorials. I presume that this is an exercise rather than something you actually need? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm building a regular expression parser using ANTLR4, and I'm running into a parsing error related to the quantifier rule.
Here is the relevant part of my grammar:
grammar regex;
regex: term ('|' regex)?;
term: factor+;
factor: base quantifier?;
base: CHAR | group | class_;
group: '(' regex ')';
class_: '[' ('^')? classElem+ ']';
classElem: range | CHAR;
range: CHAR '-' CHAR;
quantifier:
STAR
| PLUS
| QUESTION
| LBRACE NUMBER (COMMA NUMBER?)? RBRACE;
NUMBER: [0-9]+;
CHAR: [a-zA-Z0-9_~@#$%&=<>!;:./];
STAR: *;
PLUS: +;
QUESTION: ?;
LBRACE: {;
RBRACE: };
COMMA: ,;
PIPE: |;
LPAREN: (;
RPAREN: );
LBRACK: [;
RBRACK: ];
DASH: -;
CARET: ^;
WS: whitespace skipped
When I try to parse the input:
a{2,3}
I get the following error:
line 1:3 mismatched input ',' expecting '}' or ','
I also get similar errors when trying a{2,} or other quantifier formats.
I've tried writing the quantifier rule using quoted literals like '{', ',' and '}', and also using lexer tokens like LBRACE, COMMA, RBRACE, but the problem persists.
How can I fix this so that quantifiers in the form {n}, {n,m}, and {n,} are accepted correctly?
Any help would be greatly appreciated!
I’ve tried using literal characters (e.g., '{', ',', '}') and also using lexer tokens like LBRACE, COMMA, etc., but the problem persists.
How can I fix this error so that {n}, {n,m}, and {n,} quantifiers are parsed correctly?
Any help would be greatly appreciated!
Beta Was this translation helpful? Give feedback.
All reactions