Can anyone help me with treetop

11 views
Skip to first unread message

Tom Hall

unread,
Feb 10, 2014, 6:36:53 AM2/10/14
to computa...@googlegroups.com
Worked on the interpretor a bit more last night, included environments and define, but I am missing a trick somewhere in the treetop grammar. 

I want to allow spaces between the sexps (that now make up a program)

But whenever I add spaces around the sexps in the program definition, nothing works, if anyone can help id appreciate it (I stole some ideas from James's Stickup grammar but as he's used the class based thing when we did the inline to_ast stuff I cant spot how to make it work).

The documentation is rubbish for treetop, any good tutorials people recommend?

Tom

Tim Cowlishaw

unread,
Feb 10, 2014, 6:44:33 AM2/10/14
to Tom Hall, computa...@googlegroups.com
On 10 February 2014 11:36, Tom Hall <thatto...@gmail.com> wrote:
But whenever I add spaces around the sexps in the program definition,
> nothing works,

AAh - nothing works in the sense that it halts forever, or you get a
stack overflow? You've probably defined a left-recursive grammar:

http://stackoverflow.com/questions/6119059/how-to-deal-with-treetop-left-recursion

> The documentation is rubbish for treetop, any good tutorials people
> recommend?

I like Aaraon Gough's tutorial
(http://thingsaaronmade.com/blog/a-quick-intro-to-writing-a-parser-using-treetop.html)
- and, while it's not a tutorial in such, the examples in Tom S's
book are pretty instructive too!

Hope this helps!

Cheers,


Tim

Tom Stuart

unread,
Feb 10, 2014, 7:18:30 AM2/10/14
to Tom Hall, computa...@googlegroups.com
On 10 Feb 2014, at 11:36, Tom Hall <thatto...@gmail.com> wrote:
> whenever I add spaces around the sexps in the program definition, nothing works

I assume this means you've changed

rule program
expressions:sexp* {
def to_ast
Program.new(*expressions.elements.map{|el| el.to_ast})
end
}
end

to

rule program
expressions:(sexp ' '*)* {
def to_ast
Program.new(*expressions.elements.map{|el| el.to_ast})
end
}
end

i.e. just replaced `sexp` with `(sexp ' '*)` in the parsing expression. That's the correct change to the parsing expression, but it affects the shape of the concrete syntax tree nodes inside `expressions`. Each element of `expressions` now includes both the `sexp` and the whitespace, so you also need to update your `#to_ast` implementation to pick out just the `sexp` as the receiver of the recursive call:

rule program
expressions:(sexp ' '*)* {
def to_ast
Program.new(*expressions.elements.map{|el| el.sexp.to_ast})
end
}
end

This is (vaguely, confusingly) covered by the "Automatically-Defined Element Accessor Methods" section of http://treetop.rubyforge.org/semantic_interpretation.html.

Cheers,
-Tom
Reply all
Reply to author
Forward
0 new messages