Balachandar
Balachandar

Reputation: 1628

pyparsing how to create grammar object from string

I am having grammar for my pyparsing application in DB. They are in String format. I want to convert the same to the grammer object in my application and struck up with it.

Word(alphas) + Word(nums)

Above snippet is one of the entries in the database for which i need to construct grammer object

Can any one let me know how it can be accomplished

Upvotes: 2

Views: 399

Answers (2)

PaulMcG
PaulMcG

Reputation: 63782

For something safer than eval, you can use the EBNF parser that is included with the pyparsing source installation, or download it from the wiki. Then you can define your parser using EBNF, generate a pyparsing parser, and use that to parse the related text.

Upvotes: 2

Amper
Amper

Reputation: 514

If I understand correctly, you can use the eval or exec. For Example:

g = eval("Word(alphas) + Word(nums)")

Upvotes: 3

Related Questions