i’m learning ANTLR right now. Let’s say, I have a VHDL code and would like to do some processing on the PROCESS blocks. The rest should be completely ignored. I don’t want to describe the whole VHDL language, since I’m interested only in the process blocks. So I could write a rule that matches process blocks. But how do I tell ANTLR to match only the process block rule and ignore anything else?
Share
I know next to no VHDL, so let’s say you want to replace all single line comments in a (Java) source file with multi-line comments:
should become:
You need to let the lexer match single line comments, of course. But you should also make sure it recognizes multi-line comments because you don’t want
//barto be recognized as a single line comment in:The same goes for string literals:
Finally, you should create some sort of catch-all rule in the lexer that will match any character.
A quick demo:
If you now parse input like this:
the following will be printed to your console:
Note that this is just a quick demo: a string literal in Java could contain Unicode escapes, which my demo doesn’t support, and my demo also does not handle char-literals (the char literal
char c = '"';would break it). All of these things are quite easy to fix, of course.