English 中文(简体)
antlr v3 contextaware Country comment inclusion
原标题:antlr v3 context-aware conditional comment inclusion

I m modifying a DSL grammar for a product that is in public use. Currently all /*...*/ comments are silently ignored, but I need to modify it so that comments that are placed before certain key elements are parsed into the AST. I need to maintain backwards compatibility whereby users can still add comments arbitrarily throughout the DSL and only those key comments are included.

目前,教区认为:

grammar StateGraph;
graph:  graph  ID  {  graph_body  } ;
graph_body: state+;
state:  state  ID  {  state_body  } ;
state_body: transition* ...etc...; 
transition:  transition  (transition_condition) ID  ; ;
COMMENT:  /*  ( options {greedy=false;} : . )*  */  {skip();}

Comments placed before the graph and state elements contain meaningful description and annotations and need to be included within the parsed AST. So I ve modified those two rules and am no longer skipping COMMENT:

graph: comment*  graph  ID  {  graph_body  } ;
state: comment*  state  ID  {  state_body  } ;
COMMENT:  /*  ( options {greedy=false;} : . )*  */ 

If I naively use the above, the other comments cause mismatched token errors when subsequently executing the tree parser. How do I ignore all instances of COMMENT that are not placed in front of graph or state ?

一个例子是:

/* Some description
 * @some.meta.info
 */
graph myGraph {
  /* Some description of the state.
   * @some.meta.info about the state
   */
  state first {
    transition if (true) second; /* this comment ignored */
  }

  state second {
  }

  /* this comment ignored */
}
最佳回答

This is the solution I ve actually got working. I d love feedback.

The basic idea is to send comments to the HIDDEN channel, manually extract them in the places where I want them, and to use rewrite rules to re-insert the comments where needed. The extraction step is inspired by the information here: http://www.antlr.org/wiki/pages/viewpage.action?pageId=557063.

图表目前为:

grammar StateGraph;

@tokens { COMMENTS; }

@members {
// matches comments immediately preceding specified token on any channel -> ^(COMMENTS COMMENT*)
CommonTree treeOfCommentsBefore(Token token) {
    List<Token> comments = new ArrayList<Token>();
    for (int i=token.getTokenIndex()-1; i >= 0; i--) {
       Token t = input.get(i);
       if (t.getType() == COMMENT) {
          comments.add(t);
       }
       else if (t.getType() != WS) {
          break;
       }
    }
    java.util.Collections.reverse(comments);

    CommonTree commentsTree = new CommonTree(new CommonToken(COMMENTS, "COMMENTS"));
    for (Token t: comments) {
       commentsTree.addChild(new CommonTree(t));
    }
    return commentsTree;
}
}

graph
    :  graph  ID  {  graph_body  } 
      -> ^(ID {treeOfCommentsBefore($start)} graph_body);
graph_body: state+;
state
    :  state  ID  {  state_body  } 
      -> ^(ID {treeOfCommentsBefore($start)} staty_body);
state_body: transition* ...etc...; 
transition:  transition  (transition_condition) ID  ; ;
COMMENT:  /*  .*  */  {$channel=HIDDEN;}
问题回答

你们是否做了这项工作?

grammar StateGraph;
graph:  graph  ID  {  graph_body  } ;
graph_body: state+;
state: .COMMENT  state  ID  {  state_body  } ;
state_body: .COMMENT transition* ...etc...; 
transition:  transition  (transition_condition) ID  ; ;
COMMENT:  /*  ( options {greedy=false;} : . )*  */  {skip();}

我如何忽视没有排在图表或国家前面的所有言论?

检查<>后<> 评注的结尾<代码>*/“,>有:graph ,有> 或 前面的国家,其中有一些任择空间。 如果情况如此,就没有做任何事情,如果情况并非如此,那么,通过<>>/em>获得的排名即告失败。 该规则仅限skip()

在荷兰皇家骑士团,希望:

COMMENT
 :  /*  .*  */  ( (SPACE* (GRAPH | STATE))=> /* do nothing, so keep this token */ 
                | {skip();}                  /* or else, skip it               */ 
                )
 ;

GRAPH  :  graph ;
STATE  :  state ;
SPACES : SPACE+ {skip();};

fragment SPACE :     |  	  |  
  |  
 ;

注:* and .+ are ungreedy byault: no need to set options{greedy=false;}

并且知道你没有在<条码>上使用。 自<代码>SPACES>> 采用<代码>skip() 方法,时称!





相关问题
ANTLR parser hanging at proxy.handshake call

I am attempting to get a basic ECMAScript parser working, and found a complete ANTLR grammar for ECMAScript 3, which appears to compile ok and produces the appropriate Lexer/Parser/Walker Java files. (...

Will ANTLR Help? Different Suggestion?

Before I dive into ANTLR (because it is apparently not for the faint of heart), I just want to make sure I have made the right decision regarding its usage. I want to create a grammar that will parse ...

How to use ANTLR to parse xml document

can anybody tell how to use ANTLR tool(in java) to create our own grammar for xml documents and how to parse those documents using ANTLR tool(in java)?

JavaCC Problem - Generated code doesn t find all parse errors

Just started with JavaCC. But I have a strange behaviour with it. I want to verify input int the form of tokens (letters and numbers) wich are concatenated with signs (+, -, /) and wich can contain ...

How to generate introductory recognizer using ANTLR3C?

The Definitive ANTLR Guide starts with a simple recognizer. Using grammar verbatim to target C-runtime fails because %s means something to ANTLR: $ cat T.g grammar T; options { language = ...

What s the matter with this Grammar?

grammar Test; IDHEAD: ( a .. z | A .. Z | _ ); IDTAIL: (IDHEAD | 0 .. 9 ); ID: (IDHEAD IDTAIL*); fragment TYPE: ( text | number | bool ); define: define ID as TYPE; The problem ...

热门标签