Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with Treetop left-recursion

I have a grammar file for a new general-purpose programming language I'm trying to build. I'm trying to make the language robust and natural to use (it is heavily inspired by Ruby, among others), and in doing so I have introduced some left-recursive rules.

I've seen some examples that seem to indicate the following left-recursive rule:

rule l_recurse
  l_recurse / 'something else'
end

can be made non-left-recursive by changing it to:

rule r_recurse
  'something else' / r_recurse
end

To me this looks like it would have the a different problem and would still fail. Am I right, or will this "just work"?

The specific left-recursions I'm trying to (find and) eliminate are found in this grammar file. I'm not sure which rules are affected, but at least some were pointed out to have left-recursion. (By the way I have tried to eliminate the specific range issue he mentioned by tightening up range's rule.)

like image 933
ravinggenius Avatar asked May 25 '11 02:05

ravinggenius


1 Answers

The particular case

rule l_recurse
  l_recurse / 'something else'
end

simplifies to

rule l_recurse
   'something_else'
end

(and so does the right recursion rule) so I need to see your specific example to find out what you want to know. The answer to this question gives the general rule for left recursion elimination.

One of the typical easy-to-remove left recursion cases is lists:

rule l_list
    item | l_list ',' item
end

and that can be changed to right recursion

rule r_list
    item r_tail?
end

rule r_tail
    ',' r_list
end

(which is a special case of the general recursion elimination).

like image 122
Kathy Van Stone Avatar answered Nov 13 '22 01:11

Kathy Van Stone