Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does using a character class make this left-recursive?

I have the following to define a basic arithmetic grammar:

grammar Calc;


expression
    : '(' expression ')'                // parenExpression has highest precedence
    | expression ('*' | '/') expression // then multDivExpression
    | expression ('+' | '-') expression // then addSubExpression
    | OPERAND
    ;

// 12 or .12 or 2. or 2.38
OPERAND
    : [0-9]+ ('.' [0-9]*)?
    | '.' [0-9]+
    ;

And it can handle something like:

1+2*3

However, as soon as I change the ('*' | '/') to the character class [*/] I get the following error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

How does adding a character class do this? Or is it because I’m trying to add that to a parser rule and not a lexer rule, or why does that occur?

Update: extracting the character class out of the grammar rule fixes it, but it’d be great to understand why:

expression
    : '(' expression ')'           // parenExpression has highest precedence
    | expression MULDIV expression // then multDivExpression
    | expression ADDSUB expression // then addSubExpression
    | OPERAND
    ;

MULDIV
    : [*/]
    ;

ADDSUB
    : [-+]
    ;

>Solution :

Character classes are only available to Lexer rules.

when you put literals ('+', '/', etc.) implicit Lexer rules are defined for you (and given rather unusable names like T__1, T__2, etc.). So they’re just convenient ways of inserting implicit tokens. (The character class is only available in actual Lexer rules.)

Not sure which version of ANTLR you’re using, but I get a "rule expression has no defined parameters" error, and not mutual left recursion.

I think you’re seeing a side effect of the character class syntax not being available in parser rules. (The [] syntax has an entirely different meaning in parser rules.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading