1

I am making a IEC-61131 to C++ converter, which is being done using C#. I have a good Antlr4 grammar file for IEC-61131 and I am using a custom Listener to generate the output C++ code. I know StringTemplate C# could be used to replace my custom Listener. But I cannot find a good example how to use the StringTemplate CodeGenerator and select the CPP Target Language. Can anyone provide direction here? Or provide an example? Here is code I have so far.

            private void Button_Click(object sender, RoutedEventArgs e)
    {
        AntlrInputStream input = new AntlrInputStream(this.ViewModel.stText);
        STLexer lexer = new STLexer(input);
        lexer.RemoveErrorListeners();
        lexer.AddErrorListener(new ThrowExceptionErrorListener());
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        STParser parser = new STParser(tokens);
        IParseTree tree = parser.program();

        if (parser.NumberOfSyntaxErrors > 0)
        {
            Build.Text = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz") + "\r" + parser.NumberOfSyntaxErrors.ToString() + " Build Error";
            CEditor.Text = "";
        }
        else
        {
            var listener = new ExtractInterfaceListener(parser);
            ParseTreeWalker walker = new ParseTreeWalker();
            walker.Walk(listener, tree);
            CEditor.Text = listener.code;
            Build.Text = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz") + "\r" + "No Errors";
        }
    }
C James
  • 11
  • 2
  • What "CPP Target Language" are you talking about? StringTemplate doesn't have anything like it. And the purpose of Antlr's C++ target is to generate parsers in C++. That's all it does, and it won't magically become a general-purpose transpiler that you can bind your grammar to. – Yegor Feb 27 '18 at 18:14
  • Thanks for responding to this, but I solved this a long time ago. My goal was to make a IEC-61131 to C++ converter. I was using a Visual Studio program written in C#. It took me a while to figure out String Template, but once I did it really took off. So my entire project does several conversion IEC-61131 to C++, then C++ to ARM machine code. Now I have my own PLC programming dropping directly onto a ARM processor. – C James Feb 28 '18 at 22:46

0 Answers0