简体   繁体   中英

ANTLR Filtering Grammar for specific tokens and ignore everything else possible?

I am currently trying to create a SQL-grammar for the Data Definition Language. For my program, the parser only needs to recognize some specific sql-commands like "CREATE TABLE", "ALTER TABLE", etc. Since I am working with automatically generated export files there is also a lot of overhead in the things I am gonna parse like "SET CURRENT PATH" etc. This is not necessary to be parsed and I am wondering if there is a way to ignore "everything else" that is not defined in the SQL-Statements. Hope anyone has some experience with this..

Here's the header part of my grammar:

list: sql_expression ENDOFFILE?;

sql_expression: 
    ((create_statement|alter_table_statement|create_unique_index_statement|insert_statement) SEMICOLON)+    
;

...

and I am wondering if it is possible to extend the sql_expression rule like this:

list: sql_expression ENDOFFILE?;

sql_expression: 
    ((create_statement|alter_table_statement|create_unique_index_statement|insert_statement|else_stuff) SEMICOLON)+ 
;

Thanks in advance!

Yes you can achieve this. You can ignore statements like "SET CURRENT PATH" or "CONNECT ..blah blah". These are nothing but SQL plus commands. You need to swallow everything which comes after particular keyword. For eg , in case of "ACCEPT ..blah.." , you can create following rule:

SQL_PLUS_ACCEPT
:   'accept' SPACE ( ~('\r' | '\n') )* (NEWLINE|EOF)
;
accept_key
:   SQL_PLUS_ACCEPT
;

this will ignore "ACCEPT.. " command and u can parse whatever stmt u wnat to parse. You need to do this for other sql plus commands like SET, CONNECT, EXIT etc. You can refer to this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM