渲染模板时ANTLR StringTemplate无限循环

渲染模板时ANTLR StringTemplate无限循环,antlr,stringtemplate,Antlr,Stringtemplate,我使用的是antlr-3.4-complete.jar,我相信它使用的是StringTemplate版本3.2.1 我在树语法中有以下结果 functionCall : ^(FUNCCALL NCName pr+=params*) ->template(n={$NCName.text},p={$pr})"<n> <p>" 为上述产品生成的代码如下所示 Exception in thread "main" java.lang.StackOverflowErro

我使用的是antlr-3.4-complete.jar,我相信它使用的是StringTemplate版本3.2.1

我在树语法中有以下结果

functionCall 
  : ^(FUNCCALL NCName pr+=params*) ->template(n={$NCName.text},p={$pr})"<n> <p>"
为上述产品生成的代码如下所示

Exception in thread "main" java.lang.StackOverflowError
    at org.antlr.stringtemplate.StringTemplate.getAttributeRenderer(StringTemplate.java:1080)
    at org.antlr.stringtemplate.StringTemplate.getAttributeRenderer(StringTemplate.java:1080)
    at org.antlr.stringtemplate.StringTemplate.getAttributeRenderer(StringTemplate.java:1080)
    at org.antlr.stringtemplate.StringTemplate.getAttributeRenderer(StringTemplate.java:1080)
    at org.antlr.stringtemplate.StringTemplate.getAttributeRenderer(StringTemplate.java:1080)
    at org.antlr.stringtemplate.StringTemplate.getAttributeRenderer(StringTemplate.java:1080)
    at org.antlr.stringtemplate.StringTemplate.getAttributeRenderer(StringTemplate.java:1080)
    at org.antlr.stringtemplate.StringTemplate.getAttributeRenderer(StringTemplate.java:1080)
    at org.antlr.stringtemplate.StringTemplate.getAttributeRenderer(StringTemplate.java:1080)
    at org.antlr.stringtemplate.StringTemplate.getAttributeRenderer(StringTemplate.java:1080)
    at org.antlr.stringtemplate.StringTemplate.getAttributeRenderer(StringTemplate.java:1080)
    at org.antlr.stringtemplate.StringTemplate.getAttributeRenderer(StringTemplate.java:1080)
    at org.antlr.stringtemplate.StringTemplate.getAttributeRenderer(StringTemplate.java:1080)
    at org.antlr.stringtemplate.StringTemplate.getAttributeRenderer(StringTemplate.java:1080)
    at org.antlr.stringtemplate.StringTemplate.getAttributeRenderer(StringTemplate.java:1080)
retval.st = new StringTemplate(templateLib, "<a> <n> <pc>",new STAttrMap().put("a", (axisSpecifier14!=null?axisSpecifier14.st:null)).put("n", (nodeTest15!=null?nodeTest15.st:null)).put("pc", list_pred));
模式的输出:

Exception in thread "main" java.lang.IllegalStateException: infinite recursion to <anonymous([])@76> referenced in <anonymous([])@69>; stack trace:  
<anonymous([])@76>, attributes=[predexp=<anonymous()@75>], references=[predexp, stppred]>  
<anonymous([])@69>, attributes=[ndtst=<anonymous()@68>, stppred, axs=<anonymous()@67>], references=[axs, ndtst, stppred]>  
<anonymous([])@70>, attributes=[stp=<anonymous()@69>], references=[stp, stppred]>  
<anonymous([])@71>, attributes=[stps=List[..<anonymous()@70>..]], references=[stps, stppred]>  
<anonymous([])@72>, attributes=[rlp=<anonymous()@71>], references=[rlp, stppred]>  
<anonymous([])@73>, attributes=[mnexpr=<anonymous()@72>], references=[mnexpr, stppred]>  
<anonymous([])@75>, attributes=[e21=<anonymous()@74>, e11=<anonymous()@73>],   references=[e11, stppred]>  
<anonymous([])@76> (start of recursive cycle)  
线程“main”java.lang.IllegalStateException中的异常:中引用的无限递归;堆栈跟踪:
,attributes=[predexp=],references=[predexp,stpred]>
,attributes=[ndtst=,stpred,axs=],references=[axs,ndtst,stpred]>
,属性=[stp=],引用=[stp,STPRED]>
,属性=[stps=List[..]],引用=[stps,stppred]>
,attributes=[rlp=],references=[rlp,stpred]>
,属性=[mnexpr=],引用=[mnexpr,STPRED]>
,attributes=[e21=,e11=],references=[e11,stpred]>
(递归循环的开始)

ANTLR v3.4使用ST v4进行热电联产,但为了向后兼容,生成的代码使用ST v3.2.1


您已经在其内部嵌入了一个模板。打开lint模式以在模板嵌套图中查找无限循环。

ANTLR 3.4不是使用StringTemplate v4吗?你可能会想问你的问题:特伦斯时不时地漫游,但是这里没有很多的斯特林模板手(现在!):)祝你好运@巴特基尔斯,谢谢你的回答。我打印变量
StringTemplate.VERSION
的值,得到3.2.1,所以我说它使用3.2.1。此外,我为3.2.1下载的源代码在调试时能够与代码匹配。@BartKiers问题出在StringTemplate.toString()中的某个地方,调试此方法的问题在于,当我试图查找错误源时,每当我检查任何StringTemplate的值时,调试器都会在内部调用
toString()
在我完成toString()的步骤之前:)啊,我很快在3.4 JAR中看到了一个
org/stringtemplate/v4
路径,但是
stringtemplate.VERSION
似乎非常确定:)。我帮不上忙:不太熟悉ST…嗨,Terr,非常感谢,先生,谢谢你花时间回答。但是我的StringTemplates遵循与CombinedGrammar和TreeGrammar相同的模式。顶层是
Expr
,它下面一行包含一个
谓词
,它本身可以是一个
Expr
。TreeGrammar与Grammar相结合可以支持这一点。我可以和树妈妈一起在树上散步。但是当我引入StringTemplates时,我得到了这个错误。是,my ST for
谓词调用ST for
Expr
。我使用了lint模式,并在lint输出中得到了相同的结果。这是否意味着ST不能支持这一点。或者我不应该把ST看作是一种语法……嗨,我在上面的主要问题中添加了带ST的TreeGrammar。你能看一下并提出修改意见吗谢谢,有没有可能因为我使用的是匿名模板(“lambda”生成的模板),所以出现了这个错误?我将尝试使用“命名”模板,并让您知道结果。嗨,Terr,它现在工作正常。我将ST定义从TreeGrammar移到了一个单独的stg文件中,并从TreeGrammar调用了这些ST,现在一切正常:)非常感谢您关注我的推文并花时间回答这个问题。
expr  
   :  ^('<' e1=expr e2=expr) ->template(e11={$e1.st},e21={$e2.st})"\< <e11> <e21>"  
   | mainexpr -> template(mnexpr={$mainexpr.st})"<mnexpr>"  
   ;  

mainexpr  
scope  
{  
  boolean isRLP ;  
} :   
    filterExpr ('/' {$mainexpr::isRLP = true;} relativeLocationPath)?   
     -> {$mainexpr::isRLP}? template(filtr={$filterExpr.st},rlp=  {$relativeLocationPath.st})"<filtr> <rlp>"  
     -> template(filtr={$filterExpr.st})"<filtr>"  
  | relativeLocationPath -> template(rlp={$relativeLocationPath.st})"<rlp>"  
  ;  

relativeLocationPath : ^(STEPS st+=steps+) -> template(stps={$st})"<stps>";  

steps  
  :   ^(STEP step) ->template(stp={$step.st})"<stp>"    
  ;  
step   
  :  axisSpecifier nodeTest (pred+=predicate)*  
           ->template(axs={$axisSpecifier.st},ndtst={$nodeTest.st},stppred={$pred})"<axs> <ndtst> <stppred>"  
  ;  

predicate  
  : ^(PRED expr) ->template(predexp={$expr.st})"<predexp>"  
  ;  
Exception in thread "main" java.lang.IllegalStateException: infinite recursion to <anonymous([])@76> referenced in <anonymous([])@69>; stack trace:  
<anonymous([])@76>, attributes=[predexp=<anonymous()@75>], references=[predexp, stppred]>  
<anonymous([])@69>, attributes=[ndtst=<anonymous()@68>, stppred, axs=<anonymous()@67>], references=[axs, ndtst, stppred]>  
<anonymous([])@70>, attributes=[stp=<anonymous()@69>], references=[stp, stppred]>  
<anonymous([])@71>, attributes=[stps=List[..<anonymous()@70>..]], references=[stps, stppred]>  
<anonymous([])@72>, attributes=[rlp=<anonymous()@71>], references=[rlp, stppred]>  
<anonymous([])@73>, attributes=[mnexpr=<anonymous()@72>], references=[mnexpr, stppred]>  
<anonymous([])@75>, attributes=[e21=<anonymous()@74>, e11=<anonymous()@73>],   references=[e11, stppred]>  
<anonymous([])@76> (start of recursive cycle)