Antlr grammar generates invalid C # code
I am trying to develop a C # code generator using ANTLR and the StringTemplate library. AntlrWorks can generate C # parser files and lexer files without reporting any errors. However, the C # parser code is invalid 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' ;
This results in the following lines of code in the generated parameter_def ()
List<object> list_p = null;
...snipped some code
if (list_p==null) list_p=new List<StringTemplate>();
This results in a List <StringTemplate>
type assignment error List<Object>
. The grammar works before I add the string template rules. The error occurs when I add the syntax (p+=param)
required to process the list in the StringTemplate library.
I'll add my StringTemplate file for completeness, but I don't think it might cause an error as it doesn't load 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>
}
>>
Input string example PARAMETERS( D,B,T)
Antlr versions
- Antlr3.Runtime 3.4.1.9004
- AntlrWorks 1.4.3
source to share