Add token image to node values automatically

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Add token image to node values automatically

Bauer, Thomas

Dear Per,

 

as to my understanding if I want to have my productions to have values I need to add those values in my token callbacks. For example as seen in ArithmeticCalculator.cs

        public override Node ExitAdd(Token node) {

            node.Values.Add("+");

            return node;

        }

Instead of this I could also write node.Values.Add(node.Image); right?

 

But … isn’t this some kind of default behaviour, to have those token image values as node values. I am wondering because now I need to override every token callback to add each token value to the node values list. And there are a lot of tokens. So I ask myself whether I misunderstood something or why isn’t this by default generated in Analyzer code or at least having an option provided to do so?

 

regards

Thomas


_______________________________________________
Grammatica-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/grammatica-users
Reply | Threaded
Open this post in threaded view
|

Re: Add token image to node values automatically

Per Cederberg
You've understood the default behavior correctly. Grammatica currently doesn't add any default node values.

But for tokens, there is often no need to add them as values. The Exit-methods can also access child nodes and retrieve values or token images there. And if your grammar is structured with separate productions for each alternative, you'll find that only tokens like IDENTIFIER or NUMBER needs special treatment. I.e. write productions like this:

  Statement = StatementOne
    | StatementTwo
    | StatementThree ;

  StatementOne = "if" IDENTIFIER "then" ... ;

  StatementTwo = "when" IDENTIFIER "do" ... ;

You'll then know exactly what to do in the Exit-methods for these productions. The ArithmeticCalculator code is perhaps a bit sloppy, since it uses the token images as operators (instead if proper constants or such).

Hard to be more specific without better examples. For a more realistic grammar that mostly follows this structure, check out the ASN.1 grammar here:
https://github.com/cederberg/mibble/blob/master/src/grammar/asn1.grammar

Cheers,

/Per



On Thu, May 30, 2013 at 3:57 PM, Bauer, Thomas <[hidden email]> wrote:

Dear Per,

 

as to my understanding if I want to have my productions to have values I need to add those values in my token callbacks. For example as seen in ArithmeticCalculator.cs

        public override Node ExitAdd(Token node) {

            node.Values.Add("+");

            return node;

        }

Instead of this I could also write node.Values.Add(node.Image); right?

 

But … isn’t this some kind of default behaviour, to have those token image values as node values. I am wondering because now I need to override every token callback to add each token value to the node values list. And there are a lot of tokens. So I ask myself whether I misunderstood something or why isn’t this by default generated in Analyzer code or at least having an option provided to do so?

 

regards

Thomas


_______________________________________________
Grammatica-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/grammatica-users



_______________________________________________
Grammatica-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/grammatica-users