90 likes | 292 Views
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
E N D
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
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
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
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
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