[antlr-interest] simple "find-and-replace"
Terence Parr
parrt at cs.usfca.edu
Mon Feb 23 18:34:23 PST 2009
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
More information about the antlr-interest
mailing list