topic 5 semantic analysis n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Topic 5 -Semantic Analysis PowerPoint Presentation
Download Presentation
Topic 5 -Semantic Analysis

Loading in 2 Seconds...

play fullscreen
1 / 29

Topic 5 -Semantic Analysis - PowerPoint PPT Presentation


  • 154 Views
  • Uploaded on

Topic 5 -Semantic Analysis. Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems Concepts Fall 2002 Monday Wednesday 2:30-3:50 LI 99. Introduction to Semantic Analysis. Semantics are what a program means

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Topic 5 -Semantic Analysis' - graceland


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
topic 5 semantic analysis
Topic 5 -Semantic Analysis

Dr. William A. Maniatty

Assistant Prof.

Dept. of Computer Science

University At Albany

CSI 511

Programming Languages and Systems Concepts

Fall 2002

Monday Wednesday 2:30-3:50

LI 99

introduction to semantic analysis
Introduction to Semantic Analysis
  • Semantics are what a program means
    • Decorating the parse tree/syntax tree is done to generate code and analyze semantics.
    • Actions are inserted into the parser to generate the semantic analysis when language constructs are recognized.
  • Analysis means
    • Determining correct behavior
    • Detecting Dangerous or Nonsensical Behavior
when are semantics analyzed
When are Semantics Analyzed?
  • Semantic analysis done at compile time and run time
    • Can be done at most binding times.
  • Compilers split into front end/back end
    • Back end does semantic analysis
    • Front end does syntax analysis
some notes on correctness
Some notes on correctness
  • Semantic errors are hard to catch at compile time
    • Late binding makes static analysis hard
  • Correct programs can be derived
    • But this is hard and typically done for short heavily used code
  • In practice, combine static analysis and run time checking to help the programmer.
so why does my software crash
So, Why does my software crash?
  • Engineers know how to make working machines, why does software crash?
    • Software is discrete.
      • Failure not gradual or but discontinuous.
    • Analysis/understanding is hard
      • Bad Tools, Bad Techniques or Bad Paradigm?
        • Like doing calculus with roman numerals
    • Cost and performance trade offs
      • Last to market loses, Slow performance loses.
the role of testing and verification
The Role of Testing and Verification
  • Verification - Derive correct solution
    • Useful but hard, expensive and not feasible for large pieces of software.
  • Testing - Checks for the presence of errors.
    • But cannot rule out errors.
    • Cheaper, and practical.
    • Catching errors late increases repair costs.
assertions invariants pre post conditions
Assertions, Invariants, Pre/Post Conditions
  • Hardware provides some runtime checks
  • Languages can support testing:
    • Assertion - A condition which must be true at a particular point during execution.
    • Invariant -A condition which must hold true at all "clean points" of execution
    • Precondition - True before a statement
    • Postcondition - Ture after a statement
attribute grammars 1
Attribute Grammars 1
  • Recall that context free grammars specify programming language syntax.
  • An Attribute grammar (AG) extends the CFG to specify semantics of the language.
    • Each nonterminal is associated with a set of attributes (semantic information)
    • Each production has a set of semantic rules.
    • Terminals can supply needed information.
attribute grammars 2
Attribute Grammars 2
  • Semantic Rules are either:
    • Copy rules -Copy the RHS Attribute to LHS
    • Semantic Functions -Perform some function on the RHS attributes.
  • The Semantic Rules are simple
    • Refer to information available in the RHS of the production.
    • Avoid using non-local information
attribute grammars 3
Attribute Grammars 3
  • Closely related to:
    • Denotational Semantics -Focus on machine independent details.
    • Axiomatic Semantics - Focuses on verification/theorem proving.
attribute tree construction
Attribute Tree Construction
  • CFGs and Attribute Grammars do not specify:
    • Shape of the parse/attribute tree
    • Order of parse/attribute tree construction
  • Attribute Trees can be constructed:
    • On the fly - At the same time as parse tree
    • After the parse tree is built (in another pass)
attribute trees
Attribute Trees
  • Attribute Trees are annotated parse trees
    • Attributes flow between nodes
    • Values initialized by leaf nodes
    • Synthesized Attributes are computed only in productions where the nonterminal appears on the RHS
    • S-Attributed grammars synthesize all their attributes.
    • Scanner gives initial attributes for tokens
