grammatica-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Grammatica-users] Add token image to node values automatically


From: Per Cederberg
Subject: Re: [Grammatica-users] Add token image to node values automatically
Date: Thu, 30 May 2013 17:06:39 +0200

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 <address@hidden> 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
address@hidden
https://lists.nongnu.org/mailman/listinfo/grammatica-users



reply via email to

[Prev in Thread] Current Thread [Next in Thread]