Simple grammar for JSPs

19 views
Skip to first unread message

Patrick Mulder

unread,
Dec 13, 2009, 9:53:29 AM12/13/09
to Treetop Development
I would like to implement a basic grammar to extract some information
from the JSP templating language.

My problem is very basic at the start of using Treetop.

My grammar currently is:

grammar Jspgrammar
rule document
jspToken* jspPageToken*
end
rule jspToken
'<%'
end

rule jspPageToken
'<%@page'
end
end


If I then load:
irb -r rubygems -r treetop

and type in the parser:

>> p.parse("<%")
=> SyntaxNode+Document0 offset=0, "<%":
SyntaxNode offset=0, "<%":
SyntaxNode offset=0, "<%"
SyntaxNode offset=2, ""

This looks ok, but my problem now is:
>>p.parse("<%@page")
=> nil

Why is this nil? This should be recognized as jspPageToken.
How can I debug what is going wrong? I have seen already that
inspection of the parser reveals some information:

>> p
=> #<JspgrammarParser:0x10c6c90 @regexps={}, @index=2, @input="<%
@page", @terminal_failures=[], @node_cache={:jspToken=>{0=>SyntaxNode
offset=0, "<%"}}, @consume_all_input=true,
@max_terminal_failure_index=0, @input_length=8>

put not sure how to progress from here.

If anyone has some suggestions, help to offer, I would be very glad!
Thank you!

Clifford Heath

unread,
Dec 13, 2009, 4:57:57 PM12/13/09
to treet...@googlegroups.com
Treetop (like all PEGs) parses greedily, and does not backtrack on an
iterated rule.
In your case, this means that when parsing "<%@page", the "jspToken"
rule matches
first, then the following role jspPageToken fails because it's not
looking at "<%@page",
but at "page". The repetition of jspToken at the start of "document"
is not backtracked,
so the parser fails.

Clifford Heath.
> --
>
> You received this message because you are subscribed to the Google
> Groups "Treetop Development" group.
> To post to this group, send email to treet...@googlegroups.com.
> To unsubscribe from this group, send email to treetop-dev...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/treetop-dev?hl=en
> .
>
>

Patrick Mulder

unread,
Dec 14, 2009, 10:39:49 AM12/14/09
to Treetop Development
Ah, ok.. I guess my understanding of recursion was wrong. Thank you
for pointing this out.
I guess I need some more time to play with grammar and recursion rules
to fully define my JSP grammar.
Reply all
Reply to author
Forward
0 new messages