accessing lable in sub action.

42 views
Skip to first unread message

Chin-Lung Chang

unread,
Feb 24, 2013, 4:53:17 AM2/24/13
to pe...@googlegroups.com

I want to parse a string like this
10 | 1 2 3
every number after the "|" has to be less than the first number, here 10.

I wrote following grammar to process it:

start = expr

expr = limit:number "|" (n:number " " &{ return n < limit})+

number = n:[0-9]+ { return parseInt(n.join(""), 10); }

In this example, it doesn't work since PEGJS cannot access it. Is there any way to access limit without using global variable ?

Thanks for your help.

Regards,

Chin-Lung Chang

David Majda

unread,
Mar 12, 2013, 3:47:30 PM3/12/13
to chin...@gmail.com, pegjs
Hi,

2013/2/24 Chin-Lung Chang <chin...@gmail.com>:
> I want to parse a string like this
> 10 | 1 2 3
> every number after the "|" has to be less than the first number, here 10.
>
> I wrote following grammar to process it:
>
> start = expr
>
> expr = limit:number "|" (n:number " " &{ return n < limit})+
>
> number = n:[0-9]+ { return parseInt(n.join(""), 10); }
>
> In this example, it doesn't work since PEGJS cannot access it. Is there any
> way to access limit without using global variable ?

You can initialize a variable in the initializer and then remember the
limit in it. Such a variable won't be global in the JavaScript sense,
but it will be global in the sense that it will be accessible in the
whole parser code.

--
David Majda
Entropy fighter
http://majda.cz/

Chin-Lung Chang

unread,
Mar 13, 2013, 1:40:14 AM3/13/13
to pe...@googlegroups.com, chin...@gmail.com
Thanks David, but that what I mean, I don't want to use initializer as well. 
Could you modify the pegjs slightly to allow the sub action could access the upper variables ?
That will be very helpful to parse some special syntax.

Chin-Lung Chang

David Majda於 2013年3月13日星期三UTC+8上午3時47分30秒寫道:

David Majda

unread,
Mar 17, 2013, 3:14:56 PM3/17/13
to Chin-Lung Chang, pegjs
2013/3/13 Chin-Lung Chang <chin...@gmail.com>:
> Thanks David, but that what I mean, I don't want to use initializer as well.
> Could you modify the pegjs slightly to allow the sub action could access the
> upper variables ?

As discussed elsewhere [1], this will happen as part of #69 [2].

[1] https://github.com/dmajda/pegjs/issues/162
[2] https://github.com/dmajda/pegjs/issues/69
Reply all
Reply to author
Forward
0 new messages