[antlr-interest] Debuging C# language target with ANTLRWorks...

Lloyd Dupont ld at galador.net
Fri Jul 13 07:27:35 PDT 2007


Awesome, thanks!
I will look at that.. tomorrow (times to go to bed now, I reckon, mmhh. 
still debugging...).

----- Original Message ----- 
From: "Johannes Luber" <jaluber at gmx.de>
To: <antlr-interest at antlr.org>
Sent: Saturday, July 14, 2007 12:16 AM
Subject: Re: [antlr-interest] Debuging C# language target with ANTLRWorks...


> Lloyd Dupont wrote:
>> From the ANTLR book:
>> "Because ANTLRWorks communicates with running parsers via sockets, the
>> ANTLRWorks debugger works with any ANTLR language target"
>>
>> Now I wonder, how can I test my grammar in C#?
>> Currently I am running with dummay Java method, which I should replace
>> with C# code once it will work.
>> I tried to debug my C# grammar initially but it didn't work.
>>
>> I probably missed something, buty someone might know, what are the steps
>> to debug my C# Parser from ANTLRWorks?
>
> I've attached the source code of a file which handles remote debugging.
> Adapt the code to your situation, start the parser and start remote
> debugging in ANTLRworks.
>
> Best regards,
> Johannes Luber
>


--------------------------------------------------------------------------------


> namespace Kerriv.Antlr3ToRelaxNG {
> using Antlr.Runtime;
> using Antlr.Runtime.Debug;
> using Antlr.Runtime.Tree;
> using Antlr.StringTemplate;
> using Antlr.StringTemplate.Language;
> using System;
> using System.IO;
> using System.Xml.Serialization;
> using Kerriv.ANTRL3ToRelaxNG;
>
> public class ANTRL3ToRelaxNG {
> public static StringTemplateGroup templates;
> private static string inputFileName;
> private static string templateFileName;
> private static string ruleNamingFileName;
> private static string startRule;
> private static bool debugMode = false;
> private static bool parseTree = false;
>
> public static void Main(string[] args) {
> // Use a try/catch block for parser exceptions
> try {
> EvaluateCommandLineParameters(args);
>
> templates = new StringTemplateGroup(new StreamReader(templateFileName),
> typeof(AngleBracketTemplateLexer));
>
> ICharStream input = new ANTLRFileStream(inputFileName);
> ANTLR3ToRelaxNGLexer lexer = new ANTLR3ToRelaxNGLexer(input);
> CommonTokenStream tokens = new CommonTokenStream(lexer);
> ANTLR3ToRelaxNGParser parser;
> ParseTreeBuilder builder = new ParseTreeBuilder("ANTRL3ToRelaxNG");
> if (parseTree) {
> parser = null; //new ANTLR3ToRelaxNGParser(tokens, builder);
> } else {
> parser = new ANTLR3ToRelaxNGParser(tokens);
> }
>
> ANTLR3ToRelaxNGParser.grammarDef_return r = parser.grammarDef();
> CommonTree r0 = ((CommonTree) r.tree);
> if (parseTree) {
> ParseTree pt = builder.GetTree();
> } else {
> // Missing: Setting the start rule!
> CommonTreeNodeStream nodes = new CommonTreeNodeStream(r0);
> nodes.TokenStream = tokens;
> ANTLR3ToRelaxNGGenerator walker = new ANTLR3ToRelaxNGGenerator(nodes);
> walker.TemplateLib = templates;
> RuleReturnScope r1 = walker.grammarDef();
> Console.Out.WriteLine(r1.Template.ToString());
> }
> } catch (System.Exception e) {
> Console.Error.WriteLine("exception: " + e);
> Console.Error.WriteLine(e.StackTrace);
> }
> }
>
> /// <summary>
> /// This functions exits when invalidate parameters are given
> /// </summary>
> /// <param name="args"></param>
> private static void EvaluateCommandLineParameters(string[] args) {
> bool acceptArguments = false;
>
> if ((args.Length >= 2) && (args.Length <= 4)) {
> // Default template file
> templateFileName = "ANTRL3ToRelaxNG.stg";
>
> if (args.Length == 2) {
> inputFileName = args[0];
> startRule = args[1];
> acceptArguments = true;
> } else if (args.Length == 3) {
> // Either debug option or output template is argument 0
> if (args[0].Trim().ToLower() == "-debug") {
> debugMode = true;
> } else if (args[0].Trim().ToLower() == "-parseTree") {
> parseTree = true;
> } else {
> templateFileName = args[0];
> }
> inputFileName = args[1];
> startRule = args[2];
> acceptArguments = true;
> } else {
> templateFileName = args[1];
> inputFileName = args[2];
> startRule = args[3];
> if (args[0].Trim().ToLower() == "-debug") {
> debugMode = true;
> acceptArguments = true;
> }
> if (args[0].Trim().ToLower() == "-parseTree") {
> parseTree = true;
> acceptArguments = true;
> }
> }
> }
>
> if (acceptArguments) {
> // Ensure full pathnames
> if (!Path.IsPathRooted(templateFileName)) {
> templateFileName = Path.Combine(Environment.CurrentDirectory,
> templateFileName);
> }
> if (!Path.IsPathRooted(inputFileName)) {
> inputFileName = Path.Combine(Environment.CurrentDirectory, inputFileName);
> }
> // Derive ruleNamingFileName from inputFileName
> ruleNamingFileName = Path.GetDirectoryName(inputFileName) + 
> Path.PathSeparator +
> Path.GetFileNameWithoutExtension(inputFileName) + ".xml";
> } else {
> Console.Error.WriteLine("Usage: ANTLR3ToRelaxNG [-debug|-parseTree] 
> [<output-template-file>] <grammar-input-file> <start-rule>");
> Environment.Exit(1);
> }
> }
> }
> } 



More information about the antlr-interest mailing list