Java Reference
In-Depth Information
Functions add() and lookup() are auxiliary functions defined for adding variable definitions
into a context (symbol table) and for looking up a variable in a given context. In the above
attribute grammar, the attribute context is synthesized in localVariableDeclarationState-
ment and is inherited in primary.
Attribute grammars might also be used for specifying the code to be produced for (or,
at least the mathematical meaning of) the various syntactic forms in our source language.
For better or worse, attribute grammars have never found their way into the practice of
compiler writing, but have rather been an academic tool for specifying the formal semantics
of programming languages.
4.9 Further Readings
The Java Language Specification [Gosling et al., 2005] is the best source of information on
the semantic rules for the Java language.
Attribute grammars were first introduced in [Knuth, 1968]. Chapter 3 of [Slonneger and
Kurtz, 1995] offers an excellent treatment of attribute grammars; Chapter 7 discusses the
application of attribute grammars in code generation. A general compilers text that makes
extensive use of attribute grammars is [Waite and Goos, 1984]. Chapter 5 of [Aho et al.,
2007] also treats attribute grammars.
[Gamma, 1995] describes the Visitor Pattern as well as many other design patterns.
Another good text describing how design patterns may be used in Java programming is [Jia,
2003]. Yet another resource for design patterns is [Freeman et al., 2004].
4.10 Exercises
The j-- compiler does not enforce all of the Java rules that it might. The following exercises
allow the reader to rectify this.
Exercise 4.1. The compiler does not enforce the Java rule that only one type declaration
(that is, class declaration in j--) be declared public. Repair this in one of the analyze()
methods.
Exercise 4.2. The analyze() method in JVariable assigns the type Type.ANY to a
JVariable (for example, x ) in the AST that has not been declared in the symbol table.
This prevents a cascading of multiple errors from the report of a single undeclared variable.
But we could go further by declaring the variable (as Type.ANY ) in the symbol table so as
to suppress superfluous error reports in subsequent encounters of the variable. Make this
improvement to analysis.
Exercise 4.3. Go through The Java Language Specification [Gosling et al., 2005], and for
each node type in the j-- compiler, compile a list of rules that Java imposes on the language
but are not enforced in j--.
a. Describe how you might implement each of these.
b. Which of these rules cannot be enforced?
 
Search WWH ::




Custom Search