01.03.2015 Views

Modern compiler design [PDF]

Modern compiler design [PDF]

Modern compiler design [PDF]

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7<br />

Semantic Analysis in C<br />

Chapter Overview<br />

7.1 Types in simpleJava<br />

7.1.1 Built-in Types<br />

7.1.2 Array Types<br />

7.1.3 Class Types<br />

7.2Implementing Environments in C<br />

7.3 Writing a Suite of Functions for Semantic Analysis<br />

7.3.1 Analyzing a simpleJava Program<br />

7.3.2 Analyzing simpleJava Expressions in C<br />

7.3.2 Analyzing Variables Expressions in C<br />

7.3.3 Analyzing simpleJava Statements in C<br />

7.4 Semantic Analyzer Project in C<br />

7.4.1 Project Definition<br />

7.4.1 Type Rules For Statements<br />

7.4.1 Type Rules For Expressions<br />

7.4.2 Project Difficulty<br />

7.4.3 Project “Gotcha”s<br />

7.4.4 Provided Files<br />

7.1 Types in simpleJava<br />

First, we will consider the implementation of types for simpleJava in C. A header file that defines the internal<br />

representation of types is in Figure 7.1.<br />

7.1.1 Built-in Types<br />

For the build-in types integer and boolean, we do not need to keep track of any extra information. The<br />

constructors for the base types are thus extremely simple – just IntegerType(), BooleanType(), and Void-<br />

Type(). To make our life a little easier, multiple calls to each of these constructors will always return the<br />

same pointer, so we can use pointer comparisons to check for type equivalence. That is, in the following<br />

code:<br />

type t1,t2;<br />

t1 = IntegerType();<br />

t2 = IntegerType();<br />

if (t1 == t2) {<br />

...<br />

}<br />

49

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!