0

I am trying to use ANTLR to create understand how grammar works. I have started playing with ANTLR and created a simple addition program. Below is my simple grammar.

grammar addition;
expr: NUMBER PLUS NUMBER;
NUMBER: [0-9]+
PLUS: '+';
SPACE
  :  ' ' {skip();};

This grammar works perfect for input like "1+1". But what I am trying to do is to generate below C# code for input "1+1":

var a = 1;
var b = 1;
Console.WriteLine(1+1);

When I researched, I found that I can use string templates with ANTLR C#. I tried exploring them but didn't find much help. The documentation is a little hard to understand. Can somebody please tell me or provide some study references through which I will be able to know how to use string templates with ANTLR.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • (1) Read Parr "Enforcing strict model-view separation in template engines." Proceedings of the 13th international conference on World Wide Web. 2004. (2) Read https://github.com/antlr/stringtemplate4/blob/master/doc/introduction.md. (3) The Nuget library for StringTemplate is ancient, requires Antlr3. The source doesn't build in VS2019 or Net SDK. I brought it up to date here: https://www.nuget.org/packages/Domemtech.StringTemplate4/. (4) Ex: `using Antlr4.StringTemplate; ...Main(...){var t = new Template("your code etc"); t.Add("a", 1); t.Add("b", 2); Console.WriteLine(t.Render());}` – kaby76 Apr 29 '21 at 03:33

1 Answers1

0

Add Package StringTemplate4 from nuget:

 Install-Package StringTemplate4 -Version 4.0.8

A Minimal example:

public string SumNumbers()
        {
        //Create string template and enclose variables between <..>
            var template = @"
A template for sum (a,b):

a= <a>
b= <b>
Sum = <sum>  
";
          //Instantiate Template class  
            var strTemplate = new Template(template);
            int a = 5;
            int b = 6;
            var sum = a + b;
            
            //define variables used by template 
            strTemplate.Add("a", a);
            strTemplate.Add("b", b);
            strTemplate.Add("sum", sum);
            //render the template
            return strTemplate.Render();
        }

output:

A template for sum (a,b):

a= 5

b= 6

Sum = 11

you can find a detailed documentation in using StringTemplat4 in project site:

M.Hassan
  • 10,282
  • 5
  • 65
  • 84
  • Thank you for your response. But is there any way I can call template from grammar? . Basically I dont want to hardcode anything. – chinmaykelkar Apr 29 '21 at 04:33
  • StringTemplate is other product with different library/different objective( something like T4 template ) to generate code/text from template and substitute the corresponding runtime values . It can't be used with the grammar. – M.Hassan Apr 29 '21 at 04:53
  • ST supports "attribute.property" access in a template, which works just fine. You might be able to define property accessors over a tree node type, but it's not native. See https://stackoverflow.com/questions/31593038/antlr-4-and-stringtemplate-4-using-tree-walker-with-templates. In any case, you're going to need a driver program because ST is a library. But it shouldn't have to be. It should be a command-line program with JSON interface containing property maps so as to not require any programming, work more like Unix "sed". ST & Antlr are in "maintenance mode"--no new development. – kaby76 Apr 29 '21 at 15:37
  • Thank you for all your responses. What exactly I am looking for is for a grammar of addition(mentioned in the problem statement) if I give input as “1 + 1”, it should generate a C# code in console automatically with output of code as 2.(mentioned in the problem statement). using ANTLR, I have generated visitors, listeners etc. But I am not getting how to use those to create C# code. I hope I made sense. I also want to know if the my requirement is achievable using ANTLR. – chinmaykelkar Apr 29 '21 at 16:15