[antlr-interest] simple "find-and-replace"

Des Hartman des at deshartman.com
Tue Feb 24 14:58:45 PST 2009


Terrence

Had a look at what you have so far on ANTLRMorph. The problem I am also
going to have is that I need to output the result using ActionScript. All
the code is for a Flex page and thus only have ActionScript available. :-(

I had a look at a possible way a parser can do it, but it is ugly (see
below). Any other way to simply replace a recognize token with another token
and spit out the original input using ActionScript???

===================================

formula returns [String value]
    : (EQ)? e=expression { if($EQ.text==null) {$value = $e.value;} else
{$value = $EQ.text + $e.value;} }
    ;


expression returns [String value]
    : e=boolExpr {$value = $e.value;}
    ;
boolExpr returns [String value]
    : e=concatExpr {$value = $e.value;} (
    ( AND   { if($AND.text==null)   {$value += $e.value;} else {$value +=
$AND.text + $e.value;} }
    | OR    { if($OR.text==null)    {$value += $e.value;} else {$value +=
$OR.text + $e.value;} }
    | LT    { if($LT.text==null)    {$value += $e.value;} else {$value +=
$LT.text + $e.value;} }
    | LTEQ  { if($LTEQ.text==null)  {$value += $e.value;} else {$value +=
$LTEQ.text + $e.value;} }
    | GT    { if($GT.text==null)    {$value += $e.value;} else {$value +=
$GT.text + $e.value;} }
    | GTEQ  { if($GTEQ.text==null)  {$value += $e.value;} else {$value +=
$GTEQ.text + $e.value;} }
    | EQ    { if($EQ.text==null)    {$value += $e.value;} else {$value +=
$EQ.text + $e.value;} }
    | NOTEQ { if($NOTEQ.text==null) {$value += $e.value;} else {$value +=
$NOTEQ.text + $e.value;} }
    )
      e=concatExpr {$value += $e.value;}
    )*
    ;
concatExpr returns [String value]
    : e=sumExpr {$value = $e.value;}
    (
    CONCAT { if($CONCAT.text==null) {$value += $e.value;} else {$value +=
$CONCAT.text + $e.value;} }
    e=sumExpr {$value += $e.value;}
    )*

Thanks
Des



2009/2/24 Terence Parr <parrt at cs.usfca.edu>

> Leon and I are working on getting ANTLRMorph ready to release. Given a
> grammar and a script like you have below, it will do the transformations for
> you. Unfortunately it's not ready. probably another two, three weeks
> Ter
>
> On Feb 23, 2009, at 6:28 PM, Des Hartman wrote:
>
>  Hi,
>>
>> I'm looking for a simple "find-and-replace" like way to use ANTLR to
>> convert the following back and forth:
>>
>> "=SUM(A,5)"   ---->   "=SUM([3],5)"
>>
>> and
>>
>> "=SUM([3],5)"   ---->   "=SUM(A,5)"
>>
>> I have written a lexer/parser for a user entered formula. In the formula,
>> the user references columns like in Excel and the parser picks these up as
>> localReferences (LREF). I need to convert these local references into an
>> internal reference (IREF) to speed up recalculations in another part of the
>> program. I need to preserve the formula text to allow the user to come back
>> and edit it in the original local reference format.
>>
>> "=SUM(A,5)"   ---->   "=SUM([3],5)" - Internal reference has A in position
>> 3 in the internal Array.
>>
>> When the user edits this formula I need to reverse the "find-and-replace"
>> and convert IREF to LREF and present the formula back to the user
>>
>> "=SUM([3],5)"   ---->   "=SUM(A,5)" - Internal reference has A in position
>> 3 in the internal Array.
>>
>>
>> ==========
>>
>> I have been looking at StringTrees as a possible way to do this and have a
>> few questions about it:
>>
>> 1) Can I use it with ActionScript? I am using ActionScript for the
>> Lexer/Parser and Tree. I only see references to Java, C# and Python.
>> 2) Is there a simple way to do this? Maybe a separate parser file instead
>> of a more complex Tree?
>> 3) As part of the rewrite, I need to call a function that does the look up
>> to get [3] to "A" and back. Can this be inserted as part of the StringTree
>> rewrite?
>>
>> Here is the relevant parts of the parser
>>
>> operand
>>    : literal
>>    | localRefExpr             -> ^(LREF localRefExpr)
>>    | intRefExpr             -> ^(IREF intRefExpr)
>>    | LPAREN expression RPAREN     -> ^(expression)
>>    ;
>>
>> intRefExpr
>>    // ={5}. Used for internal reference
>>    : '[' NUMBER ']'
>>    ;
>>
>> localRefExpr
>>    // =A. Used for Formula editing to give Excel feel
>>    : NAME
>>    ;
>>
>> NAME     : LETTER*
>>    ;
>>
>> NUMBER    :
>>    ('0' .. '9')+ '.' ('0' .. '9')+ Exponent?
>>    |   '.' ( '0' .. '9' )+ Exponent?
>>    |   '0'..'9' ('0'..'9')* Exponent?
>>        ;
>>
>> fragment
>> Exponent
>>    :   ( 'e' | 'E' ) ( '+' | '-' )? ( '0' .. '9' )+
>>    ;
>>
>> fragment
>> LETTER
>>    : ('a'..'z') | ('A'..'Z')
>>
>> Thanks
>> Des
>>
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe:
>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090225/62f7f4d7/attachment.html 


More information about the antlr-interest mailing list