Recursive expression problem

19 views
Skip to first unread message

caio ariede

unread,
Dec 24, 2009, 7:56:56 AM12/24/09
to neoto...@googlegroups.com
I have the following code:

call <- "call" spaces value;

op <- value "+" value;
value <- op / decimal / variable;

That is proposed to match:

call 1
call x
call 1+1 or
call 1+x or
call 1+1+1

But it's causing an infinite recursion.

The expected parsing is:

call 1+1 // "call" <op> // <value: 1> + <value: 1> // <decimal: 1> +
<decimal: 1>

But I think it's taking:

call 1+1 // "call" <op> // <value: 1+1> ...recursive

What I'm doing wrong?

Caio Ariede
http://caioariede.com/

Sean Cribbs

unread,
Dec 24, 2009, 11:02:08 AM12/24/09
to neoto...@googlegroups.com
Neotoma and PEGs in general tend not to support left-recursion. Your
rule needs to reduce the leftmost term, while you could allow the
right to expand. The "arithmetic" parser has good examples of this.
Here's how I'd refactor your grammar:

call <- "call" spaces value;

value <- op / primitive;
op <- primitive "+" value / primitive;
primitive <- decimal / variable;

Sean

caio ariede

unread,
Dec 25, 2009, 8:50:54 PM12/25/09
to neoto...@googlegroups.com
Nice, it worked.

Thanks!

Caio Ariede
http://caioariede.com/

mikhailfranco

unread,
Jan 12, 2010, 3:46:09 AM1/12/10
to Neotoma

Sean Cribbs wrote:
> Neotoma and PEGs in general tend not to support left-recursion.

"Packrat Parsers Can Support Left Recursion"
http://www.viewpointsresearch.org/pdf/tr2007002_packrat.pdf

It would be 'a piece of work' to implement, but worth it.

Mik

Sean Cribbs

unread,
Jan 12, 2010, 9:01:32 AM1/12/10
to neoto...@googlegroups.com
Mik,

Thanks for the link. I was aware of that paper, but hadn't gotten
reading it closely yet. In many cases, you can factor left-recursion
into right-recursion anyway, so it's a little low on the list of todos.

Sean

Reply all
Reply to author
Forward
0 new messages