简体   繁体   中英

Grako left recursion

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.

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 }* ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM