|
1.5 Semantic Analysis and Semantic Analyzer Generators
The input to a semantic analyzer generator is often an attribute grammar. Attribute grammars are a common specification language for semantic analysis. Because an attribute grammar is a context-free grammar with the addition of equations called semantic functions, the input in Figure 5 is shown going to both the parser generator an the semantic analyzer generator. The context-free grammar is input to the parser generator, and the semantic functions are input to the semantic analyzer generator. The semantic analysis phase of a compiler performs many diverse tasks, rather than a single task as in scanning and parsing. Many, many variables called attributes are needed to describe these tasks. In Example 5 we show only two attributes, Use and Def, which describe information about the use and definition of the variables used in the program. This information can be used to finish parsing, perhaps by warning the user that a variable is used before it is defined. This information can also be used by the optimization phase of the compiler, perhaps by eliminating a variable that was defined but never used. Example 5 shows an attribute grammar for the assignment statement grammar. Example 6 shows the results produced by the generated semantic analyzer generator.
|