[antlr-interest] V3: How to translate one language to another

Gyula László gyula.laszlo at profund.hu
Fri Dec 29 08:17:10 PST 2006


Hello,

On 2006.12.29., at 15:49, Bill Mayfield wrote:

>
> I have the grammer for my parser figured out and I can build an AST  
> that represents the language but I'm kind of STUCK on what to do  
> next. I need to semantically validate the query; but how? Do I do  
> this in a treewalker? Do I need a symbol table? And how can I  
> translate the query into SQL statements? Do I use templates?  
> TemplateString?
>

I think you should write two tree parsers, each using the same grammar:

one does nothing else, but validates the statements
the other uses stringTemplate to generate the statements.


An example, which might be buggy :) :

	selectStatement
		: ^('select' (a+=selectedFields)+ 'from' objectName)
		;

A long example for the first parser:

	@members {
		ISQLTableObject currentObject;
		DelegateCollection delegates;
	}

	selectStatement
		: ^('select' (a+=selectedFields)+ 'from' objectName)
		{
			SelectedStaementChecker selectStatementChecker =
				delegates.getCheckerDelegateByName("selectStatement");
			
			selectStatementChecker.checkFieldNameASTListForObject 
(currentObject, $a);
			selectStatementChecker.checkObjectNameAST($objectName);

			selectStatementChecker.assertIfStatementInvalid();
		}
		;

A shorter example for the second:
	
	selectStatement
		: ^('select' (a+=selectedFields)+ 'from' objectName)
			-> selectStatementTemplate(fields={toTemplates($a)}, objectName= 
{$objectName.st})
		;

and in "selectStatementTemplate.st":

	SELECT <fields; separator=","> FROM <objectName>;


I hope it helps...
Cheers,

Gyula László

email:gyula.laszlo AT profund.hu
http://profund.hu


>
> BTW: I've decided to use ANTLR v3

Wise choice :)




More information about the antlr-interest mailing list