How to deal with Treetop left recursion

I have a grammar file for a new general purpose programming language that I am trying to build. I try to make the language reliable and natural to use (it is strongly inspired by Ruby and others), and at the same time I introduced some left recursive rules.

I have seen several examples that seem to point to 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 

It seems to me that he will have another problem, and she will fail anyway. Am I right or is it β€œjust working”?

The specific left recursions that I am trying to eliminate are found in this grammar file . I'm not sure which rules are affected, but at least some of them pointed to have left recursion. (By the way, I tried to fix the specific range problem that he mentioned by tightening the range rule.)

+6
source share
1 answer

Special case

 rule l_recurse l_recurse / 'something else' end 

simplifies

 rule l_recurse 'something_else' end 

(as well as the correct recursion rule), so I need to see your specific example to find out what you want to know. The answer to this question is given by the general rule for eliminating left recursion.

One of the typical easily removable left recursive cases is the list:

 rule l_list item | l_list ',' item end 

and this 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 eliminating general recursion).

+4
source

Source: https://habr.com/ru/post/888971/


All Articles