1 / 18

Semantic Analysis

Semantic Analysis. COMP3190: Principle of Programming Languages. Compilation in a Nutshell 1. Source code (character stream). if (b == 0) a = b;. Lexical analysis. if. (. b. ==. 0. ). a. =. b. ;. Token stream. Parsing. if. ;. ==. =. Abstract syntax tree (AST). b. 0. a.

Download Presentation

Semantic Analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Semantic Analysis COMP3190: Principle of Programming Languages

  2. Compilation in a Nutshell 1 Source code (character stream) if (b == 0) a = b; Lexical analysis if ( b == 0 ) a = b ; Token stream Parsing if ; == = Abstract syntax tree (AST) b 0 a b Semantic Analysis if boolean int == = ; Decorated AST int b int 0 int a lvalue intb

  3. fp 4 fp 8 Compilation in a Nutshell 2 if boolean int == = ; Intermediate Code Generation int b int 0 int a lvalue intb CJUMP == MEM CONST MOVE NOP Optimization + 0 MEM MEM fp 8 + + CJUMP == Code generation CX CONST MOVE NOP CMP CX, 0 CMOVZ DX,CX 0 DX CX

  4. Questions to Answer: • Is x a scalar, an array, or a function? • Is x declared before it is used? • Which declaration of x does this reference? • Does the dimension of a reference match the declaration? • Is an array reference in bounds? • Type errors (that can be caught statically)

  5. Attribute Grammars • Context-Free Grammars (CFGs) are used to specify the syntax of programming languages • E.g. arithmetic expressions • How do we tie these rules to mathematical concepts? • Attribute grammars are annotated CFGs in which annotations are used to establish meaning relationships among symbols • Annotations are also known as decorations

  6. Specification of Programming Languages • PLs require precise definitions (i.e. no ambiguity) • Language form (Syntax) • Language meaning (Semantics) • Consequently, PLs are specified using formal notation: • Formal syntax • Tokens • Grammar • Formal semantics • Attribute Grammars • Dynamic Semantics

  7. Each grammar symbols has a set of attributes E.g. the value of E1 is the attribute E1.val Each grammar rule has a set of rules over the symbol attributes Copy rules Semantic Function rules E.g. sum, quotient Attribute GrammarsExample

  8. Attribute Flow • Context-free grammars are not tied to an specific parsing order • E.g. Recursive descent, LR parsing • Attribute grammars are not tied to an specific evaluation order • This evaluation is known as the annotation or decoration of the parse tree

  9. Attribute Flow Example • The figure shows the result of annotating the parse tree for (1+3)*2 • Each symbols has at most one attribute shown in the corresponding box • Numerical value in this example • Operator symbols have no value • Arrows represent attribute flow

  10. Attribute Flow Example

  11. Attribute FlowSynthetic and Inherited Attributes • In the previous example, semantic information is pass up the parse tree • We call this type of attributes are called synthetic attributes • Attribute grammar with synthetic attributes only are said to be S-attributed • Semantic information can also be passed down the parse tree • Using inherited attributes • Attribute grammar with inherited attributes only are said to be non-S-attributed

  12. Attribute FlowInherited Attributes • Semantic information can also be passed down the parse tree • Using inherited attributes • Attribute grammar with inherited attributes only are said to be non-S-attributed • Top-down grammars generally require non-S-attributed flows • The previous annotated grammar was an S-attributed LR(1)

  13. Non-S-Attributed GrammarsExample

  14. Action Routines • Automatic tools can construct a parser for a given context-free grammar • E.g. yacc • Automatic tools can construct a semantic analyzer for an attribute grammar • An ad hoc techniques is to annotate the grammar with executable rules • These rules are known as action routines

  15. Action RoutinesExample

  16. Static and Dynamic Semantics • Attribute grammars add basic semantic rules to the specification of a language • They specify static semantics • But they are limited to the semantic form that can be checked at compile time • Other semantic properties cannot be checked at compile time • They are described using dynamic semantics

  17. Dynamic Semantics • Use to formally specify the behavior of a programming language • Semantic-based error detection • Correctness proofs • There is not a universally accepted notation • Operational semantics • Executing statements that represent changes in the state of a real or simulated machine • Axiomatic semantics • Using predicate calculus (pre and post-conditions) • Denotational semantics • Using recursive function theory

  18. Semantic Specification • The most common way of specifying the semantics of a language is plain english • http://www.python.org/doc/current/ref/binary.html • http://www.python.org/doc/current/ref/while.html • http://java.sun.com/docs/books/jls/first_edition/html/14.doc.html#24588 • There is a lack of formal rigor in the semantic specification of programming languages • Guess why

More Related