COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our...

98
COMP3012/G53CMP: Lecture 5 Contextual Analysis: Scope I Henrik Nilsson University of Nottingham, UK COMP3012/G53CMP: Lecture 5 – p.1/39

Transcript of COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our...

Page 1: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

COMP3012/G53CMP: Lecture 5Contextual Analysis: Scope I

Henrik Nilsson

University of Nottingham, UK

COMP3012/G53CMP: Lecture 5 – p.1/39

Page 2: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

This Lecture

• Limitations of context-free languages:Why checking contextual constraints isdifferent from checking syntactical constraints.

• Identification (or Name Resolution)

• Block Structure

• Symbol table

COMP3012/G53CMP: Lecture 5 – p.2/39

Page 3: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Where Are We?

sequence of characters

scanner

parser

checker

optimizer/

code

generator

sequence of tokens

Abstract Syntax Tree (AST)

verified/annotated AST

target code

Lexical Analysis

Syntactic Analysis/Parsing

Contextual Analysis/checking Static Semantics

(e.g. Type Checking)

Optimization and Code Generation

(possibly many steps involving a number

of intermediary representations)

COMP3012/G53CMP: Lecture 5 – p.3/39

Page 4: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (1)

Our next major topic is contextual analysis orchecking static semantics.

Among other things, this involves:

COMP3012/G53CMP: Lecture 5 – p.4/39

Page 5: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (1)

Our next major topic is contextual analysis orchecking static semantics.

Among other things, this involves:

• Resolve the meaning of symbols.

COMP3012/G53CMP: Lecture 5 – p.4/39

Page 6: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (1)

Our next major topic is contextual analysis orchecking static semantics.

Among other things, this involves:

• Resolve the meaning of symbols.

• Report undefined symbols.

COMP3012/G53CMP: Lecture 5 – p.4/39

Page 7: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (1)

Our next major topic is contextual analysis orchecking static semantics.

Among other things, this involves:

• Resolve the meaning of symbols.

• Report undefined symbols.

• Type checking.

COMP3012/G53CMP: Lecture 5 – p.4/39

Page 8: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (2)

In short, contextual analysis is about ensuringthat a program is statically well-formed.

COMP3012/G53CMP: Lecture 5 – p.5/39

Page 9: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (2)

In short, contextual analysis is about ensuringthat a program is statically well-formed.

• But syntax has to do with “form” too.So what is new?

COMP3012/G53CMP: Lecture 5 – p.5/39

Page 10: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (2)

In short, contextual analysis is about ensuringthat a program is statically well-formed.

• But syntax has to do with “form” too.So what is new?

• Can’t we use context-free grammars (CFG) toexpress e.g. type constraints and thus makethe parser do the checking for us?

COMP3012/G53CMP: Lecture 5 – p.5/39

Page 11: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (2)

In short, contextual analysis is about ensuringthat a program is statically well-formed.

• But syntax has to do with “form” too.So what is new?

• Can’t we use context-free grammars (CFG) toexpress e.g. type constraints and thus makethe parser do the checking for us?

E.g., grammar productions like:

Cmd → if BoolExpr then Cmd

COMP3012/G53CMP: Lecture 5 – p.5/39

Page 12: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Limitations of CFGs (1)

Attempt to express a “declare before use”requirement using a CFG.Assumption: only a single variable a:

Prog → DeclA ProgA

ProgA → StmtA ProgA | ǫ

DeclA → int a ;

StmtA → a = ExprA ;

ExprA → a | ExprA + ExprA | Expr

Expr → LitInt | Expr + Expr

COMP3012/G53CMP: Lecture 5 – p.6/39

Page 13: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Limitations of CFGs (2)

Generalization to two variables, a and b:Prog → DeclA ProgA | DeclB ProgBProgA → StmtA ProgA | DeclB ProgAB | ǫ

ProgB → StmtB ProgB | DeclA ProgAB | ǫ

ProgAB → StmtAB ProgAB | ǫ

DeclA → int a ;

DeclB → int b ;

StmtA → a = ExprA ;

StmtB → b = ExprB ;

StmtAB → (a | b) = ExprAB ;

