0

I've seen this question asked multiple times, and also seen people "solve" it... but it either confused me or didn't solve my specific situation:

Here's approximately what's going on:

block: statement*;
statement: <bunch of stuff> | expressionStatement;
expression_statement: <more stuff, for example> | method_invoke;
method_invoke: expression LEFT_PAREN <args...> RIGHT_PAREN block;
expression: <bunch of stuff> | expression_statement;

Everything inside of the expression_statement that starts with an expression uses indirect left recursion that I do not know how to fix while still being able to use those syntaxes as statements so they'll be usable in blocks(It is possible to do something like Print("hello world"); On it's own(a statement), but also do something like int c = a + b.getValue() as a part of an expression(an expression)...

How would I handle it differently?

If you need more info please let me know and I'll try my best to provide

1Mangomaster1
  • 346
  • 1
  • 6
  • 16
  • 1
    To fix indirect left recursion, you have to remove the circular dependency. Unfold a rule of your choosing into the other, and repeat. It's likely you need 2 unfoldings because there are three symbols in the cycle. After simplifying, you'll have three rules that do not depend on each other and are pure left recursive, which Antlr handles internally. – kaby76 Jan 06 '23 at 02:45
  • Your goal here is to only allow certain expressions to be used as statements, rather than allow any valid expression to be a statement, correct? If so, why? Is there some ambiguity which results from other expression types used as statements? – rici Jan 06 '23 at 04:56
  • That is my goal indeed... but what do you mean by that? Are you asking why can't all expressions just be used as statements, or are you saying I made an error in my grammar that let's non-expression-statements be used as statements? – 1Mangomaster1 Jan 06 '23 at 07:32
  • @1Mangomaster1: I'm asking why can't all expressions just be used as statements. Doing that simplifies your grammar, and I don't see the cost. If you really want to prohibit certain kinds of expressions from being used as statements, you can add a check during the walk of the parse tree; that allows you to produce a meaningful message (eg., "Error: Expressions without side effects cannot be used as statements."), which is a lot easier for the reader than "Syntax error". – rici Jan 06 '23 at 17:47
  • The only reason that would be impractical is if you were trying to reuse the syntax for certain expressions used as statements for some other purpose, in which case allowing arbitrary expressions to be used as statements would create an ambiguity. Which is why I asked about that case. (And, by the way, you must @ whoever you're addressing if you want them to be notified about your comment. Sometimes SO guesses the notifications correctly, but often it doesn't, as in this case.) – rici Jan 06 '23 at 17:50
  • @rici I just know I'd have a harder time maintaining something like checking expressions' validity as statements, I managed to fix the left-recursion by writing the `expression_statement` rule into the `expression` manually, as well as in the statements... I knew this was a solution but I hoped there was a better way which Ig there isn't.. regardless.. I wanna avoid lines like '5;' being put in my code.. fairly enough.. this is an expression being used as a statement, but it's clearly not meant to and It sounds messy to check each expression and see if it can be a statement – 1Mangomaster1 Jan 06 '23 at 21:32

1 Answers1

0

I knew that to solve Indirect Left-Recursion I'd have to duplicate one or more of the rules... I hoped there'd be a better way to handle it then what is written online and also said here, but there isn't. I ended up doing that and it worked, thank you

1Mangomaster1
  • 346
  • 1
  • 6
  • 16