07 SemanticAnalysis

07 SemanticAnalysis - Semantic Analysis Chapter 4 in Scott...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Semantic Analysis Chapter 4 in Scott 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Syntax = form Semantics = meaning Many authors use term syntax (form) for things that are specified by CFG, semantics (meaning) for everything else 2
Background image of page 2
Context constraints Not specified by context-free grammar but we use grammar as starting point Some aspects of programming languages cannot be specified with a context-free grammar E.g. formal and actual parameters of a procedure match Some things could be specified with a CFG, but it is so complicated to do so that we usually don’t 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
We have already talked about scope rules and symbol tables Touched on the issue of types Before talking about typing rules in more detail, we’ll discuss general techniques for specifying constraints and semantics in terms of annotation of a parse or syntax tree Then look at typing systems (skipping to chapter 7) 4
Background image of page 4
Kinds of semantics contextual constraints static can be checked by a compiler performing static analysis dynamic cannot be checked by compiler the compiler generates code to check at runtime 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Examples of static context constraints Variables must be declared before used Variables cannot be declared more than once in the same program unit In x := y; x and y must have compatible types every function must contain at least one return statement 6
Background image of page 6
Static analysis Constraints can described in terms of annotation or decoration of a syntax tree. Annotations are known as attributes . Static analysis is precise if the compiler can determine whether a given program follows the rules. Example: in some languages, type checking is static and precise. In others it is not, and some type checking must be done at runtime. 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Role of semantic analyzer Wide variation in semantic rules between programming languages Wide variation in what must be checked dynamically C only checks what is done by hardware (divide by zero, etc.) Java checks as much as possible to try to ensure that program can’t damage machine on which it runs. Static analyzers enforce static rules and annotate the program with information needed to generate code for dynamic checks 8
Background image of page 8
Attribute grammars Provide formal framework for decoration of a tree Motivation Context-free grammar tells us which sentences are legal It doesn’t tell us what they mean, or impose contextual constraints on the sentences. Based on a context-free grammar Symbols in the grammar are associated with attributes Production rules in grammar are associated with semantic rules indicating how the attribute values are computed May include conditions (boolean valued functions of the attributes) that impose constraints on the legal sentences 9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example: Bounded unsigned integers unsigned ::= digit | unsigned digit digit ::= 0 | 1 | . .. | 9 Attributes capture correspondence between characters and mathematical values.
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/09/2009 for the course COP 5555 taught by Professor Staff during the Fall '08 term at University of Florida.

Page1 / 80

07 SemanticAnalysis - Semantic Analysis Chapter 4 in Scott...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online