Docsity

Semantic Analysis of Compilers: Understanding Syntax and Semantic Errors, Papers of Computer Science

Partial preview of the text

Download Semantic Analysis of Compilers: Understanding Syntax and Semantic Errors and more Papers Computer Science in PDF only on Docsity!

Compilers CS414-2008S-

Semantic Analysis

David Galles Department of Computer ScienceUniversity of San Francisco

Syntax Errors/Semantic Errors A program has

syntax

errors if it cannot be

generated from the Context Free Grammar whichdescribes the language The following code has no

syntax

errors, though it

has plenty of

semantic

errors:

void main() {

if (3 + x - true)

x.y.z[3] = foo(z);

} Why don’t we write a CFG for the language, so thatall syntactically correct programs also contain nosemantic errors?

JavaCC & CFGs JavaCC allows actions – arbitrary Java code – inrules We could use JavaCC rules to do type checking Why don’t we?

JavaCC & CFGs JavaCC allows actions – arbitrary Java code – inrules We could use JavaCC rules to do type checking Why don’t we?^ JavaCC files become very long, hard to follow,hard to debug^ Not good software engineering – trying to dotoo many things at once

Semantic Errors Semantic Errors can be classified into the followingbroad categories: Definition Errors Most strongly typed languages require variables,functions, and types to be defined before they areused with some exceptions –^ Implicit variable declarations in Fortran^ Implicit function definitions in C

Semantic Errors Semantic Errors can be classified into the followingbroad categories: Structured Variable Errors^ x.y = A[3]

x needs to be a class variable, which has aninstance variable y A needs to be an array variable x.y[z].w = 4 x needs to be a class variable, which has aninstance variable y, which is an array of classvariables that have an instance variable w

Semantic Errors Semantic Errors can be classified into the followingbroad categories:^ Type Errors^ Build-in functions – /, *,

||, &&, etc. – need to be

called with the correct types^ In simpleJava, +, -, *, / all take integers^ In simpleJava,

||^ &&,! take booleans

Standard Java has polymorphic functions &type coercion

Semantic Errors Semantic Errors can be classified into the followingbroad categories:^ Type Errors^ Assignment statements must have compatibletypes^ When are types compatible?

06-11:

Semantic Errors Semantic Errors can be classified into the followingbroad categories:^ Type Errors^ Assignment statements must have compatibletypes

In C, types must have the same structure Coerceable types also apply

struct {

struct {

int x;

int z;

char y;

char x;

} var1;

} var2;

Semantic Errors Semantic Errors can be classified into the followingbroad categories:^ Type Errors^ Assignment statements must have compatibletypes

In Object oriented languages, can assignsubclass value to a superclass variable

Environment Much of the work in semantic analysis is managingenvironments Environments store current definitions:^ Names (and structures) of types^ Names (and types) of variables^ Names (and return types, and number andtypes of parameters) of functions As variables (functions, types, etc) are declared,they are added to the environment. When avariable (function, type, etc) is accessed, itsdefinition in the environment is checked.

06-15:

Environments & Name Spaces Types and variables have different name spaces insimpleJava, C, and standard Java:

simpleJava:class foo {

int foo;

} void main() {

foo foo;foo = new foo();foo.foo = 4;print(foo.foo);

06-17:

Environments & Name Spaces Types and variables have different name spaces insimpleJava, C, and standard Java:

Java:class EnviornTest {

static void main(String args[]) {

Integer Integer = new Integer(4);System.out.print(Integer);}

Environments & Name Spaces Variables and functions in C share the same namespace, so the following C code is

not

legal:

int foo(int x) {return 2 * x;} int main() {int foo;printf("%d\n",foo(3));return 0;} The variable definition

int foo;

masks the

function definition for

foo