ContextfreeGrammar

Using circular attributes to compute nullable, first, and follow.

This is an example of a JastAdd project that makes heavy use of circular attributes. A language "CFG" is defined that allows context-free grammars to be written on a typical Backus-Naur Form. JastAdd aspects with circular attributes are used for defining the following properties:

The definitions of nullable(), first(), and follow() are natural to express using mutually recursive equations. For example, the value of nullable() for a nonterminal depends on the value of nullable() for other nonterminals, possibly including itself. Similarly for the first() set which in addition depends on nullable(). And similarly for the follow() set which in addition depends on first() and nullable(). These equations can be mapped in a straight-forward way to circular attributes.

To make use of circular attributes, their values should be arranged in a lattice of finite height, and all equations monotonic, i.e., such that they can only raise the attribute to a higher level in the lattice. The computation of the attribute can then be performed with a terminating iteration. The nullable() attribute makes use of a boolean lattice with FALSE at the bottom and TRUE at the top. Equations can only raise the value from the starting value FALSE.

The first() and follow() attributes make use of set lattices with the empty set at the bottom and the set of all terminal symbols at the top. The equations can only raise the value to sets including more terminal symbols.

The implementation also makes use of a reference attribute decl() which binds uses of nonterminals (i.e., nonterminal occurrences in the right-hand side of productions) to declarations of the corresponding nonterminal (i.e., nonterminal occurrences in the left-hand side of a production). The decl() attribute is used for propagating information about nonterminal declarations directly to their uses.

For a more detailed discussion of the example, see the article "Circular Reference Attributed Grammars - Their Evaluation and Applications".

Contents of this directory