[antlr-interest] Would antlr be suited for this task?

Andy Tripp antlr at jazillian.com
Fri Oct 10 14:01:51 PDT 2008


Gary,

I don't think ANTLR can help you much with this.
If you do want to parse this input and create an AST
(and perhaps then walk the AST), then ANTLR is the best tool for that
(assuming there isn't an already-existing ANTLR-like tool that parses
E/SQL).

But I think 99% or more of your work will be in doing that mapping from
E/SQL to Pro*C. There's no tool that's going to make that easier.

Take your strcpy/strcat code as an example. The parsing is pretty trivial -
you parse a strcpy() call, a strcat() call, and perhaps a few calls.
That might be a day or two's work at most, using ANTLR or not.
All the work is then in figuring out how to map those calls to
their replacements. The issue there is how general it needs to be.
For example, if you just look for a single strcpy() call followed by zero or
more strcat() calls, that's easy. But what if you have
the following code:

strcpy(UpdSqlStr, "UPDATE" " ");   // 2 strings instead of 1
strcpy(somethingElse, "whatever");  // an completely unrelated statement
MY_STRCAT(UpdSqlStr, HELLO);   // MY_STRCAT is some macro or your own strcpy() wrapper
sprintf(UpdSqlStr, "%s SET (", UpdSqlStr);  // sprintf instead of strcat
strcat(UpdSqlStr, foo());      // the arg is not just a fixed string
if (a) {                       // control flow analysis needed to figure out "bar" vs. "bar2"
 strcat(UpdSqlStr, "bar");
} else {
 strcat(UpdSqlStr, "bar2");
}

...you get the idea. This looks like a case where automating it might look easy,
but end up taking far longer than doing it by hand.

That's my two cents,
Andy

Gary Rosales wrote:
> Hi,
> 
>  
> 
> I am a newbie with ANTLR but I have a question that it’s been itching me 
> to ask for a while. I am in a project where we are migrating a bunch of 
> c code that has embedded SQL (E/SQL from Informix) to c code that has 
> also embedded SQL but in another format (Pro*C from Oracle). The 
> Informix c code (ec files) is full of non-ANSI 92 SQL that needs to be 
> converted, this code is inside strcpy, sprintf, strcat, and so on. For 
> instance, code fragments like:
> 
>  
> 
> strcpy(sqlstr, “select x, y, z from t1, outer t2, outer t3 where 
> t1_t2.id = t2_id  and t2_t3.id = t3.id”);
> 
>  
> 
> would be translated to
> 
>  
> 
> strcpy(sqlstr, “select x,y,z from t1 left join t2 on t1_t2.id = t2_id 
>  left join t3 on t2_t3 = t3.id “);
> 
>  
> 
> and
> 
>  
> 
>   strcpy(UpdSqlStr, "UPDATE ");
> 
>   strcat(UpdSqlStr, HELLO);
> 
>   strcat(UpdSqlStr, " SET (");
> 
>   strcat(UpdSqlStr, "foo,");
> 
>   strcat(UpdSqlStr, "bar");
> 
>   strcat(UpdSqlStr, ") = (");
> 
>  
> 
>   strcpy(sqlcomstr, UpdSqlStr);
> 
>   strcat(sqlcomstr, ":p_var,");            
> 
>   strcat(sqlcomstr, “:p_var”);       
> 
>   strcat(sqlcomstr, " WHERE hello.name = :p_var");
> 
>  
> 
> would be :
> 
>  
> 
>   strcpy(UpdSqlStr, "UPDATE ");
> 
>   strcat(UpdSqlStr, HELLO);
> 
>   strcat(UpdSqlStr, " SET foo =:p_var, bar=:p_var ”);
> 
>   strcat(_UpdSqlStr, _" WHERE hello.name =:p_var ”);__
> 
>  
> 
>  
> 
> there are many fragments like this (but they are longer and more complex).  
> 
> There is already a tool that does this that it’s provided by Oracle, but 
> it’s incomplete because it doesn’t work on cases like the example I just 
> gave. So we do use the tool; the tool takes care of other embedded sql 
> that it’s not within strings variables but, would it be possible to 
> create a compiler to transform the dynamic sql that it’s generated in 
> the variables. There are many files that use definitions on other files 
> (macros and such) that are used  for the business logic in the dynamic 
> sql, so the compiler would have to link the files and do semantic 
> checking. Is this feasible? The approach we have right now is using a 
> custom framework with match and replace files for the source, it’s quite 
> tedious and I ‘d love to hear of a better way to do this. I would think 
> it’s possible but I’d rather ask people who have much more experienced 
> than me with compiler creation and ANTLR.
> 
>  
> 
> Thank you for your help.
> 
>  
> 
> Gary Rosales
> 
> 
> ------------------------------------------------------------------------
> 
> 
> 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