[antlr-interest] Seeking advice - 2 questions using ANTLR (can translate using rewrite?)
Frank Font
mrfont at room4me.com
Thu Jan 17 14:27:55 PST 2008
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?
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>) "
;
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?
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.
>>
>>
>
>
>
>
More information about the antlr-interest
mailing list