Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Divide your grammar into several <a href="http://www.antlr.org/wiki/display/ANTLR3/Composite+Grammars" rel="nofollow">composite grammars</a>. Be careful what you place where. For example, you don't want to place the <code>NAME</code> rule in you top-grammar and keywords into an imported grammar: the <code>NAME</code> would "overwrite" the keywords from being matched.</p> <p>This works:</p> <h2>A.g</h2> <pre><code>lexer grammar A; SELECT: 'SELECT'; SET: 'SET'; SMALLINT: 'SMALLINT'; TABLE: 'TABLE'; THEN: 'THEN'; TINYINT: 'TINYINT'; UPDATE: 'UPDATE'; USE: 'USE'; VALUES: 'VALUES'; VARCHAR: 'VARCHAR'; WHEN: 'WHEN'; WHERE: 'WHERE'; QUOTED: '\'' ('\'\'' | ~'\'')* '\''; EQUALS: '='; NOT_EQUALS: '!='; SEMICOLON: ';'; COMMA: ','; OPEN: '('; CLOSE: ')'; VARIABLE: '@' NAME; NAME: ( LETTER | '#' | '_' ) ( LETTER | NUMBER | '#' | '_' | '.' )* ; NUMBER: DIGIT+; fragment LETTER: 'a'..'z' | 'A'..'Z'; fragment DIGIT: '0'..'9'; SPACE : ( ' ' | '\t' | '\n' | '\r' )+ { skip(); } ; </code></pre> <h2>SqlServerDialectLexer.g</h2> <pre><code>lexer grammar SqlServerDialectLexer; import A; AND: 'AND'; BIGINT: 'BIGINT'; BIT: 'BIT'; CASE: 'CASE'; CHAR: 'CHAR'; COUNT: 'COUNT'; CREATE: 'CREATE'; CURRENT_TIMESTAMP: 'CURRENT_TIMESTAMP'; DATETIME: 'DATETIME'; DECLARE: 'DECLARE'; ELSE: 'ELSE'; END: 'END'; FLOAT: 'FLOAT'; FROM: 'FROM'; GO: 'GO'; IMAGE: 'IMAGE'; INNER: 'INNER'; INSERT: 'INSERT'; INT: 'INT'; INTO: 'INTO'; IS: 'IS'; JOIN: 'JOIN'; NOT: 'NOT'; NULL: 'NULL'; NUMERIC: 'NUMERIC'; NVARCHAR: 'NVARCHAR'; ON: 'ON'; OR: 'OR'; </code></pre> <p>And it compiles fine:</p> <pre><code>java -cp antlr-3.3.jar org.antlr.Tool SqlServerDialectLexer.g javac -cp antlr-3.3.jar *.java </code></pre> <p>As you can see, invoking the <code>org.antlr.Tool</code> on your "top-lexer" is enough: ANTLR automatically generates classes for the imported grammar(s). If you have more grammars to import, do it like this:</p> <pre><code>import A, B, C; </code></pre> <h2>EDIT</h2> <p>Gunther is correct: changing the <code>QUOTED</code> rule is enough. I'll leave my answer though, because when you're going to add more keywords, or add quite a few parser rules (inevitable with SQL grammars), you'll most probably stumble upon the "code too large" error again. In that case, you can use my proposed solution.</p> <p>If you're going to accept an answer, please accept Gunther's.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload