1
$\begingroup$

I've read about attribute grammars, but I haven't found out, how are the attributes often implemented?

To me they scream objects (of classes), but they could be also variables.

But are there any general rules as to how the attributes should be implemented in a practical implementation? Or is it free form?

$\endgroup$
  • 2
    $\begingroup$ A question of the form "How is X implemented in the real world?" is usually offtopic here, unless there are conceptual hurdles. Are there? Community votes, please: is this offtopic? $\endgroup$ – Raphael Feb 9 '16 at 12:17
3
$\begingroup$

Attribute grammars are a conceptual tool. You are mixing this up with implementation details.

There are many parser generators around (check for example the Wikipedia pages on compiler compilers and comparison of parser generators). They all implement some flavor of attribute grammar, in actual use you aren't interested in a parse tree or a derivation. The most used ones are (direct descendants of) old designs, which predate the current object oriented craze, and write parsers implemented in languages like C, with no objects.

$\endgroup$
1
$\begingroup$

I do not know any specific implementations, but here are two ideas a annotate nodes w.r.t an attribute grammars:

  1. A recursive algorithm can evaluate top-down and bottom-up rules in two passes over the tree.

    You may want to implement it using the visitor pattern

  2. Use the iterator pattern.

    Note that the order of iteration is crucial -- decide between pre-, in- and post-order. In order to do emulate the recursive approach you need (at least) pre- and post-order, one after the other.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.