[antlr-interest] Reusing grammars
Ian McDonagh
ianmdev at yahoo.co.uk
Tue Feb 5 06:07:41 PST 2008
Thanks for that.
I've managed to implement my template system using a combination of a lexical filter and some custom helper classes.
They work together kind of how a SAX parser works with XML. When I encounter a specific token, I check if it is terminating a block, and if so I process that block.
Like the island grammar example in the downloadable examples, I keep track of tokens that appear in both my template language and my expression language.
Thus, I can have constructs like this:
____________________________________________
Dear $title ${firstName + lastName}
Here is a list of your friends:
@foreach( [friend] )Name: $firstName at if( textlength(lastName) > 0 ) $lastName at end e-mail: $email
@end
____________________________________________
I can parse the above template to an intermediate (compiled) form and then merge it with a particular context to produce output.
Hope this all makes sense.
If anyone needs more info on how I did this, feel free to mail me :)
----- Original Message ----
From: Jim Idle <jimi at temporal-wave.com>
To: antlr-interest at antlr.org
Sent: Monday, 4 February, 2008 6:46:37 PM
Subject: Re: [antlr-interest] Reusing grammars
<!--
_filtered {font-family:"Cambria Math";panose-1:2 4 5 3 5 4 6 3 2 4;}
_filtered {font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;}
_filtered {font-family:Tahoma;panose-1:2 11 6 4 3 5 4 4 2 4;}
_filtered {font-family:Verdana;panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;margin-bottom:.0001pt;font-size:12.0pt;font-family:"Times New Roman", "serif";}
a:link, span.MsoHyperlink
{color:blue;text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;text-decoration:underline;}
p
{margin-right:0in;margin-left:0in;font-size:12.0pt;font-family:"Times New Roman", "serif";}
span.EmailStyle18
{font-family:"Calibri", "sans-serif";color:#1F497D;}
.MsoChpDefault
{font-size:10.0pt;}
_filtered {margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
{}
-->
If your language change is switchable at the lexical level, then
you can use island grammars – see the example of this in the downloadable 3.0
examples.
Jim
From: Ian McDonagh
[mailto:ianmdev at yahoo.co.uk]
Sent: Monday, February 04, 2008 4:08 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Reusing grammars
Hi
all
I want to implement my own template language (like Velocity, JSP or ASP) and
reuse an existing ANTLR 3.0 grammar I've written for evaluating expressions.
I might have something like this:
________________________________________
Contacts:
@foreach( getPeople(lastName) )
Name: ${Name}
@end
________________________________________
This input would be parsed to construct a template, which is made up of various
nodes, which could in turn be merged with a context to produce the template
output. The parsed template structure would look something like this:
#Text:"Contacts:"
#ForEach: getPeople(lastName)
|----#Text:"Name: "
|----#Expression: Name
My problem is that my expression language uses some of the same tokens I want
to use in my template language, so it's difficult to use a lexical filter only
(which is pretty simple with tags like '<%' and '%>').
Is it possible to parse the entire input in one pass, switching between
different parsers?
With ANTLR 2.7 I could just use grammar inheritance. What to do in ANTLR
3?
Any advice would be greatly appreciated.
Thanks.
Sent from Yahoo!
- a smarter inbox.
___________________________________________________________
Support the World Aids Awareness campaign this month with Yahoo! For Good http://uk.promotions.yahoo.com/forgood/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080205/3f81af61/attachment.html
More information about the antlr-interest
mailing list