Grako left recursion

131 views Asked by At

I'm trying to use grako to describe a simple left-recursive grammar but I have trouble to do so.

Right-recursion does work without any problem :

symbol = /[a-z]/ ;
condition = symbol "AND" condition | symbol ;
start = condition $ ;

According to all examples I found, left-recursion should be described this way :

symbol = /[a-z]/ ;
condition = condition "AND" symbol | symbol ;
start = condition $ ;

However, it does not work for the rule given below :

a AND b AND c

I get this error :

grako.exceptions.FailedParse: srecur(1:3) Expecting end of text. :
a AND b AND c
  ^
start

What I understand at this point is that first character of rule matches symbol and not condition "AND" symbol, so grako would like to use it. But my start rule forces that all characters have been consumed.

I've tried to use many workarounds yet but I've not been able to find one that fits.

1

There are 1 answers

1
Jbb On BEST ANSWER

Grako is in fact a PEG parser. Those parsers have the implicit property of not being able to handle left recursion easily.

More details here and there.

For my needs, I have been able to solve my problem with this kind of expressions :

condition = symbol { "AND" symbol }* ;