1

I'm trying to generate LaTex for PGFPlots graphs using Antlr from a C# project.

So far, I've created this grammar for the axis, which is essentially a set of kvps:

grammar PgfPlots;

axis
    : 'axis' '[' keyValPairs ']'
    ;

keyValPairs
    : (keyValPair)+
    ;

keyValPair
    : Key '=' Value
    ;

Key
    : [a-zA-Z_][a-zA-Z_0-9]*
    ;

Value
    : INT
    | FLOAT
    | STRING
    ;

INT
    : [0-9]+
    ;

FLOAT
    : [0-9]+ '.' [0-9]+
    ;

STRING
    : '"' .*? '"'
    ;

WS
    : [ \t\r\n]+ -> skip
    ;

However, I can't for the life of me work out how to actually populate the AST.

So far, I've tried this:

    public string GenerateSourceCode(PgfPlotDefinition plotDefinition)
    {
        PgfPlotsParser.AxisContext axis = new(null, 0);

        List<KeyValuePair<string, object>> notNullProperties = GetNotNullProperties(plotDefinition.AxisDefinition);

        for (int i = 0; i < notNullProperties.Count; i++)
        {
            KeyValuePair<string, object> kvp = notNullProperties[i];
            axis.keyValPairs().keyValPair().SetValue($"{kvp.Key} = {kvp.Value}", i);
        }

        return axis.GetText();
    }

However, this gives me a null reference exception on the line where I'm trying to assign the kvp to the array, which I guess makes sense, because I've not created it to set the value... I just have no idea how I'm actually meant to do that bit?

ScottishTapWater
  • 3,656
  • 4
  • 38
  • 81
  • @BartKiers - There is no input, I'm trying to build the AST programmatically and then generate the source from that... Could well be I'm missing something but everything related to Antlr is already in the question – ScottishTapWater Dec 24 '22 at 12:36
  • Since ANTLR version 4 you build AST using either a listener or visitor pattern. See https://tomassetti.me/listeners-and-visitors/ for an example. – Pavel Ganelin Dec 24 '22 at 13:43
  • 1
    ANTLR is a tool for accepting an input stream of characters and producing a ParseTree (not technically an AST). It's not intended for building up a tree programmatically, nor does it have the capability to serialize a ParseTree back out. If I'm reading your question right, ANTLR is the opposite of the tool you're looking for (apparently, the ability to programmatically construct an AST and then write that out as LaTex) – Mike Cargal Dec 25 '22 at 17:41
  • @MikeCargal - Huh... I thought it could go both ways... Do you know of a tool that does what I want? – ScottishTapWater Dec 26 '22 at 16:50
  • 1
    You might find StringTemplate (https://www.stringtemplate.org) useful. It's not quite as simple as defining a grammar and getting source code out, but it's what ANTLR uses for code generation, and I've used it for generating output for a compiler I wrote, it's pretty nice once you get the hang of it. – Mike Cargal Dec 26 '22 at 18:32
  • I'll take a look, cheers... As it stands, I'm thinking I might just build a tree and make use of the new raw strings to interpolate everything together recursively... – ScottishTapWater Dec 26 '22 at 19:14

0 Answers0