mardi 4 août 2015

PyParsing - Parsing strings that must contain at least one occurrence of a symbol

I am trying to parse strings using PyParsing and detect at parsing time if they contain at least one occurrence of a defined symbol (say ‘$’).

For example, the following string would be parsed:

"('a$x' & 'b') | $ | ('c' <-> $)"

But this one would not (and a parsing exception would be raised):

"('a$x' & 'b') | 'c'"

How can this be done?

My goal here is to build abstract syntax trees (ASTs) that must contain at least one occurrence of this special symbol '$'. For the moment, I consider '$' as a simple literal:

my_symbol = Literal('$')
my_symbol.setParseAction(lambda tokens: Symbol())

and I verify the "at least one occurrence" condition after parsing, i.e., once my AST is built (note that Symbol() is a class that represent '$' in my AST). However, I think that a detection at parsing time would be more efficient.

EDIT: I don't know if it may help but here is a simplified version of the grammar I would like to parse:

<a> ::= '$' | '~' <a> | <a> '&' <b> | <b> '&' <a> | <a> '|' <b> 
      | <b> '|' <a> | <a> '->' <b> | <b> '->' <a> | <a> '<->' <b> 
      | <b> '<->' <a> | '(' <a> ')'

<b> ::= 'True' | 'False' | <atom> | '~' <b> | <b> '&' <b> | <b> '|' <b> 
      | <b> '->' <b> | <b> '<->' <b> | '(' <b> ')'

<atom> is defined by any string surrounded by single quotes



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire