[antlr-interest] FW: CSharp Target...

Kris Erickson kris at storefront.com
Fri Jun 13 12:19:35 PDT 2008


First of all, let me state that Antlr appears to be an awesome tool.
AntlrWorks is amazing, being able to debug, combining the lexer and
parser, and having a parser for walking the trees.  The Antlr book is
fantastic, the website is at time frustrating, but still and excellent
reference and the short period of time I have spent with the project has
made me start considering DSL's for all sorts of purposes.  However,
trying to work in Csharp has been a bit of a nightmare.  

Initially I was trying to work in Antlr 3.1b1, but getting random errors
that simply did not occur when debugging the parser in Java (as well as
endless compile errors when even attempting to create a debug, but
compiling the "Simple tree-based interpreter" (the first one)
http://www.antlr.org/wiki/display/ANTLR3/Simple+tree-based+interpeter

With the following changes:

4a5
>     language=CSharp2;
13a15,18
> @header {
> using System.Collections.Generic;
> }
> 
16c21
<     List<CommonTree> functionDefinitions = new
ArrayList<CommonTree>();
---
>     public List<CommonTree> functionDefinitions = new 
> List<CommonTree>();
32c37
< 	  functionDefinitions.add($func.tree);
---
> 	  functionDefinitions.Add($func.tree);
68c73
< WS  :   (' '|'\t')+ { skip(); }
---
> WS  :   (' '|'\t')+ { Skip(); }

Results in:

C:\svnprojects\Expression\Expression>java org.antlr.Tool -debug Expr1.g
ANTLR Parser Generator  Version 3.1b1 (May 20, 2008)  1989-2008
error(10):  internal error: Expr1.g : java.util.NoSuchElementException:
no such attribute: n in template context [output File parser
genericParser(...) region__genericParser__members
region__genericParser__members if(grammar.grammarIsRoot)_s ubtemplate
else_subtemplate ctorForRootGrammar createListenerAndHandshake
if(PARSER)_subtemplate]
org.antlr.stringtemplate.StringTemplate.checkNullAttributeAgainstFormalA
rguments(StringTemplate.java:1290)
org.antlr.stringtemplate.StringTemplate.get(StringTemplate.java:814)
org.antlr.stringtemplate.StringTemplate.get(StringTemplate.java:812)
org.antlr.stringtemplate.StringTemplate.get(StringTemplate.java:812)
org.antlr.stringtemplate.StringTemplate.get(StringTemplate.java:812)
org.antlr.stringtemplate.StringTemplate.get(StringTemplate.java:812)
org.antlr.stringtemplate.StringTemplate.get(StringTemplate.java:812)
org.antlr.stringtemplate.StringTemplate.get(StringTemplate.java:812)
org.antlr.stringtemplate.StringTemplate.get(StringTemplate.java:812)
org.antlr.stringtemplate.StringTemplate.get(StringTemplate.java:812)
org.antlr.stringtemplate.StringTemplate.getAttribute(StringTemplate.java
:696)
org.antlr.stringtemplate.language.ActionEvaluator.attribute(ActionEvalua
tor.java:360)
org.antlr.stringtemplate.language.ActionEvaluator.expr(ActionEvaluator.j
ava:136)
org.antlr.stringtemplate.language.ActionEvaluator.action(ActionEvaluator
.java:84)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:148)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:714)
org.antlr.stringtemplate.language.ConditionalExpr.writeSubTemplate(Condi
tionalExpr.java:152)
org.antlr.stringtemplate.language.ConditionalExpr.write(ConditionalExpr.
java:103)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:714)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:715)
org.antlr.stringtemplate.language.ASTExpr.writeAttribute(ASTExpr.java:65
2)
org.antlr.stringtemplate.language.ActionEvaluator.action(ActionEvaluator
.java:86)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:148)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:714)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:715)
org.antlr.stringtemplate.language.ASTExpr.writeAttribute(ASTExpr.java:65
2)
org.antlr.stringtemplate.language.ActionEvaluator.action(ActionEvaluator
.java:86)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:148)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:714)
org.antlr.stringtemplate.language.ConditionalExpr.write(ConditionalExpr.
java:126)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:714)
org.antlr.stringtemplate.language.ConditionalExpr.writeSubTemplate(Condi
tionalExpr.java:152)
org.antlr.stringtemplate.language.ConditionalExpr.write(ConditionalExpr.
java:103)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:714)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:715)
org.antlr.stringtemplate.language.ASTExpr.writeAttribute(ASTExpr.java:65
2)
org.antlr.stringtemplate.language.ActionEvaluator.action(ActionEvaluator
.java:86)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:148)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:714)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:715)
org.antlr.stringtemplate.language.ASTExpr.writeAttribute(ASTExpr.java:65
2)
org.antlr.stringtemplate.language.ActionEvaluator.action(ActionEvaluator
.java:86)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:148)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:714)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:715)
org.antlr.stringtemplate.language.ASTExpr.writeAttribute(ASTExpr.java:65
2)
org.antlr.stringtemplate.language.ActionEvaluator.action(ActionEvaluator
.java:86)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:148)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:714)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:715)
org.antlr.stringtemplate.language.ASTExpr.writeAttribute(ASTExpr.java:65
2)
org.antlr.stringtemplate.language.ActionEvaluator.action(ActionEvaluator
.java:86)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:148)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:714)
org.antlr.codegen.CodeGenerator.write(CodeGenerator.java:1271)
org.antlr.codegen.Target.genRecognizerFile(Target.java:94)
org.antlr.codegen.CodeGenerator.genRecognizer(CodeGenerator.java:455)
org.antlr.Tool.generateRecognizer(Tool.java:419)
org.antlr.Tool.process(Tool.java:286)
org.antlr.Tool.main(Tool.java:71)

