Antlr 语法生成无效的 C# 代码 [英] Antlr grammar generating invalid C# code

查看:26
本文介绍了Antlr 语法生成无效的 C# 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ANTLR 和 StringTemplate 库开发 c# 代码生成器.AntlrWorks 可以生成 c# 解析器和词法分析器文件而不会报告任何错误.但是,c#解析器代码无效,无法在visual studio中编译.

I am trying to develop a c# code generator using ANTLR and the StringTemplate library. AntlrWorks can generate the c# parser and lexer files without reporting any errors. However, the c# parser code is not valid and cannot be compiled in visual studio.

谁能看出以下语法有什么问题?

Can anyone see what is wrong with the following grammar?

grammar StrucadShape;

options {
    language=CSharp3 ;
   output=template;  

}

@header {using System;}
@lexer::header {using System;} 
@lexer::members {const int HIDDEN = Hidden;}

/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/  

 public shapedef: parameters_def  
                  -> class_temp( parameters={$parameters_def.st} )
                  ;

 parameters_def : (PARAMETERS LPAREN (p+=param) (COMMA (p+=param))* RPAREN )
                  -> parameter_list(params={$p})
                  ;

param   : IDENTIFIER ->Parameter_decl(p={$IDENTIFIER.text});  

/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/    

fragment EOL:'\r'|'\n'|'\r\n'  ;  


 WS : (' ' 
| '\t' 
| EOL) 
{ $channel = HIDDEN; } ;    

 PARAMETERS: 'PARAMETERS';
   COMMA : ',' ;
   LPAREN : '(' ; 
   RPAREN : ')' ;  

fragment LETTER :('A'..'Z' | 'a'..'z');
IDENTIFIER: LETTER (LETTER|DIGIT)*;
INTEGER : (DIGIT)+ ;
FLOAT   : (DIGIT)+'.'(DIGIT)+;
fragment DIGIT  : '0'..'9' ;

这会在生成的 parameters_def() 方法中产生以下代码行

This results in the following lines of code in generated parameters_def() method

List<object> list_p = null;

...snipped some code

if (list_p==null) list_p=new List<StringTemplate>(); 

这在将 List 分配给类型 List 时失败.在我添加字符串模板规则之前,语法有效.当我在 StringTemplate 库中添加列表处理所需的 (p+=param) 语法时引入了该错误.

This is failing on the assignment of the List <StringTemplate> to type List<Object>. The grammar works before I add the string template rules. The error is introduced when I add the (p+=param) syntax required for list processing in the StringTemplate library.

为了完整起见,我将添加我的 StringTemplate 文件,但我认为这不会导致错误,因为它直到运行时才会加载.

I'll add my StringTemplate file for completeness, but I don't think this could be causing an error as it is not loaded until runtime.

group StrucadShape;

Parameter_decl(p)::= "public double <p> { get; set; }"

parameter_list(params) ::=
<<
start expressions
<params; separator="\n">
end
>>

class_temp(  parameters)::=
<<
public class name
{
    <parameters; separator="\n>  
}
>>

示例输入字符串 PARAMETERS( D,B,T)

Antlr 版本

  • Antlr3.Runtime 3.4.1.9004
  • AntlrWorks 1.4.3

推荐答案

我在 Antlr 邮件列表中发现了一个相关问题 此处.

I found a related issue on the Antlr mailing list here.

解决方案是在语法选项中添加一个 ASTLabeltype

The solution was to add an ASTLabeltype to the grammar options

options {
    language=CSharp3;   
    output=template;  
    ASTLabelType = StringTemplate;
}  

这篇关于Antlr 语法生成无效的 C# 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