Yet another attempt at a safe eval() call

Chris Angelico rosuav at gmail.com
Fri Jan 4 14:02:49 EST 2013


On Sat, Jan 5, 2013 at 5:43 AM, Grant Edwards <invalid at invalid.invalid> wrote:
> On 2013-01-04, Chris Angelico <rosuav at gmail.com> wrote:
>> On Sat, Jan 5, 2013 at 5:09 AM, Grant Edwards <invalid at invalid.invalid> wrote:
>>> The error messages are still pretty cryptic, so improving
>>> that will add a few more lines.  One nice thing about the ast code is
>>> that it's simple to add code to allow C-like character constants such
>>> that ('A' === 0x41).  Here's the first pass at ast-based code:
>>
>> Looks cool, and fairly neat! Now I wonder, is it possible to use that
>> to create new operators, such as the letter d? Binary operator, takes
>> two integers...
>
> I don't think you can define new operators.  AFAICT, the
> lexing/parsing is done using the built-in Python grammar. You can
> control the behavior of the predefined operators and reject operators
> you don't like, but you can't add new ones or change precedence/syntax
> or anything like that.
>
> If you want to tweak the grammar itself, then I think you need to use
> something like pyparsing.

Oh well, hehe. I've not seen any simple parsers that let you
incorporate D&D-style dice notation ("2d6" means "roll two 6-sided
dice and sum the rolls" - "d6" implies "1d6").

ChrisA



More information about the Python-list mailing list