Repetition count

162 views
Skip to first unread message

jmillan

unread,
Jan 21, 2011, 4:29:45 PM1/21/11
to PEG.js: Parser Generator for JavaScript
Hi there,

I have not found a way to specify a 'repetition count' for a parsing
expression. Is it possible?

Thanks a lot.

David Majda

unread,
Jan 23, 2011, 5:37:47 AM1/23/11
to pe...@googlegroups.com
Hi,

2011/1/21 jmillan <doct...@gmail.com>:


> I have not found a way to specify a 'repetition count' for a parsing
> expression. Is it possible?

currently it is not supported directly, you need to repeat expressions
in the grammar manually. I didn't add a special syntax for expressing
repetition since in most grammars it is used very rarely. Do you have
a specific use case in mind?

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

jose luis millan

unread,
Jan 23, 2011, 7:17:50 AM1/23/11
to pe...@googlegroups.com
Hello,

First of all, thanks for building this tool.

On Sun, Jan 23, 2011 at 11:37 AM, David Majda <da...@majda.cz> wrote:
> Hi,
>
> 2011/1/21 jmillan <doct...@gmail.com>:
>> I have not found a way to specify a 'repetition count' for a parsing
>> expression. Is it possible?
>
> currently it is not supported directly, you need to repeat expressions
> in the grammar manually. I didn't add a special syntax for expressing
> repetition since in most grammars it is used very rarely. Do you have
> a specific use case in mind?
>

This could be a representative example. A case where the grammar
accepts a number of a maximum of 5 digits for a specific rule:
_________
number = number: (DIGIT DIGIT? DIGIT? DIGIT? DIGIT?) { return
number.join("") }

DIGIT = DIGIT: [0-9]
_________

The specific use cases are network port ranges and IPv4 and and IPv6
network addresses.

Anyway, for this cases, expression repetition does the work.

David Majda

unread,
Jan 24, 2011, 10:19:23 AM1/24/11
to pe...@googlegroups.com
Hi,

2011/1/23 jose luis millan <doct...@gmail.com>:


> This could be a representative example. A case where the grammar
> accepts a number of a maximum of 5 digits for a specific rule:
> _________
> number  = number: (DIGIT DIGIT? DIGIT? DIGIT? DIGIT?)  { return
> number.join("") }
>
> DIGIT     = DIGIT: [0-9]
> _________
>
> The specific use cases are network port ranges and IPv4 and and IPv6
> network addresses.

Thanks for the use-case.

I still think adding repetition to the grammar syntax is not really
necessary, but if some nice syntax gets invented, I'd probably add it
for convenience.

Jiangbin Zhao

unread,
Sep 25, 2011, 12:21:42 PM9/25/11
to pe...@googlegroups.com
Hi, I started learning pegjs yesterday. Great tool. Please see my comment about repetition below.
I ran into the same situation wanting repetition count, too. I think having it will help simplify the grammar. What if an expression could repeat 20 times, or even more?

As for the syntax to specify the count, how about: "expression(min, max)"? The count would be in the range of min and max, inclusively.

Instead of manually repeating the "expression?" constructs many times, I ended up checking the array length in parser action, for example:

    id = digits:[0-9]+ {return (digits.length <= 20 && digits.length >= 10) ? digits.join('') : null;}

But that certainly wouldn't look as good as this:

    id = digits:[0-9](10,20) {return digits.join('');}

Regards,

Jiangbin

PS: Forgive me if this post appears twice. My first attempt seemed have not gone through.

David Majda

unread,
Jan 15, 2012, 2:33:34 PM1/15/12
to zhaoji...@gmail.com, pe...@googlegroups.com
Hi,

sorry for a late reply, I am crawling through a big e-mail backlog.

I reconsidered my negative position towards specifying arbitrary
repetition count. I am now trying to decide the syntax. See the
following issue on GitHub where I tried to summarize my thinking:

https://github.com/dmajda/pegjs/issues/30

David

2011/9/25 Jiangbin Zhao <zhaoji...@gmail.com>:

--

Reply all
Reply to author
Forward
0 new messages