[antlr-interest] how to print tokens?

Nigel Sheridan-Smith nbsherid at secsme.org.au
Thu May 12 19:33:48 PDT 2005


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Michael West
> Sent: Friday, 13 May 2005 9:13 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] how to print tokens?
> 
> I have a thousand sql select statements and need to report on the tables
> and
> views they refer to.  Can antlr help me with this?  Should antlr help me
> with this?
> 
> Almost all of the sql is sqlserver2000.  The grammer at:
> http://www.antlr.org/grammar/1062280680642/MS_SQL_SELECT.html
> 
> seems to parse fine, and I can have it print out matches to simple leaf
> rules using element labels.  Can I do it with rule references?  I don't
> see how.
> 
> I think I would be happy if I could just print out all of the leaves
> attached to
> the tokens which match a rule.  It seems that AST is more suited to
> this, but this is not an AST grammer.
> 

You need to find the relevant rules, and add some actions, and probably some
return values... replace the following 3 rules, and tell us how it goes!


subTableSource
    :								{String
s=null;}
    (
      LPAREN (
            (joinedTables) => joinedTables RPAREN
          | (queryExpression) => queryExpression RPAREN alias1 // "derived
table", mandatory alias
          )
    | (function) => function (alias1)?
    | s=dbObject
{System.out.println(s);}
		(alias1)? ( (WITH)? LPAREN tableHint (COMMA tableHint)*
RPAREN )?
    | Variable (alias1)?
    | (CONTAINSTABLE | FREETEXTTABLE) LPAREN
        dbObject COMMA (dbObject | STAR) COMMA (stringLiteral | Variable)
(COMMA Integer)?
        RPAREN (alias1)?
    | COLON COLON function (alias1)? // built-in function
    )
    ;

dbObject returns [String s]
    // server.catalog.schema.object
    // server.catalog..object
    :
    (s=identifier | IDENTITYCOL | ROWGUIDCOL | keywordAsIdentifier) (
          DOT (identifier | IDENTITYCOL | ROWGUIDCOL | keywordAsIdentifier)
        | (DOT DOT) => DOT DOT (identifier | IDENTITYCOL | ROWGUIDCOL |
keywordAsIdentifier)
    )*
    ;

identifier returns [String s]
    :
      a:NonQuotedIdentifier			{s=a.getText();}
    | b:QuotedIdentifier			{s=b.getText();}
    ;

Nigel

--
Nigel Sheridan-Smith
PhD research student

Faculty of Engineering
University of Technology, Sydney
Phone: 02 9514 7946
Fax: 02 9514 2435





More information about the antlr-interest mailing list