Re: [modgrammar] Parentheses matching taking a very long time

41 views
Skip to first unread message

Alex Stewart

unread,
May 19, 2014, 5:09:07 PM5/19/14
to modgr...@googlegroups.com
Hi Peter,

I haven't had a chance to really dig into this yet, but on initial inspection, I note that your grammar syntax looks a bit odd..  Is the text you quoted actually the Modgrammar (python) code you are trying to use (or is it just some variant of BNF, for example, and your actual code is different)?

If that is the Python code you're using (and you are using Modgrammar), then I'm not actually sure how it's working at all, to be honest.  If it isn't, though, it would actually be very helpful if you could post the actual grammar-definition code you're working with as well..

Usually, if something's taking an inordinately long time, I find that usually means somebody's managed to get some left-recursion (https://pythonhosted.org/modgrammar/tutorial.html#left-recursion) somewhere in their grammar definition, but I don't see off hand anywhere that you'd be likely to have that in your grammar, so that seems less likely in this case...

You might want to take a look at the debugging functionality (http://pythonhosted.org/modgrammar/debugging.html), which can give you some deeper insight into each of the steps that the Modgrammar engine performs when parsing a particular bit of text (Note that it's just recently been pointed out to me there's a typo in that document:  To turn debugging on, you should pass the "debug=..." (NOT "debugging") parameter when calling Grammar.parser() to create a parser object, so you would want to do something like "p = MyGrammar.parser(debug=sys.stderr)" )

--Alex



On Mon, May 19, 2014 at 10:50 AM, Peter Rowe <peter.j...@gmail.com> wrote:
Here's my grammar:

Round       = Number, ':', [MoveMarker], Stitches, '.';
Number      = ( '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ), {( '0' |
              '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' )};
MoveMarker  = 'm', ( '-' | '+' ), Number, ',';
Stitches    = ( {Stitch}, [Repeat] ), {{Stitch}, [Repeat]};
Stitch      = ( Knit | Purl | Increase | Decrease );
Repeat      = ( Repeat1 | Repeat2 | Repeat3 );
Knit        = ( 'K' | 'k' ), [Number], ['-tbl'], [RepeatCount];
Purl        = ( 'P' | 'p' ), [Number], ['-tbl'], [RepeatCount];
Increase    = ( YarnOver | ( 'I' | 'i' ), 'nc', Number, 'to', Number );
Decrease    = ( ( 'S' | 's' ), 'k', ['2'], 'p' | ( 'K' | 'k' ), Number, 'tog' ),
              [RepeatCount];
Repeat1     = '(', Stitches, ')', RepeatCount;
Repeat2     = '<', Stitches, '>', RepeatCount;
Repeat3     = '[', Stitches, ']', ( RepeatCount | 'odd repeats: twice, even:
              once' );
RepeatCount = ( 'twice' | Number, 'times' );
YarnOver    = ( 'Y' | 'y' ), 'o', [RepeatCount];

When applied to the line
353: (Yo, skp, k2tog, yo) 6 times, yo, skp, k2tog, [<(skp, k2, yo) twice, k2, yo, k1-tbl, yo, k2, (yo, k2, k2tog) twice, skp, k3, k2tog> twice, (skp, k2, yo) twice, k2, yo, k1-tbl, yo, k2, (yo, k2, k2tog) twice> odd repeats: twice, even: once, skp, k2tog, yo, (yo, skp, k2tog, yo) 5 times.

it takes much longer than I've been willing to wait so far to figure out that there is a mismatched square bracket. By inspection, the > right before "odd repeats" should be a ] instead. Why does it take a super long time for the grammar to throw an error on this?

--
You received this message because you are subscribed to the Google Groups "modgrammar" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modgrammar+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages