[antlr-interest] Question on embedding other languages (grammars)

Dean Sheehan dean.sheehan at me.com
Sun Jun 24 10:55:58 PDT 2012

Hi ANTLR Interest,

I was wondering how I would go about embedding one language within another.

For example, if I have a grammar developed for language X but I want to be able to embed segments of another language that I do not want to be parsed or processed by ANTLR at this stage, basically just receive a string of unprocessed text for passing off to something else to look at, how might I go about that?

Consider the classic case of wanting to embedded some SQL in a piece of Javav code (just an example). I have the grammar for Java and all of the parsing and processing behind that but I also have a SQL interpreter. I want to be able to write something like:

void foo ( ) {
	int a = 5;
	sql {
		insert into table x ...
	} sql

The 'sql {' and '} sql' start and end tokens would be part of the 'extended' Java grammar but the stuff between is just text that I want to extract to pass off to a SQL interpreter.

Obviously I could do simple things like define a grammar than has 'sql {' STRING '} sql' but that wouldn't account for the STRING having newlines, white space etc.

Thanks for any pointers.

- Dean

More information about the antlr-interest mailing list