ExprA → a | ExprA + ExprA | ExprExprB → b | ExprB + ExprB | ExprExprAB → a | b | ExprAB + ExprAB | ExprExpr → LitInt | Expr + Expr

COMP3012/G53CMP: Lecture 5 – p.7/39

Page 14: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Limitations of CFGs (3)

Some observations:

COMP3012/G53CMP: Lecture 5 – p.8/39

Page 15: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Limitations of CFGs (3)

Some observations:

• Already for two variables, things get quitecomplicated.

COMP3012/G53CMP: Lecture 5 – p.8/39

Page 16: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Limitations of CFGs (3)

Some observations:

• Already for two variables, things get quitecomplicated.Roughly, how many “ExprXYZ ” rules for nvariables?

COMP3012/G53CMP: Lecture 5 – p.8/39

Page 17: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Limitations of CFGs (3)

Some observations:

• Already for two variables, things get quitecomplicated.

• In fact, the number of nonterminals growexponentially. E.g., for a set of n variablesV = {ai | 1 ≤ i ≤ n}, we get 2n nonterminalsExpr[W ], one for each W ⊆ V .

COMP3012/G53CMP: Lecture 5 – p.8/39

Page 18: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Limitations of CFGs (3)

Some observations:

• Already for two variables, things get quitecomplicated.

• In fact, the number of nonterminals growexponentially. E.g., for a set of n variablesV = {ai | 1 ≤ i ≤ n}, we get 2n nonterminalsExpr[W ], one for each W ⊆ V .

• Normally, the number of variables is unlimited.That would imply infinitely many productions.No longer a CFG!

COMP3012/G53CMP: Lecture 5 – p.8/39

Page 19: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Limitations of CFGs (4)

Attempt to describe simple type constraints usinga CFG:

IntExpr → LitInt

| IntVar

| IntExpr + IntExpr

BoolExpr → false

| true

| BoolVar

| IntExpr < IntExpr

| not BoolExpr

| BoolExpr && BoolExpr

COMP3012/G53CMP: Lecture 5 – p.9/39

Page 20: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Limitations of CFGs (5)

Might look reasonable at first sight.

COMP3012/G53CMP: Lecture 5 – p.10/39

Page 21: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Limitations of CFGs (5)

Might look reasonable at first sight.Any problem? Unrealistic assumption?

COMP3012/G53CMP: Lecture 5 – p.10/39

Page 22: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Limitations of CFGs (5)

Might look reasonable at first sight. However:

• The scheme hinges on partitioning the variablesby name into two groups: integer variables(IntVar ) and boolean variables (BoolVar ).

COMP3012/G53CMP: Lecture 5 – p.10/39

Page 23: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Limitations of CFGs (5)

Might look reasonable at first sight. However:

• The scheme hinges on partitioning the variablesby name into two groups: integer variables(IntVar ) and boolean variables (BoolVar ).

• But in most languages the type of a variableis given by the context, not its name.

COMP3012/G53CMP: Lecture 5 – p.10/39

Page 24: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Limitations of CFGs (5)

Might look reasonable at first sight. However:

• The scheme hinges on partitioning the variablesby name into two groups: integer variables(IntVar ) and boolean variables (BoolVar ).

• But in most languages the type of a variableis given by the context, not its name.

• And how could we in general infer argumenttypes from the name of a procedure or function?

COMP3012/G53CMP: Lecture 5 – p.10/39

Page 25: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Limitations of CFGs (5)

Might look reasonable at first sight. However:

• The scheme hinges on partitioning the variablesby name into two groups: integer variables(IntVar ) and boolean variables (BoolVar ).

• But in most languages the type of a variableis given by the context, not its name.

• And how could we in general infer argumenttypes from the name of a procedure or function?

• We should not expect to be able to capturecontext-sensitive information using acontext-free grammar.

COMP3012/G53CMP: Lecture 5 – p.10/39

Page 26: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Unrestricted Grammars (1)

• These examples do not prove that it isimpossible to achieve what we tried toachieve using CFGs.

COMP3012/G53CMP: Lecture 5 – p.11/39

Page 27: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Unrestricted Grammars (1)

• These examples do not prove that it isimpossible to achieve what we tried toachieve using CFGs.

• However, it can be proved that this indeed isthe case: contextual constraints result incontext sensitive or even recursivelyenumerable languages; such languagescannot be described by CFGs.

COMP3012/G53CMP: Lecture 5 – p.11/39

Page 28: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Unrestricted Grammars (2)

• Unrestricted grammars with productions

α → β

where α and β both are arbitrary stringscould be used to express arbitrary contextualconstraints.

COMP3012/G53CMP: Lecture 5 – p.12/39

Page 29: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Unrestricted Grammars (2)

• Unrestricted grammars with productions

α → β

where α and β both are arbitrary stringscould be used to express arbitrary contextualconstraints.

• However, unrestricted grammars are in factequivalent to Turing Machines!

COMP3012/G53CMP: Lecture 5 – p.12/39

Page 30: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Expressing Contextual Constraints

Neither Turing Machines nor UnrestrictedGrammars are very practical languages.

COMP3012/G53CMP: Lecture 5 – p.13/39

Page 31: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Expressing Contextual Constraints

Neither Turing Machines nor UnrestrictedGrammars are very practical languages.

• Specifying contextual constraints:

COMP3012/G53CMP: Lecture 5 – p.13/39

Page 32: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Expressing Contextual Constraints

Neither Turing Machines nor UnrestrictedGrammars are very practical languages.

• Specifying contextual constraints:

- Informally, using natural language.

COMP3012/G53CMP: Lecture 5 – p.13/39

Page 33: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Expressing Contextual Constraints

Neither Turing Machines nor UnrestrictedGrammars are very practical languages.

• Specifying contextual constraints:

- Informally, using natural language.

- Formally, using a mathematical formalismlike attribute grammars, logical inference rules.

COMP3012/G53CMP: Lecture 5 – p.13/39

Page 34: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Expressing Contextual Constraints

Neither Turing Machines nor UnrestrictedGrammars are very practical languages.

• Specifying contextual constraints:

- Informally, using natural language.

- Formally, using a mathematical formalismlike attribute grammars, logical inference rules.

• Implementing contextual checks:

COMP3012/G53CMP: Lecture 5 – p.13/39

Page 35: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Expressing Contextual Constraints

Neither Turing Machines nor UnrestrictedGrammars are very practical languages.

• Specifying contextual constraints:

- Informally, using natural language.

- Formally, using a mathematical formalismlike attribute grammars, logical inference rules.

• Implementing contextual checks:

- General purpose programming language.

COMP3012/G53CMP: Lecture 5 – p.13/39

Page 36: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Expressing Contextual Constraints

Neither Turing Machines nor UnrestrictedGrammars are very practical languages.

• Specifying contextual constraints:

- Informally, using natural language.

- Formally, using a mathematical formalismlike attribute grammars, logical inference rules.

• Implementing contextual checks:

- General purpose programming language.

- Direct support of mathematical formalism,unifying specification and implementation.

COMP3012/G53CMP: Lecture 5 – p.13/39

Page 37: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (1)

Two important kinds of contextual constraints:

COMP3012/G53CMP: Lecture 5 – p.14/39

Page 38: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (1)

Two important kinds of contextual constraints:

• Scope rules: visibility; which declarationstake effect where.

COMP3012/G53CMP: Lecture 5 – p.14/39

Page 39: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (1)

Two important kinds of contextual constraints:

• Scope rules: visibility; which declarationstake effect where.

• Type rules: internal consistency; ensuringthat every expression computes a value ofacceptable form, i.e., has a valid type.

COMP3012/G53CMP: Lecture 5 – p.14/39

Page 40: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (1)

Two important kinds of contextual constraints:

• Scope rules: visibility; which declarationstake effect where.

• Type rules: internal consistency; ensuringthat every expression computes a value ofacceptable form, i.e., has a valid type.

These are the ones we mainly will be concernedwith in this course.

COMP3012/G53CMP: Lecture 5 – p.14/39

Page 41: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (2)

Corresponding subphases of the contextualanalysis:

COMP3012/G53CMP: Lecture 5 – p.15/39

Page 42: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (2)

Corresponding subphases of the contextualanalysis:

• Identification or Name Resolution: applyingthe scope rules in order to relate each appliedidentifier occurrence to its declaration.

COMP3012/G53CMP: Lecture 5 – p.15/39

Page 43: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (2)

Corresponding subphases of the contextualanalysis:

• Identification or Name Resolution: applyingthe scope rules in order to relate each appliedidentifier occurrence to its declaration.

• Type checking: applying the type rules toinfer the type of each expression, andcompare it with the expected type.

COMP3012/G53CMP: Lecture 5 – p.15/39

Page 44: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (3)

Many other possible kinds of contextualconstraints. E.g. Java has rules concerning:

COMP3012/G53CMP: Lecture 5 – p.16/39

Page 45: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (3)

Many other possible kinds of contextualconstraints. E.g. Java has rules concerning:

• Abstract classes; e.g.:

- Only abstract classes can have abstr. methods:abstract class A {

abstract void callme();

}

COMP3012/G53CMP: Lecture 5 – p.16/39

Page 46: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (3)

Many other possible kinds of contextualconstraints. E.g. Java has rules concerning:

• Abstract classes; e.g.:

- Only abstract classes can have abstr. methods:abstract class A {

abstract void callme();

}

- Abstract classes may not be instantiated:Not allowed: new A();

COMP3012/G53CMP: Lecture 5 – p.16/39

Page 47: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (4)

• Final classes; e.g.:

- a final class cannot be extended

- a class cannot be both final and abstract.

COMP3012/G53CMP: Lecture 5 – p.17/39

Page 48: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (4)

• Final classes; e.g.:

- a final class cannot be extended

- a class cannot be both final and abstract.

• Exceptions; e.g., the set of exceptions amethod can raise must be declared (exceptfor unchecked exceptions):

public void writeList()

throws IOException { ... }

COMP3012/G53CMP: Lecture 5 – p.17/39

Page 49: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (5)

• Definite assignment; a local variable mustnot be read unless it has been “definitelyassigned before”.For example, the code fragment

int k, n = 5;

if (n > 2) k = 3;

System.out.println(k);

is rejected.

COMP3012/G53CMP: Lecture 5 – p.18/39

Page 50: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (6)

An example of a Java "definite assignment" rule:

V is definitely assigned after

if (e) S else T

iff V is definitely assigned after S and V isdefinitely assigned after T .

COMP3012/G53CMP: Lecture 5 – p.19/39

Page 51: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Contextual Analysis (6)

An example of a Java "definite assignment" rule:

V is definitely assigned after

if (e) S else T

iff V is definitely assigned after S and V isdefinitely assigned after T .

Note: The rule does not take the ultimaterun-time value of e into account. That is whatmakes the analysis decidable.

COMP3012/G53CMP: Lecture 5 – p.19/39

Page 52: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Identification

Identification (or Name Resolution) is the taskof relating each applied identifier occurrence toits declaration.

public class C {

int x, n;

void set(int n) { x = n; }

}

COMP3012/G53CMP: Lecture 5 – p.20/39

Page 53: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Identification

Identification (or Name Resolution) is the taskof relating each applied identifier occurrence toits declaration.

public class C {

int x, n;

void set(int n) { x = n; }

}

In the body of set, the one applied occurrence of

• x refers to the instance variable x

COMP3012/G53CMP: Lecture 5 – p.20/39

Page 54: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Identification

Identification (or Name Resolution) is the taskof relating each applied identifier occurrence toits declaration.

public class C {

int x, n;

void set(int n) { x = n; }

}

In the body of set, the one applied occurrence of

• x refers to the instance variable x

• n refers to the argument n.COMP3012/G53CMP: Lecture 5 – p.20/39

Page 55: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Scope and Scope Rules (1)

The identification process is governed by thescope rules of the language.

Important terms:

COMP3012/G53CMP: Lecture 5 – p.21/39

Page 56: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Scope and Scope Rules (1)

The identification process is governed by thescope rules of the language.

Important terms:

• Scope: the portion of a program over which adeclaration takes effect.

COMP3012/G53CMP: Lecture 5 – p.21/39

Page 57: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Scope and Scope Rules (1)

The identification process is governed by thescope rules of the language.

Important terms:

• Scope: the portion of a program over which adeclaration takes effect.

• Block: a program phrase that delimits thescope of declarations within it.

COMP3012/G53CMP: Lecture 5 – p.21/39

Page 58: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Scope and Scope Rules (2)

Consider the MiniTriangle let block command:

let decls in body

The scope of each declaration is the rest of theblock.

COMP3012/G53CMP: Lecture 5 – p.22/39

Page 59: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Scope and Scope Rules (2)

Consider the MiniTriangle let block command:

let decls in body

The scope of each declaration is the rest of theblock.

For example:

let Scope of m

const m = 10;

const n = m * 2

in

putint(n);Scope of n

COMP3012/G53CMP: Lecture 5 – p.22/39

Page 60: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Scope and Scope Rules (3)

Haskell’s let-expressions:

let id = expr in body

The scope of id includes both expr and body!

COMP3012/G53CMP: Lecture 5 – p.23/39

Page 61: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Scope and Scope Rules (3)

Haskell’s let-expressions:

let id = expr in body

The scope of id includes both expr and body!

For example:

let xs = 1:xs in take 7 xs

Scope of xs

COMP3012/G53CMP: Lecture 5 – p.23/39

Page 62: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Scope and Scope Rules (4)

Part II of the coursework uses a version of Mini-Triangle extended with procedures and functions:

let

const n : Integer = 2;

proc p(x : Integer) begin

... p(x * m) ...

end;

const m : Integer = n * n

in

...

COMP3012/G53CMP: Lecture 5 – p.24/39

Page 63: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Scope and Scope Rules (5)

In the extended version:

COMP3012/G53CMP: Lecture 5 – p.25/39

Page 64: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Scope and Scope Rules (5)

In the extended version:

• The scope of a declared entity is extended toinclude the bodies of all procedures andfunctions declared in the same let-block.

COMP3012/G53CMP: Lecture 5 – p.25/39

Page 65: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Scope and Scope Rules (5)

In the extended version:

• The scope of a declared entity is extended toinclude the bodies of all procedures andfunctions declared in the same let-block.

• This allows procedures and functions to be(mutually) recursive.

COMP3012/G53CMP: Lecture 5 – p.25/39

Page 66: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Scope and Scope Rules (5)

In the extended version:

• The scope of a declared entity is extended toinclude the bodies of all procedures andfunctions declared in the same let-block.

• This allows procedures and functions to be(mutually) recursive.

• However, definition/initialization expressionsfor constants/variables must not use functionsdefined in the same let-block.

COMP3012/G53CMP: Lecture 5 – p.25/39

Page 67: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Scope and Scope Rules (5)

In the extended version:

• The scope of a declared entity is extended toinclude the bodies of all procedures andfunctions declared in the same let-block.

• This allows procedures and functions to be(mutually) recursive.

• However, definition/initialization expressionsfor constants/variables must not use functionsdefined in the same let-block.

• This avoids calling functions that may refer toas-yet uninitialized variables.

COMP3012/G53CMP: Lecture 5 – p.25/39

Page 68: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Scope and Scope Rules (6)

In addition to deciding the range of declarations,the scope rules also also deal with issues like

COMP3012/G53CMP: Lecture 5 – p.26/39

Page 69: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Scope and Scope Rules (6)

In addition to deciding the range of declarations,the scope rules also also deal with issues like

• whether explicit declarations are required

COMP3012/G53CMP: Lecture 5 – p.26/39

Page 70: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Scope and Scope Rules (6)

In addition to deciding the range of declarations,the scope rules also also deal with issues like

• whether explicit declarations are required

• whether multiple declarations at the samelevel are allowed

COMP3012/G53CMP: Lecture 5 – p.26/39

Page 71: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Scope and Scope Rules (6)

In addition to deciding the range of declarations,the scope rules also also deal with issues like

• whether explicit declarations are required

• whether multiple declarations at the samelevel are allowed

• whether shadowing/hiding is allowed.

COMP3012/G53CMP: Lecture 5 – p.26/39

Page 72: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Some Java Scope Rules (1)

From the Java Language Specification ver. 1.0:

COMP3012/G53CMP: Lecture 5 – p.27/39

Page 73: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Some Java Scope Rules (1)

From the Java Language Specification ver. 1.0:

• The scope of a member declared in orinherited by a class type or interface type isthe entire declaration of the class or interfacetype. The declaration of a member needs toappear before it is used only when the use isin a field initialization expression.

COMP3012/G53CMP: Lecture 5 – p.27/39

Page 74: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Some Java Scope Rules (1)

From the Java Language Specification ver. 1.0:

• The scope of a member declared in orinherited by a class type or interface type isthe entire declaration of the class or interfacetype. The declaration of a member needs toappear before it is used only when the use isin a field initialization expression.

• The scope of a parameter of a method is theentire body of the method.

COMP3012/G53CMP: Lecture 5 – p.27/39

Page 75: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Some Java Scope Rules (2)

• Hiding the name of a local variable is notpermitted. For example, the following codefragment is rejected:

static int x = 10;

public void foo(int x) {

if (x < 0) {

int x = 10;

...

}

...

}

OK, hides classvariable X

Not OK, hidesparameter X

COMP3012/G53CMP: Lecture 5 – p.28/39

Page 76: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Symbol Table

A symbol table, also called identification tableor environment, is used during identification tokeep track of symbols and their attributes, suchas:

• kind of symbol (class name, local variable, etc.)

• scope level

• type

• source code position

COMP3012/G53CMP: Lecture 5 – p.29/39

Page 77: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Block Structure (1)

The organisation of the symbol table depends onthe source language’s block structure. Threemain possibilities:

COMP3012/G53CMP: Lecture 5 – p.30/39

Page 78: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Block Structure (1)

The organisation of the symbol table depends onthe source language’s block structure. Threemain possibilities:

• Monolithic block structure: one common,global scope.(Old Basic dialects, Cobol, Assembly lang., . . . )

COMP3012/G53CMP: Lecture 5 – p.30/39

Page 79: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Block Structure (1)

The organisation of the symbol table depends onthe source language’s block structure. Threemain possibilities:

• Monolithic block structure: one common,global scope.(Old Basic dialects, Cobol, Assembly lang., . . . )

• Flat block structure: blocks with local scopeenclosed in a global scope. (Fortran)

COMP3012/G53CMP: Lecture 5 – p.30/39

Page 80: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Block Structure (1)

The organisation of the symbol table depends onthe source language’s block structure. Threemain possibilities:

• Monolithic block structure: one common,global scope.(Old Basic dialects, Cobol, Assembly lang., . . . )

• Flat block structure: blocks with local scopeenclosed in a global scope. (Fortran)

• Nested block structure: blocks can benested to arbitrary depth.(Ada, C, C++, Java, C#, Haskell, ML, . . . )

COMP3012/G53CMP: Lecture 5 – p.30/39

Page 81: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Block Structure (2)

We will focus on nested block structure in thefollowing because:

• monolithic and flat block structure can beconsidered special cases of nested blockstructure

• variations on nested block structure is by farthe most common in modern high-levellanguages.

COMP3012/G53CMP: Lecture 5 – p.31/39

Page 82: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Using the Symbol Table (1)

For a simple language with a declare-before-userule, redeclarations not allowed, the symbol tablewould be used as follows during identification:

COMP3012/G53CMP: Lecture 5 – p.32/39

Page 83: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Using the Symbol Table (1)

For a simple language with a declare-before-userule, redeclarations not allowed, the symbol tablewould be used as follows during identification:

• Initialise the table; e.g., enter the standardenvironment.

COMP3012/G53CMP: Lecture 5 – p.32/39

Page 84: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Using the Symbol Table (1)

For a simple language with a declare-before-userule, redeclarations not allowed, the symbol tablewould be used as follows during identification:

• Initialise the table; e.g., enter the standardenvironment.

• When a declaration is encountered:

COMP3012/G53CMP: Lecture 5 – p.32/39

Page 85: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Using the Symbol Table (1)

For a simple language with a declare-before-userule, redeclarations not allowed, the symbol tablewould be used as follows during identification:

• Initialise the table; e.g., enter the standardenvironment.

• When a declaration is encountered:

- check if declared identifier clashes withexisting symbol

COMP3012/G53CMP: Lecture 5 – p.32/39

Page 86: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Using the Symbol Table (1)

For a simple language with a declare-before-userule, redeclarations not allowed, the symbol tablewould be used as follows during identification:

• Initialise the table; e.g., enter the standardenvironment.

• When a declaration is encountered:

- check if declared identifier clashes withexisting symbol

- report error if it does

COMP3012/G53CMP: Lecture 5 – p.32/39

Page 87: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Using the Symbol Table (1)

For a simple language with a declare-before-userule, redeclarations not allowed, the symbol tablewould be used as follows during identification:

• Initialise the table; e.g., enter the standardenvironment.

• When a declaration is encountered:

- check if declared identifier clashes withexisting symbol

- report error if it does

- if not, enter declared identifier into tablealong with its attributes.

COMP3012/G53CMP: Lecture 5 – p.32/39

Page 88: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Using the Symbol Table (2)

• When an applied identifier occurrence isencountered:

COMP3012/G53CMP: Lecture 5 – p.33/39

Page 89: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Using the Symbol Table (2)

• When an applied identifier occurrence isencountered:

- look up identifier in table, taking scoperules into account

COMP3012/G53CMP: Lecture 5 – p.33/39

Page 90: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Using the Symbol Table (2)

• When an applied identifier occurrence isencountered:

- look up identifier in table, taking scoperules into account

- report error if not found

COMP3012/G53CMP: Lecture 5 – p.33/39

Page 91: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Using the Symbol Table (2)

• When an applied identifier occurrence isencountered:

- look up identifier in table, taking scoperules into account

- report error if not found

- if found, annotate applied occurrence withsymbol attributes from table.

COMP3012/G53CMP: Lecture 5 – p.33/39

Page 92: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Using the Symbol Table (3)

Before identification:

1 let int x = 1

2 in

3 let int y = x * 3

4 in

5 x + y

COMP3012/G53CMP: Lecture 5 – p.34/39

Page 93: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Using the Symbol Table (4)

After identification:

let int x = 1

in

let int y =

x [level 0, type int, line 1]

* 3

in

x [level 0, type int, line 1]

+ y [level 1, type int, line 3]

(Textual representation of annotated AST.)

COMP3012/G53CMP: Lecture 5 – p.35/39

Page 94: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Using the Symbol Table (5)

Suppose variables have to be declared, and thatredeclarations are not allowed.

1 let int x = 1; int x = x * 3

2 in

3 x + y

During symbol table insert and lookup it would bediscovered that:

• x is declared twice at the same scope level,

• y is not declared at all.

COMP3012/G53CMP: Lecture 5 – p.36/39

Page 95: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Using the Symbol Table (6)

• When entering a new block, arrange so thatsubsequently entered symbols becomeassociated with the scope corresponding tothe block (“open scope”).

COMP3012/G53CMP: Lecture 5 – p.37/39

Page 96: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Using the Symbol Table (6)

• When entering a new block, arrange so thatsubsequently entered symbols becomeassociated with the scope corresponding tothe block (“open scope”).

• When leaving a block, remove/makeinaccessible symbols declared in that block(“close scope”).

COMP3012/G53CMP: Lecture 5 – p.37/39

Page 97: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Using the Symbol Table (7)

1 let int x = 1

2 in

3 (let y = x * 3 in x) + y

• A new scope is opened for the innerlet-block when it is analysed.

• When the inner let-block has beenanalysed, its scope is closed.

• It is then discovered that y is no longer inscope. (However, x is still in scope.)

COMP3012/G53CMP: Lecture 5 – p.38/39

Page 98: COMP3012/G53CMP: Lecture 5psznhn/G53CMP/LectureNotes-2018/lecture05.pdfContextual Analysis (1) Our next major topic is contextual analysis or checking static semantics. Among other

Summary

• Contextual analysis includes checking scoperules and types.

• Contextual constraints lead to context-sensitivelanguages and thus cannot be captured by acontext-free grammar.

• Identification is the task of relating eachapplied identifier occurrence to its declaration.A key step for any contextual analysis.

• The Symbol Table or Environment recordsinformation about declared entities and is thecentral data structure during contextual analysis.

COMP3012/G53CMP: Lecture 5 – p.39/39