almostProgramming
almostProgramming

Reputation: 217

ANTLR Not Emitting Error Messages on Invalid Input

I have begun learning ANTLR in order to implement a domain-specific language (DSL) in the future. I have purchased The Definitive ANTLR Reference and have begun working my way through it in order to familiarize myself with the program and the concepts of languages/compiler design. I have successfully gotten everything working within my environment (Visual Studio 2010 and C#), and I have successfully been able to create some basic grammars, as described throughout the book. Source code for java can be found here: http://pragprog.com/titles/tpantlr/source_code

However, while working through Chapter 3, I have come across a problem involving the classes not emitting errors to the console, as it shows in the book. I am using the same grammar that is used in the book, so I am assuming it has something to do with the C# runtimes. I am currently using ANTLRWorks to generate the lexer/parser, and I am using the 3.4 ANTLR distribution runtimes for CSharp3 (Antlr3.Runtime.dll and Antlr4.StringTemplate.dll).

Is this a known issue? If not, should I try using an older version of the runtimes or ANTLRWorks?

The grammar I am using:

grammar Expr;

options
{
   language = CSharp3;
}

prog    :   stat+ ;

stat    :   expr NEWLINE
|   ID '=' expr NEWLINE
|       NEWLINE
;

expr    :   multExpr (('+'|'-') multExpr)*
;

multExpr 
:   atom('*' atom)*
;

atom    :   INT
|   ID
|   '(' expr ')'
;

ID  :   ('a'..'z'|'A'..'Z')+ ;
INT :   '0'..'9'+ ;
NEWLINE :   '\r'? '\n';
WS  :   (' '|'\t')+ {Skip();};

I will post the C# classes if anyone needs to see them, but they are very long, so I will hold off until someone requests them. Thank you.

Upvotes: 1

Views: 385

Answers (2)

pseudonym
pseudonym

Reputation: 41

After some debugging on 3.4.1.9004 source code, I found that the traceDestination property was null. So, I set the property as shown below in Main() and messages started showing up on the console.

parser.TraceDestination = Console.Out;

Upvotes: 4

Bart Kiers
Bart Kiers

Reputation: 170178

I've only worked with the CSharp2 target (and runtime) because I've not been able to get the CSharp3 target running on my Ubuntu-machine with MonoDevelop.

When you use ANTLRWorks, the org.antlr.Tool is used to generate the ExprLexer.cs and ExprParser.cs classes, which the CSharp3 runtime has (sometimes?) issues with, if memory serves me well.

Try generating the .cs source files with the Antlr3.exe tool instead: http://www.tunnelvisionlabs.com/downloads/antlr/antlr-dotnet-tool-3.4.1.9004.7z (note that tunnelvisionlabs.com is from the author of the CSharp3 target).

I haven't tested this, because of my unfamiliarity with the CSharp3 target, but it's worth a try.

Upvotes: 1

Related Questions