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

Des Hartman des at deshartman.com
Mon Feb 23 18:28:04 PST 2009


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090224/7793a870/attachment.html 


More information about the antlr-interest mailing list