Questions about separatedBy

37 views
Skip to first unread message

stefan stanciulescu

unread,
Oct 15, 2015, 7:48:54 AM10/15/15
to parboiled2.org User List
Hi all,

I have been making progress understanding how to use parboiled2 to write a very simple parser for reading C/C++ files into a very simple custom AST.

My problem is in understanding how separatedBy / using a separator and EOI works.

The simplified code is in http://pastebin.com/nHTZRF8a

Example string:
#ifdef A
test
#endif

This is what I get:
Unexpected end of input, expected '\n' or '\r' (line 3, column 7): #endif

I do understand that it expects a sep, as I wrote in the grammar, but if I change the sep rule to be :
def sep = rule{ch(EOI) | "\n" | "\r\n" }
the program does not terminate.

Now, I can use instead 
 rule{ zeroOrMore(line).separatedBy(sep)
which will work fine, except that it adds an extra empty line in my result, e.g. Vector(YourChoice(defined(A),Vector(Textline(test), Textline()),None))

I simplified my example, and I would prefer to use this separatedBy but how do I get it to not add an extra line? I do not understand where does that come from...

What am I missing, what am I doing wrong? (Quite a beginner in parsing, though I am trying to learn and understand better).

Thanks in advance for your time!

Mathias Doenitz

unread,
Oct 15, 2015, 7:55:37 AM10/15/15
to parboil...@googlegroups.com
Stefan,

the difference between

zeroOrMore(line ~ sep)

and

line.*(sep)

is that the former will always expect (and match) a separator after a line, even the very last line,
while the latter will never match a trailing separator.

So given `line sep line` the former will match only `line sep` while the latter matches `line sep line`.

Cheers,
Mathias

---
mat...@parboiled.org
http://www.parboiled.org
> --
> You received this message because you are subscribed to the Google Groups "parboiled2.org User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to parboiled-use...@googlegroups.com.
> Visit this group at http://groups.google.com/group/parboiled-user.
> To view this discussion on the web visit https://groups.google.com/d/msgid/parboiled-user/a4683068-cd89-45e9-97c2-c56cf137e22f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

stefan stanciulescu

unread,
Oct 22, 2015, 7:41:20 AM10/22/15
to parboiled2.org User List
Thanks Mathias. I made it work, though now I have a different problem (parsing cyrillic and chinese etc :-) )
Reply all
Reply to author
Forward
0 new messages