1 / 5

Semantic Analysis

Semantic Analysis. Allows rigorous specification of a program’s meaning Lets (parts of) programming languages be proven correct Allows (parts of) language translators to be validated Helps language designers discover inconsistency, ambiguity

rjonnie
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 • Allows rigorous specification of a program’s meaning • Lets (parts of) programming languages be proven correct • Allows (parts of) language translators to be validated • Helps language designers discover inconsistency, ambiguity • No one form of semantic analysis is used universally • Appropriate formal semantics must be consistent, complete • Operational Semantics (what we’ll focus on) • Define semantics in terms of abstract machine / operations • Denotational Semantics (machine independent) • Define semantics via mathematical functions over programs • Axiomatic Semantics (prove theorems about program) • Define semantics in terms of predicates and formal logic

  2. Operational Semantics • Augment syntax with rules for an abstract machine • Specifically, a reduction machine that reduces parts of a program (building up to the program itself) to its values • Reduction rules repeatedly infer conclusions from premises • For example, reduction of string “425” to value 425 • Parse off digit ‘4’, convert to value 4 • Parse off digit ‘2’, add value 2 to 10*4 (gives 42) • Parse off digit ‘5’, add value 5 to 10*42 (gives 425) • Axioms (rules without a premise) give basic reductions • E.g., digit and number strings to their corresponding values • Richer inference rules allow reductions to be nested • E.g., convert operands of an addition expression, then add

  3. Environments and Assignment • If a program handles only (e.g., arithmetic) expressions, then it can be reduced to a single value • E.g., ((4+5)*(12-7) – (4*4 + 3*3)) reduces to 425 • However, if assignment statements are added to a language, an environment is needed to store state • E.g., the current values of variables at any point in the code • Environment must be added to reduction rules • E.g., if variables are used in an expression like (a+2)*c • Reductions now are made with respect to an environment • Need additional rules for effects of assignment, etc. • An identifier can reduce to its current value in environment • Assignment updates identifier’s value in the environment • Program reduces to environment after aggregate changes

  4. Attribute Grammars and Parse Trees • Attribute grammars describe relationship between parsing and semantic attributes and their analysis • Can perform one (inline) or two passes (explicit parse tree) • Two kinds of augmentations to the grammar • Copy rules move values from terminals to non-terminals or between non-terminals, by copying the values over • Semantic functions perform richer operations like addition, subtraction, multiplication division, inverse operations, etc. • Attribute flow may matter if coupled to parse order • E.g., S-attributed grammars most general for an LR parse • E.g., L-attributed grammars most general for an LL parse • Action routines can be interleaved with parsing as well • Set up nodes of a parse tree, perform evaluation, or both

  5. Today’s Studio Exercises • We’ll code up ideas from Scott Chapter 4 • Using postfix operations over complex numbers to implement a rudimentary stack based reduction machine • Today’s exercises are again in C++ • Please take advantage of the on-line reference manual pages that are linked on the course web site • As always, please ask us for help as needed • When done, send email with “Semantic Analysis Studio” in the subject line to the cse425@seas.wustl.edu course account

More Related