8

What is the difference between:

foo = TOKEN1 + TOKEN2

and

foo = Combine(TOKEN1 + TOKEN2)

Thanks.

UPDATE: Based on my experimentation, it seems like Combine() is for terminals, where you're trying to build an expression to match on, whereas plain + is for non-terminals. But I'm not sure.

Nick Heiner
  • 119,074
  • 188
  • 476
  • 699

1 Answers1

20

Combine has 2 effects:

  • it concatenates all the tokens into a single string

  • it requires the matching tokens to all be adjacent with no intervening whitespace

If you create an expression like

realnum = Word(nums) + "." + Word(nums)

Then realnum.parseString("3.14") will return a list of 3 tokens: the leading '3', the '.', and the trailing '14'. But if you wrap this in Combine, as in:

realnum = Combine(Word(nums) + "." + Word(nums))

then realnum.parseString("3.14") will return '3.14' (which you could then convert to a float using a parse action). And since Combine suppresses pyparsing's default whitespace skipping between tokens, you won't accidentally find "3.14" in "The answer is 3. 14 is the next answer."

PaulMcG
  • 62,419
  • 16
  • 94
  • 130
  • +1, a pretty authoritative answer (not all may realize Paul is the author of `pyparsing`, so I'm mostly pointing that out!-). – Alex Martelli May 31 '10 at 00:36
  • Thanks, Alex. And @Rosarch, welcome to pyparsing! These questions you are posting are very common for first-time pyparsing users, so keep plugging away. I've tried to cover some of these points in the online wiki and docs, but I can see there is still a ways to go! – PaulMcG May 31 '10 at 00:44
  • Yeah, I am fully aware that Paul is the author of pyparsing. It's great that you're on SO to provide noobs like me with guidance. – Nick Heiner May 31 '10 at 01:05