Copying and line breaks

I just started with pyparsing and I have problems with line breaks.

My grammar:

 from pyparsing import * newline = LineEnd () #Literal ('\n').leaveWhitespace () minus = Literal ('-') plus = Literal ('+') lparen = Literal ('(') rparen = Literal (')') ident = Word (alphas) integer = Word (nums) arith = Forward () parenthized = Group (lparen + arith + rparen) atom = ident | integer | parenthized factor = ZeroOrMore (minus | plus) + atom arith << (ZeroOrMore (factor + (minus | plus) ) + factor) statement = arith + newline program = OneOrMore (statement) 

Now that I am parsing the following:

 print (program.parseString ('--1-(-a-3+n)\nx\n') ) 

The result will be as expected:

 ['-', '-', '1', '-', ['(', '-', 'a', '-', '3', '+', 'n', ')'], '\n', 'x', '\n'] 

But when the second line can be analyzed as the tail of the first line, does the first \n wave away?

code:

 print (program.parseString ('--1-(-a-3+n)\nx\n') ) 

Actual result:

 ['-', '-', '1', '-', ['(', '-', 'a', '-', '3', '+', 'n', ')'], '-', 'x', '\n'] 

Expected Result:

 ['-', '-', '1', '-', ['(', '-', 'a', '-', '3', '+', 'n', ')'], '\n', '-', 'x', '\n'] 

Actually, I don't want the parser to automatically attach to statements.

1. What am I doing wrong?

2. How can I fix this?

3. What happens under the hood, causing this behavior (which is certainly reasonable, but I just don't see the point)?

+6
source share
2 answers

'\ n' is usually skipped as a space character. If you want "\ n" to be significant, you must call setDefaultWhitespaceChars to remove the "\ n" as a missing space (you need to do this before defining any of your pirated expressions):

 from pyparsing import * ParserElement.setDefaultWhitespaceChars(' \t') 
+9
source

What happens here is that the parser ignores any spaces by default. Before defining any elements, you must add the following line of code:

 ParserElement.setDefaultWhitespaceChars(" \t") 

The normal default space characters are "\ t \ r \ n", I suppose.

Edit: Paul beat me. I had to upgrade after lunch together. :)

+1
source

All Articles