Adrian
Adrian

Reputation: 769

ANTLR generating c++ code inside c# file for a given grammar

I'm trying to parse and execute a fairly simple Oracle SQL query in my C# application. For this, I decided to use ANTLR as a parser tool, since the grammar for the Oracle SQL language is already available for download from the grammar list page.

I've also picked up the latest version of ANTLR 3.4 and used the following command to generate C# source files to use in my application

java -cp antlr-3.4-complete.jar org.antlr.Tool OracleSQL.g

Small remark: the original grammar file had C as output language, so I've changed it to CSharp2 (I've also tried CSharp3, but with no luck).

The problem with the code is that I get blocks of code like this one

if ((((( !(strcasecmp((const char*)LT(1)->getText(LT(1))->chars, "SUBPARTITION")) )&&

therefore the solution doesn't compile.

I've also tried the solution posted here, but to no avail.

What am I missing?

Thanks, Adrian.

Upvotes: 1

Views: 438

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170148

Quite often, ANTLR grammars contain embedded (target) code. C code, in your case. You can't simply change the language=C; into language=CSharp2 (or another language): it will only cause ANTLR to generate a lexer and/or parser in C# with embedded C code. You'll have to rewrite all the embedded C code yourself.

For example, the following C targeted grammar:

grammar T;

options {
  language=C;
}

parse
 : Any* EOF {printf("done!\n");}
 ;

Any
 : .
 ;

should be rewritten (for C#) like this:

grammar T;

options {
  language=CSharp2; // or CSharp3
}

parse
 : Any* EOF {Console.WriteLine("done!");}
 ;

Any
 : .
 ;

Upvotes: 3

Related Questions