attribute flow
Attribute Flow
  • Attribute Trees are annotated parse trees
    • Attribute flow is the pattern of information movement in the attribute grammar tree.
    • Synthesized Attributes are computed in productions with the nonterminal on the LHS
      • S-Attributed grammars synthesize all their attributes.
    • Inherited attributes are calculated when a symbol is on the RHS of a production.
attribute flow lr example
Attribute Flow LR Example
  • LR Parsers have
    • Rightmost Trees
    • Bottom-Up Construction
  • Attributes Flow Along Parse Tree Edges.
    • In Bottom-Up Direction
    • Can Augment Parser Stack
  • E.g. AG for (3 + 1) * 2
attribute flow lr example1
Attribute Flow LR Example
  • Attributes initialized by scanner.
  • Transitive closure of copied pointers makes attributes available when evaluated.
attribute flow in ll grammars
Attribute Flow in LL Grammars
  • Notice that some productions have multiple rules.
  • TT and FT use an extra "subtotal" attribute.
  • If a symbol is on LHS and RHS add a subscript.
attribute flow in ll grammars1
Attribute Flow in LL Grammars
  • Minor errata, the RHS in expression 3: should be
    • TT1} - T TT2
attribute flow in ll grammars2
Attribute Flow in LL Grammars
  • LL Grammars need to work with
    • Leftmost parse tree derivations
    • Top to bottom
  • L-Attributed grammars do as follows:
    • Each LHS symbol's attributes depend on
      • The symbol's own attributes
      • Attributes of a symbol on the RHS
    • Each inherited attribute depends only on inherited attributes of the LHS grammars.
so why are attribute flows important
So Why are Attribute Flows Important?
  • A translation scheme is an algorithm that invokes the attributes of a parse tree in an order consistent with its attribute flow.
    • This is how compilers generate code!
    • One Pass compilers can evaluate attributes during parse tree construction using a stack.
      • LR Parsers can mirror or augment the parse stack
        • Since The Attribute Flow Mimics the parse tree
      • LL Parsers need a separate attribute stack.
some notation
Some Notation
  • An AG is well-defined if every parse tree has a unique set of attributes.
  • An AG is noncircular if no attribute ever depends (transitively) on itself.
action routines
Action Routines
  • Action routines realize the AG:
    • What Semantic Rule to apply
    • When (during what phase of parsing)
      • Can be after parsing the entire RHS (typical)
      • Can be in the middle of parsing the RHS
        • Sometimes convenient
  • Does LL differ from LR?
    • Yes, LL can evaluate any time, with LR you may get conflicts (when to shift/reduce?)
an ll action routine example
An LL Action Routine Example
  • Given the same LL AG as earlier
  • Insert action routines
  • Predictive nature of LL allows actions at arbitrary times
yacc and bison handling of attributes 1
YACC and Bison Handling of Attributes 1
  • YACC and BISON are LALR(1) parsers
  • Attributes are managed with a stack
    • Augment the parse stack
  • To access attributes in a production
    • The attributes on the LHS is denoted $$
    • The symbol's attributes on the RHS are denoted $1, $2, ..., $n where $i is the ith term from the left
yacc and bison semantic values
YACC and BisonSemantic Values
  • Symbol attributes are pushed on the stack
    • Want to change the stack's element type.
  • To select a stack element type:
    • For single type stacks, define YYTYPE:
      • #define YYTYPE typename // A macro
    • For multi-type stacks, use %union
      • Each field in the union becomes is a type
      • Access them using say $<fieldtype>1
yacc and bison semantic values1
YACC and BisonSemantic Values
  • Attribute initialization:
    • Synthesized Attributes are assigned, using a statement like:
      • $$ = f($1, $2, ..., $n); /* n = |RHS|, f is a function */
    • Sometimes Inherited Attributes are useful
      • e.g. A grammar has productions:
        • Decl } Type Id_List; and, Id_List } Id , Id_List | Id;
      • So need to set an Id's type need a statement like
        • $1.type = $$.type; $2.type = $$.type
yacc and bison semantic values2
YACC and BisonSemantic Values
  • Symbol attributes are pushed on the stack
    • Want to change the stack's element type.
  • To select a stack element type:
    • For single type stacks, define YYTYPE:
      • #define YYTYPE typename // A macro
    • For multi-type stacks, use %union
      • Each field in the union becomes is a type