1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix.

Download 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix.

Post on 19-Dec-2015

212 views

Category:

Documents

0 download

TRANSCRIPT

  • Slide 1
  • 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos Feb 6 The University of North Carolina at Chapel Hill
  • Slide 2
  • 2 COMP 144 Programming Language Concepts Felix Hernandez-Campos Semantic Analysis From Code Form To Program Meaning Compiler or Interpreter Translation Execution Source Code Target Code Interpre-tation
  • Slide 3
  • 3 COMP 144 Programming Language Concepts Felix Hernandez-Campos Phases of Compilation
  • Slide 4
  • 4 COMP 144 Programming Language Concepts Felix Hernandez-Campos Specification of Programming Languages PLs require precise definitions (i.e. no ambiguity)PLs require precise definitions (i.e. no ambiguity) Language form (Syntax) Language meaning (Semantics) Consequently, PLs are specified using formal notation:Consequently, PLs are specified using formal notation: Formal syntax Tokens Grammar Formal semantics Attribute Grammars Dynamic Semantics
  • Slide 5
  • 5 COMP 144 Programming Language Concepts Felix Hernandez-Campos Attribute Grammars Context-Free Grammars (CFGs) are used to specify the syntax of programming languagesContext-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?How do we tie these rules to mathematical concepts? Attribute grammars are annotated CFGs in which annotations are used to establish meaning relationships among symbolsAttribute grammars are annotated CFGs in which annotations are used to establish meaning relationships among symbols Annotations are also known as decorations
  • Slide 6
  • 6 COMP 144 Programming Language Concepts Felix Hernandez-Campos Attribute Grammars Example Each grammar symbols has a set of attributesEach grammar symbols has a set of attributes E.g. the value of E 1 is the attribute E 1.val Each grammar rule has a set of rules over the symbol attributesEach grammar rule has a set of rules over the symbol attributes Copy rules Semantic Function rules E.g. sum, quotient
  • Slide 7
  • 7 COMP 144 Programming Language Concepts Felix Hernandez-Campos Attribute Flow Context-free grammars are not tied to an specific parsing orderContext-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 orderAttribute grammars are not tied to an specific evaluation order This evaluation is known as the annotation or decoration of the parse tree
  • Slide 8
  • 8 COMP 144 Programming Language Concepts Felix Hernandez-Campos Attribute Flow Example The figure shows the result of annotating the parse tree for (1+3)*2The figure shows the result of annotating the parse tree for (1+3)*2 Each symbols has at most one attribute shown in the corresponding boxEach symbols has at most one attribute shown in the corresponding box Numerical value in this example Operator symbols have no value Arrows represent attribute flowArrows represent attribute flow
  • Slide 9
  • 9 COMP 144 Programming Language Concepts Felix Hernandez-Campos Attribute Flow Example
  • Slide 10
  • 10 COMP 144 Programming Language Concepts Felix Hernandez-Campos Attribute Flow Synthetic and Inherited Attributes In the previous example, semantic information is pass up the parse treeIn 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 treeSemantic 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
  • Slide 11
  • 11 COMP 144 Programming Language Concepts Felix Hernandez-Campos Attribute Flow Inherited Attributes Semantic information can also be passed down the parse treeSemantic 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 flowsTop-down grammars generally require non-S- attributed flows The previous annotated grammar was an S-attributed LR(1)
  • Slide 12
  • 12 COMP 144 Programming Language Concepts Felix Hernandez-Campos Non-S-Attributed Grammars Example
  • Slide 13
  • 13 COMP 144 Programming Language Concepts Felix Hernandez-Campos Action Routines Automatic tools can construct a parser for a given context-free grammarAutomatic tools can construct a parser for a given context-free grammar E.g. yacc Automatic tools can construct a semantic analyzer for an attribute grammarAutomatic 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
  • Slide 14
  • 14 COMP 144 Programming Language Concepts Felix Hernandez-Campos Action Routines Example
  • Slide 15
  • 15 COMP 144 Programming Language Concepts Felix Hernandez-Campos Static and Dynamic Semantics Attribute grammars add basic semantic rules to the specification of a languageAttribute 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 timeBut they are limited to the semantic form that can be checked at compile time Other semantic properties cannot be checked at compile timeOther semantic properties cannot be checked at compile time They are described using dynamic semantics
  • Slide 16
  • 16 COMP 144 Programming Language Concepts Felix Hernandez-Campos Dynamic Semantics Use to formally specify the behavior of a programming languageUse to formally specify the behavior of a programming language Semantic-based error detection Correctness proofs There is not a universally accepted notationThere 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
  • Slide 17
  • 17 COMP 144 Programming Language Concepts Felix Hernandez-Campos Semantic Specification The most common way of specifying the semantics of a language is plain englishThe 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/binary.html http://www.python.org/doc/current/ref/while.html http://www.python.org/doc/current/ref/while.html http://java.sun.com/docs/books/jls/first_edition/html/14.do c.html#24588 http://java.sun.com/docs/books/jls/first_edition/html/14.do c.html#24588http://java.sun.com/docs/books/jls/first_edition/html/14.do c.html#24588 There is a lack of formal rigor in the semantic specification of programming languagesThere is a lack of formal rigor in the semantic specification of programming languages Guess why
  • Slide 18
  • 18 COMP 144 Programming Language Concepts Felix Hernandez-Campos Reading Assignment Scotts chapter 4Scotts chapter 4 Section 4.1 Section 4.2 Section 4.3 Section 4.4

Recommended

View more >