Specifying a 'set' of subgrammars

11 views
Skip to first unread message

PD Priyadarshan

unread,
Feb 14, 2018, 1:56:04 PM2/14/18
to modgrammar
Hi,

I am using modgrammar to create a parser that needs to handle a case as shown in this example:

'book' has these 3 attributes that can appear in any order in the input file:
  - title
  - author
  - isbn

Is there a better way to specify this other than  ONE_OR_MORE(G(OR(title, author, isbn))) ?

How to enforce that all three appear only once, but can appear in any order? Is there a way to specify a 'set' of subgrammars?

Thanks,
-PD

Alex Stewart

unread,
Feb 16, 2018, 4:39:47 PM2/16/18
to modgr...@googlegroups.com, peed...@gmail.com
Hmm..

There isn't really a preexisting grammar construct for this sort of thing..  You could do an OR with all of the possible permutations of the three subgrammars, like so:

OR(
    (title, author, isbn),
    (title, isbn, author),
    (author, title, isbn),
    (author, isbn, title),
    (isbn, title, author),
    (isbn, author, title),
)

..but I'll grant you that's not particularly elegant and doesn't scale very well.  I haven't actually tried it, but I think you may be able to do the same thing in a somewhat cleaner way by using Python's built-in itertools module, like so:

OR(*itertools.permutations((title, author, isbn)))

(note that there's no G() around that, those are just simple tuples.  Most of the time, modgrammar is smart enough to automatically convert tuples/lists into grammars when needed, so you can actually use simple tuples and existing tuple/list manipulating routines and then pass the result as subgrammars to things like OR(), etc)

Short of that, it would probably require defining your own custom grammar class to implement that..  I may need to think about this a bit, though.. I'll let you know if I come up with a better answer.

-alex

(PS: You don't need G() around your OR().. OR already produces a grammar class as its result so using G() to convert it to a grammar is a no-op in that case.  In fact, usually the only time you need G() is at the top level if your toplevel operation isn't already a grammar construct of some kind)


--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages