darenn
darenn

Reputation: 479

Skipping unmatched input in Antlr

Is there a way to specify in a grammar that I want to skip all input that doesnt match any of the rules (that would otherwise throw a recognition error)?

Upvotes: 1

Views: 1265

Answers (1)

GRosenberg
GRosenberg

Reputation: 5991

Yes. Implementation depends on where you need/want to do the skipping.

In the lexer, a last rule like:

Unknown : . -> skip ; // or -> channel(HIDDEN) ;

will consume any otherwise unmatched input characters yet keep them from being tokenized and considered by the parser. You do want to match a single character at a time so that at every input text index all other lexer rules have a chance to match first.

Similarly, in the parser, a last rule like:

ignored : . ;

will consume unmatched tokens, creating parse tree nodes, each as a context containing a single 'ignored' token. Their presence in the parse-tree can then be, well, ignored.

Again, the ignored rule match should be for just a single token, ensuring that all other longer match rules have priority, and last in the ordering of the rules, ensuring that all other single token match rules are considered first.

Upvotes: 3

Related Questions