0
votes

How to write an antlr4 grammar lexer rule to not match a string. For example, I have the following input string :

CREATE TABLE person 
( age integer,
  id integer,
  name character varying(30)),
  PRIMARY KEY ( id )
);

Here, I need to skip those create table queries like above which contains "PRIMARY KEY" constraint.

Can we use regular expressions directly in lexer rules ?

1
lexer rules are regex, well more powerful but still regex.Terence Parr

1 Answers

0
votes

Write a couple of rules for SQLs which are required and not required.

goodSQL:
    'CREATE' 'TABLE' Id '('
    Id typeDef (',' Id typeDef)? ','
    ')'
;

badSQL:
    'CREATE' 'TABLE' Id '('
    Id typeDef (',' Id typeDef)? ','
    'PRIMARY' 'KEY' keyDef
    ')' ->skip
;

This is something to start with. You have to define Id, typeDef and keyDef after this. Adding ->skip will not parse SQL statements that match the rule.

Good Luck!