Question re: porting a Parboiled parser to PEG.js. From a PEG newbie.

97 views
Skip to first unread message

josh.r...@gmail.com

unread,
Dec 26, 2012, 5:08:04 PM12/26/12
to pe...@googlegroups.com
I'm having some trouble understanding how to do something recursion related in PEG.js, and I'm sure it's because I'm just generally a PEG newbie in general.

I have the following rule in a Parboiled parser:

public Rule AndExpression() {
return Sequence(
NotSwanExpression(),
ZeroOrMore(
Sequence(
FirstOf(AND(), AND_SYMBOL()),
NotSwanExpression(),push(searcher.and(pop(1),pop()))
)
)
);
}

That can parse "foo AND bar AND baz" into something like AND(AND(TERM(foo),TERM(bar)),TERM(baz))

The closest I could figure out to this in PEG.js was:
AndExpression
= left:NotSwanExpression (AND / AND_SYMBOL) right:NotSwanExpression { return new And(left, right) }
/ NotSwanExpression

However, that doesn't chain. I.e. it parses "foo AND bar" but it fails on "foo AND bar AND baz". I don't know how to properly handle recursion like that in PEG.js, and it is as you might guess a point of grief :( Can anyone help me out here? Thanks in advance,

-Josh Adams

josh.r...@gmail.com

unread,
Dec 29, 2012, 7:17:20 PM12/29/12
to pe...@googlegroups.com, josh.r...@gmail.com
For the record, I ended up resolving this by using partial function application to roll up the right sides until the left side was available to pass to the innermost match, and then rolled out from there.

Just didn't want to go without mentioning how I resolved the issue :) Thanks everybody

soar...@gmail.com

unread,
May 17, 2013, 11:57:36 AM5/17/13
to pe...@googlegroups.com, josh.r...@gmail.com
On Saturday, December 29, 2012 7:17:20 PM UTC-5, josh.r...@gmail.com wrote:
> For the record, I ended up resolving this by using partial function application to roll up the right sides until the left side was available to pass to the innermost match, and then rolled out from there.
>
> Just didn't want to go without mentioning how I resolved the issue :) Thanks everybody

Hi Josh, I'm facing the same issue. Can you please post your solution if you still have it ?

Thanks.

Philippe

Reply all
Reply to author
Forward
0 new messages