Search code examples
pythonpyparsingebnf

pyparsing with ebnf and whitespaces


I'm using http://pyparsing.wikispaces.com/file/view/ebnf.py to convert my ebnf definition.

ebnf def looks like this:

TEST = A, SPACE, A;

A = "AA" | "BB";
SPACE = " ";

if I load the file and try to parse a string like:

e = ebnf.parse(ebnf_file)
e['TEST'].leaveWhitespace().parseString('AA BB') # same without leaveWhitespace()

I get:

ParseException: Expected " " (at char 3), (line:1, col:4)

Does anybody have an ideas/solutions?


Solution

  • The leaveWhitespace() has to be applied to the original whitespace-containing tag, so try the following:

    e = ebnf.parse(ebnf_file)
    e['SPACE'] = e['SPACE'].leaveWhitespace()
    e['TEST'].parseString('AA BB')