0

I've a grammar as follow:

expression : scalar 
           | vector;
scalar : <bunch of rules>
       | vector[scalar] #VectorIndex
       ;
vector : <bunch of rules>
       | scalar ('*' | '+' | '-') vector
       ;

Is there any possibility to remove indirect left recursion from this grammar? Replacing vector with all its sub-rules will make the grammar too repetitive and messy.

daycoder
  • 21
  • 3
  • You should just merge the `vector` and `scalar` rules into a single `expression` rule and let the type checker take care of the distinction between vectors and scalars. – sepp2k Dec 27 '18 at 20:51
  • If I do that I'll have all the `vector` rules twice - once when I merge `vector` into `expression` and once for the `scalar` rule `vector[scalar]`. – daycoder Dec 27 '18 at 21:40
  • `vector[scala]` would just become `expression[expression]`. – sepp2k Dec 27 '18 at 21:42
  • It'll make the grammar more flexible than desired - I don't want to accept something like `vector[vector]`. How do you suggest we can handle that? – daycoder Dec 27 '18 at 21:53
  • As I said, that'd be the type checker's job (or the runtime system's in a dynamically typed language) – sepp2k Dec 27 '18 at 21:54
  • What do you mean type checker in Antlr? You mean add checks in the visitor? – daycoder Dec 27 '18 at 22:00
  • A type checker is the phase of the compiler that rejects programs if they contain type errors. And yes, that would usually be implemented as a visitor. – sepp2k Dec 27 '18 at 22:06

0 Answers0