I’m missing some basic knowledge. Started playing around with ATLR today missing any source telling me how to do the following:
I’d like to parse a configuration file a program of mine currently reads in a very ugly way. Basically it looks like:
A [Data] [Data]
B [Data] [Data] [Data]
where A/B/… are objects with their associated data following (dynamic amount, only simple digits).
A grammar should not be that hard but how to use ANTLR now?
- lexer only: A/B are tokens and I ask for the tokens he read. How to ask this and how to detect malformatted input?
- lexer & parser: A/B are parser rules and… how to know the parser processed successfully A/B? The same object could appear multiple times in the file and I need to consider every single one. It’s more like listing instances in the config file.
Edit:
My problem is not the grammer but how to get informed by parser/lexer what they actually found/parsed? Best would be: invoke a function upon recognition of a rule like recursive descent
ANTLR production rules can have return value(s) you can use to get the contents of your configuration file.
Here’s a quick demo:
If you runt the class below:
the following will be printed to the console:
{A=[12, 34], B=[5, 6, 7, 8], C=[9]}