lepl and blocks

5 views
Skip to first unread message

wrobell

unread,
Dec 12, 2011, 6:46:20 PM12/12/11
to lepl
Hi,

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

andrew cooke

unread,
Dec 12, 2011, 7:43:05 PM12/12/11
to le...@googlegroups.com

as i said earlier, i can't do much from here, but i would suggest using "with
TraceVariable()" http://acooke.org/lepl/debugging.html#index-3 which should
help clarify what is matching what. if it works with tokens and lines (i
guess it should, but cannot be sure). andrew

> --
> 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.
>

wrobell

unread,
Dec 13, 2011, 7:35:54 PM12/13/11
to lepl
On Dec 13, 12:43 am, andrew cooke <and...@acooke.org> wrote:
> as i said earlier, i can't do much from here, but i would suggest using "with
> TraceVariable()"http://acooke.org/lepl/debugging.html#index-3which should

> help clarify what is matching what.  if it works with tokens and lines (i
> guess it should, but cannot be sure).  andrew

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

andrew cooke

unread,
Dec 13, 2011, 8:30:45 PM12/13/11
to le...@googlegroups.com

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

Reply all
Reply to author
Forward
0 new messages