开发者

Parsing user expression for formatting errors

User provides a string containing a Python expression : "a==(1,1) and {b==2 or c==foo}".

I am trying to write a parser that checks the following rules:

  1. There needs to be a whitespace before and after a logical expression (or or and)
  2. There needs to be a whitespace before and after curly braces
  3. There should be NO whitespace within an expression (a ==(1,1) is invalid)

I found that "parser" module but I am not sure I understand it.

Establishing all the rules through multiple regular expressions (reading the string char by char and making sure if satisfies the rules) is very tedious.

What is the most elegant way to tackle this 开发者_开发知识库problem?


The most elegant way I'd say is to use something like pyparsing, a real parser combined with "pythonic" ease of use.

It may be a bit of overkill for small projects, but it isn't too hard to get started and will allow for plenty of growth in the syntax. Software requirements always seem to grow in one direction so I'd recommend giving it a try.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