ANTLR: removing clutter

為{幸葍}努か 提交于 2019-12-11 06:54:57

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/13347997/antlr-removing-clutter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!