Chapter 5:Semantic Analysis

Attribute Grammar Framework

. Generalization of CFG where each grammar symbol has an associated set of attributes

. Values of attributes are computed by semantic rules

. Two notations for associating semantic rules with productions

- Syntax directed definition

. high level specifications

. hides implementation details

. explicit order of evaluation is not specified

- Translation schemes

. indicate order in which semantic rules are to be evaluated

. allow some implementation details to be shown

An attribute grammar is the formal expression of the syntax-derived semantic checks associated with a grammar. It represents the rules of a language not explicitly imparted by the syntax. In a practical way, it defines the information that will need to be in the abstract syntax tree in order to successfully perform semantic analysis. This information is stored as attributes of the nodes of the abstract syntax tree. The values of those attributes are calculated by semantic rule.

There are two ways for writing attributes:

1) Syntax Directed Definition : It is a high level specification in which implementation details are hidden, e.g., $$ = $1 + $2; /* does not give any implementation details. It just tells us. This kind of attribute equation we will be using, Details like at what point of time is it evaluated and in what manner are hidden from the programmer.*/

2) Translation scheme : Sometimes we want to control the way the attributes are evaluated, the order and place where they are evaluated. This is of a slightly lower level.