ANTLR: removing clutter

2019-08-06 12:53发布

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?

标签: antlr antlr3
2条回答
做个烂人
2楼-- · 2019-08-06 13:16

In the upcoming ANTLR v4, you can do fuzzy parsing. take a look at

http://www.antlr.org/wiki/display/ANTLR4/Wildcard+Operator+and+Nongreedy+Subrules

You can get the beta software here:

http://antlr.org/download/antlr-4.0b3-complete.jar

Terence

查看更多
Root(大扎)
3楼-- · 2019-08-06 13:24

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:

//foo

should become:

/* foo */

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 //bar to be recognized as a single line comment in:

/*
//bar
*/

The same goes for string literals:

String s = "no // comment";

Finally, you should create some sort of catch-all rule in the lexer that will match any character.

A quick demo:

grammar T;

parse
 : (t=. {System.out.print($t.text);})* EOF
 ;

Str
 : '"' ('\\' . | ~('\\' | '"'))* '"'
 ;

MLComment
 : '/*' .* '*/'
 ;

SLComment
 : '//' ~('\r' | '\n')*
   {
     setText("/* " + getText().substring(2) + " */");
   }
 ;

Any
 : . // fall through rule, matches any character
 ;

If you now parse input like this:

//comment 1
class Foo {

  //comment 2

  /* 
   * not // a comment
   */
  String s = "not // a // comment"; //comment 3
}

the following will be printed to your console:

/* comment 1 */
class Foo {

  /* comment 2 */

  /* 
   * not // a comment
   */
  String s = "not // a // comment"; /* comment 3 */
}

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.

查看更多
登录 后发表回答