C:\svnprojects\Expression\Expression>

Turning off debug, it compiles but now when it runs, it gets a null
exception in Expr1Parser.cs

char_literal19=(IToken)Match(input,13,FOLLOW_13_in_expr319);
char_literal19_tree = (CommonTree)adaptor.Create(char_literal19);
root_0 = (CommonTree)adaptor.BecomeRoot(char_literal19_tree, root_0);

Because char_literal19 is null.  Input looks good to me: {3+4\n}, so I
don't know why Match is returning null...

Compiling with 3.0.1 works fine.  So I think, great, I'll use 3.0.1,
however trying to run --debug with this.  But I get the same error when
debugging any tree grammer:

In the prog

	int LA1_0 = input.LA(1);

Message: 	"Object reference is not set to an instance of an
object."
Source:  	"Antlr3.Rutime"
StackTrace: "   at Antlr.Runtime.Debug.DebugTreeNodeStream.LA(Int32
i)\r\n   at Eval1.prog() in
C:\\svnprojects\\Expression\\Expression2\\Eval1.cs:line 184\r\n   at
ConsoleApplication1.Program.Main(String[] args) in
C:\\svnprojects\\Expression\\Expression2\\Program.cs:line 22\r\n   at
System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)\r\n
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)\r\n   at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)\r\n
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)\r\n   at
System.Threading.ThreadHelper.ThreadStart()"
TargetSite:  {Int32 LA(Int32)}

So, I was wondering:

1) Is there any version of Antlr with CSharp that run with debug (in the
tree grammar)?
2) Where can I find the sources to the 3.0.1 CSharp runtime so I can
figure out why the DebugTreeNodeStream fails?  I tried finding it at
FishEye but downloading the source from Aug 2007 results only in the
DLL's.
3) Is there any examples of Antlr with CSharp for any projects that use
a Tree Grammar?
4) If the 3.1.b1 is the only version of Antlr that supports the CSharp2
target, and the CSharp2
   target is so broken it won't run against a simple grammar like the
"Simple Tree based interpreter",
   should the all the Wiki examples point to the CSharp2 grammar?  I
spent many hours thinking my
   own grammar had some weird errors because I was getting NULL
exceptions only to find out that
   the 3.1.b1 with CSharp2 is very broken.

Kris


More information about the antlr-interest mailing list