Start by separating your grammar into a separate lexer grammar and parser grammar. For example, if you have a grammar Foo;
, create the following:
Create a file FooLexer.g4, and move all of the lexer rules from Foo.g4 into FooLexer.g4.
Create a file FooParser.g4, and move all of the parser rules from Foo.g4 into FooParser.g4.
Include the following option in FooParser.g4:
options { tokenVocab=FooLexer; }
This separation will ensure that your parser isn't silently creating lexer rules for you. In a combined grammar, using a literal such as 'not'
in a parser rule will create a lexer rule for you if one does not already exist. When this happens, it's easy to lose track of what kinds of tokens your lexer is able to produce. When you use a separate lexer grammar, you will need to explicitly declare a rule like the following in order to use 'not'
in a parser rule.
NOT : 'not';
This should solve the problems with whitespace should you have included the literal ' '
somewhere in a parser rule.