python - How to generate all possible expressions satisfying a grammar -


i have grammar application following expressions:

(find, search, lookup) [a, the, an, for] item [in, at] (nest, shelf, desk) 

items in round brackets "()" - required, items in square brackets optional, words without brackets required.

simple expressions me be:

  • find item @ desk
  • find item in desk
  • lookup item in nest
  • search item @ shelf

is there parsing methods or libraries can use generate possible combinations?

sjoerd's answer points exrex. since expression can written regex library it.

>>> import exrex >>> list(exrex.generate(r'(find|search|lookup)( a| the| an| for)? item ( in| at)?( nest| shelf| desk)')) ['find item  nest', 'find item  shelf', 'find item  desk', 'find item  in nest', 'find item  in shelf', 'find item  in desk', 'find item  @ nest', 'find item  @ shelf', 'find item  @ desk', 'find item  nest', 'find item  shelf', 'find item  desk', 'find item  in nest', 'find item  in shelf', 'find item  in desk', 'find item  @ nest', 'find item  @ shelf', 'find item  @ desk', 'find item  nest', 'find item  shelf', 'find item  desk', 'find item  in nest', 'find item  in shelf', 'find item  in desk', 'find item  @ nest', 'find item  @ shelf', 'find item  @ desk', 'find item  nest', 'find item  shelf', 'find item  desk', 'find item  in nest', 'find item  in shelf', 'find item  in desk', 'find item  @ nest', 'find item  @ shelf', 'find item  @ desk', 'find item  nest', 'find item  shelf', 'find item  desk', 'find item  in nest', 'find item  in shelf', 'find item  in desk', 'find item  @ nest', 'find item  @ shelf', 'find item  @ desk', 'search item  nest', 'search item  shelf', 'search item  desk', 'search item  in nest', 'search item  in shelf', 'search item  in desk', 'search item  @ nest', 'search item  @ shelf', 'search item  @ desk', 'search item  nest', 'search item  shelf', 'search item  desk', 'search item  in nest', 'search item  in shelf', 'search item  in desk', 'search item  @ nest', 'search item  @ shelf', 'search item  @ desk', 'search item  nest', 'search item  shelf', 'search item  desk', 'search item  in nest', 'search item  in shelf', 'search item  in desk', 'search item  @ nest', 'search item  @ shelf', 'search item  @ desk', 'search item  nest', 'search item  shelf', 'search item  desk', 'search item  in nest', 'search item  in shelf', 'search item  in desk', 'search item  @ nest', 'search item  @ shelf', 'search item  @ desk', 'search item  nest', 'search item  shelf', 'search item  desk', 'search item  in nest', 'search item  in shelf', 'search item  in desk', 'search item  @ nest', 'search item  @ shelf', 'search item  @ desk', 'lookup item  nest', 'lookup item  shelf', 'lookup item  desk', 'lookup item  in nest', 'lookup item  in shelf', 'lookup item  in desk', 'lookup item  @ nest', 'lookup item  @ shelf', 'lookup item  @ desk', 'lookup item  nest', 'lookup item  shelf', 'lookup item  desk', 'lookup item  in nest', 'lookup item  in shelf', 'lookup item  in desk', 'lookup item  @ nest', 'lookup item  @ shelf', 'lookup item  @ desk', 'lookup item  nest', 'lookup item  shelf', 'lookup item  desk', 'lookup item  in nest', 'lookup item  in shelf', 'lookup item  in desk', 'lookup item  @ nest', 'lookup item  @ shelf', 'lookup item  @ desk', 'lookup item  nest', 'lookup item  shelf', 'lookup item  desk', 'lookup item  in nest', 'lookup item  in shelf', 'lookup item  in desk', 'lookup item  @ nest', 'lookup item  @ shelf', 'lookup item  @ desk', 'lookup item  nest', 'lookup item  shelf', 'lookup item  desk', 'lookup item  in nest', 'lookup item  in shelf', 'lookup item  in desk', 'lookup item  @ nest', 'lookup item  @ shelf', 'lookup item  @ desk'] 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -