Docsity

Compiler and Interpreter: Semantic Analysis and Scope Rules - Prof. Westley Weimer, Study notes of Programming Languages

Partial preview of the text

Download Compiler and Interpreter: Semantic Analysis and Scope Rules - Prof. Westley Weimer and more Study notes Programming Languages in PDF only on Docsity!

Scoping Scoping

and and

Type Type

Checking Checking

First, WA

  • Pick it up! Even if you got a passing grade you’ll want to see what we marked up.
  • The midterm is not pass/fail.
  • Derivations and parse trees are closely related, but if we ask you to draw a parse tree you must draw the parse tree.
  • WA2#4 was in the book (Fig 2.34; you just had to substitute in k=3):

PA2 Problems

  • -5: completely bizarre and partially incorrect error messages (2x)
  • (^) -5: outputting partially lexed file on error (3x)
  • -5: outputting to STDOUT (2x)
  • -10: Claire having to fix code to get it to compile
  • Don't do these things on PA3, PA4 or PA5!
    • We only become harsher as time goes by.

Next. Midterm Fever: Catch it!

In One Slide

  • Scoping rules match identifier uses with

identifier definitions.

  • A type is a set of values coupled with a

set of operations on those values.

  • A type system specifies which operations

are valid for which types.

  • Type checking can be done statically (at

compile time) or dynamically (at run

time).

Lecture Outline

  • The role of semantic analysis in a compiler
    • A laundry list of tasks
  • Scope
  • Types

What’s Wrong?

  • Example 1

let y: Int in x + 3

  • Example 2

let y: String Ã

“abc” in y + 3

Why a Separate Semantic

Analysis?

  • Parsing cannot catch some errors
  • Some language constructs are not context- free - Example: All used variables must have been declared (i.e. scoping) - Example: A method must be invoked with arguments of proper type (i.e. typing)

Scope

  • Scoping rules match identifier uses with identifier declarations - Important semantic analysis step in most languages - Including COOL!

Scope (Cont.)

  • The scope of an identifier is the portion of a program in which that identifier is accessible
  • The same identifier may refer to different things in different parts of the program - Different scopes for same name don’t overlap
  • An identifier may have restricted scope

Static Scoping Example

let x: Int <- 0 in { x; { let x: Int <- 1 in x; } ; x; }

Static Scoping Example (Cont.)

let x: Int <- 0 in { x; { let x: Int <- 1 in x; } ; x; } Uses of x refer to closest enclosing definition

Implementing the Implementing the

Most-Closely Nested Rule Most-Closely Nested Rule

  • Much of semantic analysis can be expressed asMuch of semantic analysis can be expressed as a a recursive descentrecursive descent of an ASTof an AST - Process an AST nodeProcess an AST node nn - Process the children ofProcess the children of nn - Finish processing the AST nodeFinish processing the AST node nn

Implementing... (Cont.)

  • Example: the scope of let bindings is one subtree

let x: Int à 0 in e

  • x can be used in subtree e