[antlr-interest] Seeking advice - 2 questions using ANTLR (can translate using rewrite?)

Adam Connelly adam.rpconnelly at googlemail.com
Fri Jan 18 01:44:31 PST 2008


I would tend to go along the template route because, apart from anything
else, it let's you provide multiple output targets (i.e. you could translate
into other syntaxes than just excel simply by creating a new template file
if you wanted).

So far I've only got good things to say about StringTemplate and template
rewriting.

Adam

On 17/01/2008, Mark Volkmann <r.mark.volkmann at gmail.com> wrote:
>
> On Jan 17, 2008 4:27 PM, Frank Font <mrfont at room4me.com> wrote:
> > Hi Mark,
> >
> > Thanks for the sample grammar.
> >
> > To keep things simple, do you think the entire translation can be done
> > via "rewrite=true" setting and then adding things like "->
> > template(....)" actions to the relevant rules?
>
> I don't think that's a good approach because I think there is too much
> difference between the input and output. I think the rewrite thing is
> best used when you're outputting most of the input as-is. Maybe others
> will have a different opinion.
>
> I have to admit I haven't used StringTemplate yet. I need to learn that
> still.
>
> > For example, I'm inclined to add this to the grammar you provided...
> >
> > ifStatement
> >         : 'IF' e1=condition 'THEN' e2=statement ('ELSE' e3=statement)?
> 'END IF'
> >         -> template(e1={$e1.text},e2={$e2.text}) " IF(<e1>,<e2>,<e3>) "
> >         ;
>
> Hmm ... that looks much nicer than my string concatenation!
>
> > I started going down the rewrite road with mixed success and I'm not
> > sure it is the right road.
> >
> > Regards,
> > Frank Font
> >
> > PS - Not familiar with the "com.ociweb.accounting" package.  Do I really
> > need that installed?
>
> That's just the Java package name I selected. The generated classes
> come out with
>
> package com.ociweb.accounting.
>
> You should change that to be a reasonable Java package name for your
> company.
>
> > Mark Volkmann wrote:
> > > Here's a start on your grammar.
> > >
> > > grammar Accounting;
> > >
> > > options {
> > >   output = template;
> > > }
> > >
> > > @lexer::header { package com.ociweb.accounting; }
> > > @parser::header { package com.ociweb.accounting; }
> > >
> > > start: ifStatement EOF;
> > >
> > > ifStatement
> > >   : 'IF' condition 'THEN' statement ('ELSE' statement)? 'END IF';
> > >
> > > comparison: expression RELATIONAL_OPERATOR expression;
> > >
> > > condition: comparison (LOGICAL_OPERATOR comparison)*;
> > >
> > > expression
> > >   : STRING_LITERAL
> > >   | value (SIGN value)*;
> > >
> > > statement: expression | ifStatement;
> > >
> > > value: NAME | NUMBER;
> > >
> > > LOGICAL_OPERATOR: 'AND' | 'OR';
> > >
> > > RELATIONAL_OPERATOR: '<' | '<=' | '=' | '>=' | '>';
> > >
> > > APOSTROPHE: '\'';
> > >
> > > NUMBER: INTEGER | FLOAT;
> > > fragment FLOAT: INTEGER '.' NATURAL_NUMBER;
> > > fragment INTEGER: SIGN? NATURAL_NUMBER;
> > > fragment NATURAL_NUMBER: '0' | '1'..'9' '0'..'9'*;
> > > SIGN: '+' | '-';
> > >
> > > NAME: LETTER (LETTER | NUMBER | '_')*;
> > >
> > > STRING_LITERAL: APOSTROPHE NONCONTROL_CHAR* APOSTROPHE;
> > >
> > > WHITESPACE: (NEWLINE | SPACE)+ { $channel = HIDDEN; };
> > >
> > > // Note that NONCONTROL_CHAR does not include the double-quote
> character.
> > > fragment NONCONTROL_CHAR: LETTER | DIGIT | SYMBOL | SPACE;
> > > fragment LETTER: LOWER | UPPER;
> > > fragment LOWER: 'a'..'z';
> > > fragment UPPER: 'A'..'Z';
> > > fragment DIGIT: '0'..'9';
> > > fragment NEWLINE: '\r'? '\n';
> > > fragment SPACE: ' ' | '\t';
> > >
> > > // Note that SYMBOL does not include the
> > > // apostrophe or double-quote characters.
> > > fragment SYMBOL: '!' | '#'..'&' | '('..'/' | ':'..'@' | '['..'`' |
> '{'..'~';
> > >
> > > It parses your example input except for I changed "H-CAPRESTATE" to
> > > "H_CAPRESTATE" to simply things. This way I can use "-" for
> > > subtraction. I'm sure there's a way to work this out so you can also
> > > use "-" in names.
> > >
> > > The grammar above doesn't output anything. It just verifies that input
> conforms.
> > >
> > > Email me privately if you want the Java code I wrote that uses the
> > > generated classes and my Ant build file.
> > >
> > > On Jan 17, 2008 3:02 PM, Mark Volkmann <r.mark.volkmann at gmail.com>
> wrote:
> > >
> > >> On Jan 17, 2008 2:04 PM, Frank Font <mrfont at room4me.com> wrote:
> > >>
> > >>> Hello,
> > >>>
> > >>> I purchased the book, read through it, but I have a thick skull.
> > >>> Perhaps I can get some advice here on two questions about converting
> > >>> formula expressions that look like "basic" syntax...
> > >>>
> > >>> IF REP_DTE > '2001-01-01' AND ATOTAL>100 THEN
> > >>>     H-CAPRESTATE
> > >>> ELSE
> > >>>     IF REP_DTE < '2001-01-01' THEN
> > >>>         ACCTG_CNG + ACCTG_ERR_CRCT
> > >>>     END IF
> > >>> END IF
> > >>>
> > >>> Into a flat format that looks like Excel formula syntax...
> > >>>
> > >>> IF( AND(REP_DTE > '2001-01-01',ATOTAL>100), H-CAPRESTATE, IF(REP_DTE
> <
> > >>> '2001-01-01', ACCTG_CNG + ACCTG_ERR_CRCT))
> > >>>
> > >>> I tried writing a few grammar files, but all have had various
> runtime
> > >>> issues.
> > >>>
> > >>> Here are my questions...
> > >>>
> > >>> 1.  Is ANTLR the right tool for this job?  (I don't have much time.)
> > >>>
> > >> ANTLR can definitely do this. However, you shouldn't expect the work
> > >> to go quickly if this is your first ANTLR grammar. You'll be learning
> > >> lots of things along the way.
> > >>
> > >>
> > >>> 2.  If it is the right tool, is there already a grammar that will
> get me
> > >>> most of the way there?
> > >>>
> > >> I'm not aware of a particular existing grammar that is close to what
> > >> you want. Maybe someone else knows of one.
> > >>
> > >>
> > >>
> > >>> Thanks in advance for any advice.
> > >>>
> > >>> Regards,
> > >>> Frank Font
> > >>>
> > >> --
> > >> R. Mark Volkmann
> > >> Object Computing, Inc.
> > >>
> > >>
> > >
> > >
> > >
> > >
> >
> >
>
>
>
> --
> R. Mark Volkmann
> Object Computing, Inc.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080118/6671a3fa/attachment.html 


More information about the antlr-interest mailing list