pocky0719
pocky0719

Reputation: 19

Is there a natural mechanism to implement synthesized attributes or inherited attributes in an LR(1) parser?

I think it is simple to implement the synthesized attributes in LR(1) parser. Becuase it is also Bottom-up.

But how to implement inherited attributes in LR(1) parser?

And for comparing those two things in LR(1). For both synthesized attributes and inherited attributes in LR(1). Can you give me an example?

Upvotes: 2

Views: 108

Answers (1)

Ira Baxter
Ira Baxter

Reputation: 95400

To do arbitrary attribute grammars, you need need to build a full tree, because synthesized attributes from "right" children may be used as inherited attributes by "left" children of a node.

So you can't do the evaluation as you parse; you need the full AST. (If you are willing to limit inherited attributes only to "right" children, you could evaluate attributes as you parse).

Once you decide to capture the full AST before running the evaluation, it doesn't matter what your parsing technology is, as long as it produces the AST.

You can see what such attibute grammars look like for a production program transformation tool that uses AG results. The parser is a GLR parser, which is a generalization of LR parsing.

Upvotes: 1

Related Questions