I want to build a parser for analyzing a large input file, but I don’t need the entire input file, only some parts of it.
For exmaple, the input file may look like this:
bla bla bla bla bla ...
EVENT: e1
type: t1
version: 1
additional-info: abc
EVENT: e2
type: t2
version: 1
uninteresting-info: def
blu blu blu blu blu ...
From this file, all I want is to have a map of event to type (e1=>t1, e2=>t2). All other information is of no interest for me.
How can I build a simple ANTLR grammar that does this?
You can do that by introducing a boolean flag inside your lexer that keeps track whether an
event– ortype-keyword has been encountered. If it has been encountered, the lexer should not skip the word, all other words should be skipped.A small demo:
You can test the parser with the following class:
which will produce the following output: