Looking at the example at
http://www.acooke.org/lepl/offside.html#example
I am trying to create a grammar with blocks defined with indentation
but without special line ending.
Thanks to above example and Andrew's help I have so far
---- cut ----
from lepl import *
word = Token(Word(Lower()))
statement = Delayed()
simple = Line(word[:])
empty = Line(Empty(), indent=False)
block = Block(statement[1:])
statement += (simple | empty | block) > list
program = statement[:]
program.config.lines(block_policy=constant_indent(4))
parser = program.get_parse_string()
print(parser('''
abc def
ghijk
mno pqr
stu
vwx yz
'''))
---- cut ----
Above code gives me:
[[], ['abc', 'def'], ['ghijk'], [['mno', 'pqr'], [['stu']],
['vwx', 'yz']]]
How to change above in order to get:
[[], ['abc', 'def'], ['ghijk', ['mno', 'pqr', ['stu']], ['vwx',
'yz']]]
?
Best regards,
w
> --
> You received this message because you are subscribed to the Google Groups "lepl" group.
> To post to this group, send email to le...@googlegroups.com.
> To unsubscribe from this group, send email to lepl+uns...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/lepl?hl=en.
>
Thanks for the tip.
The order matters!
Should be
statement += (block | line | empty) > list
instead of
statement += (empty | line | block) > list
The whole example below.
------- cut --------------
from lepl import *
word = Token(Word(Lower()))
statement = Delayed()
line = Line(word[:])
empty = Line(Empty(), indent=False)
block = line & Block(statement[1:])
statement += (block | line | empty) > list
program = statement[:]
program.config.lines(block_policy=constant_indent(4))
parse = program.parse
print(parse('''
abc def
ghijk
mno pqr
stu
vwx yz
'''))
------- cut --------------
Best regards,
w
Not sure if you understand what is happening or not - this is just in case it
helps / apologies if it's obvious.
- First, Lepl does things in order, even if they are ambiguous. This is by
design - Lepl will provide multiple parses for input if you ask it to
(called "parse trees"). It won't warn you (and it's not an error) if the
grammar is ambiguous.
- Second, I guess that you are seeing a problem here, when there was no
similar problem in the initial example, because removing the ":" has made
things ambiguous in some way. I am not sure what the ambiguity is, but it
might be that Lepl can match an indented line as a less indented line plus
some extra space. If that's the case, then you may want to look at how to
avoid that.
Cheers,
Andrew