Formal Grammar for Java

This chapter presents an attribute grammar for the Java programming language (v. 1.1). This grammar is derived from the LALR grammar presented in the Java Language Specification (JLS) [1]. The purpose of this grammar is to formally specify not only the syntactic structure of Java programs, but also their static semantics. Specifically, in this chapter we try to formally capture all aspects of the language that would result in compile-time errors. These errors include, but are not limited to: -Type checking for assignment statements, ensuring that the type of the right-hand side of the statement is assignment compatible with the left hand side. -Type checking expression operands, ensuring that they are of compatible types. -Type checking method parameters, ensuring that they are the correct type and number. -Checking for duplicate variable and method names. -Checking for undefined variables.