1 / 14

3.2 Semantics

3.2 Semantics . Semantics. Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3. Attribute Grammars. CFG cannot describe all necessary information An attribute grammar is a CFG G = (S, N, T, P) with the following additions:

eyal
Télécharger la présentation

3.2 Semantics

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. 3.2 Semantics

  2. Semantics • Attribute Grammars • The Meanings of Programs: Semantics • Sebesta Chapter 3

  3. Attribute Grammars • CFG cannot describe all necessary information • An attribute grammar is • a CFG G = (S, N, T, P) with the following additions: • For each grammar symbol xthere is a set A(x) of attribute values • Each rule has a set of functionsthat define certain attributes of the nonterminals in the rule • Each rule has a (possibly empty) set of predicates to check for attribute consistency

  4. Attribute Grammars (cont.) • Invented by Donald Knuth • Can carry complex syntactic and some semantic information along through parse trees • e.g. complex syntax rule (static semantics): a variable must be defined before it is used • Primary value of AGs: • static semantics specification • compiler design (static semantics checking)

  5. Describing Semantics • There is no single widely accepted notation or formalism for semantics • Different formalisms are used: • Operational Semantics • Axiomatic Semantics • Denotational Semantics

  6. Operational Semantics • Describes the meaning of a program by executing its statements on a machine, either simulated or actual. • The meaning of a statement is defined by the change in the state of the machine (memory, registers, etc.)

  7. Operational Semantics Limitations • A virtual machine is needed • A hardware pure interpreter • too expensive for a high-level language • A software pure interpreter • too difficult to understand the actions because of the detailed characteristics of the particular computer used • machine-dependent

  8. Operational Semantics • A better alternative: A complete computer simulation • The process: • Build a translator which translates source code to the machine code of an idealized computer • Build a simulator for the idealized computer • Evaluation of operational semantics: • Good if used informally (language manuals, etc.) • Extremely complex if used formally (e.g., VDL)

  9. Axiomatic Semantics • Based on formal logic - predicate calculus • Original purpose: formal program verification • Approach: • Define axioms or inference rules for each statement type in the language (to allow transformations of expressions to other expressions) • The expressions are called assertions

  10. Prolog • Prolog – logic programming language • Axiomatic semantics • Contains facts (assertions) • Contains inference rules • Inference process answers a query by generating new facts from known facts and rules • We will learn Prolog in this course to practice the logic programming paradigm

  11. Axiomatic Semantics Evaluation • Good for correctness proofs and for reasoning about programs • Too difficult to develop axioms and inference rules for all of the statements in a high-level language • Too difficult to understand the axioms and rules • It is not very useful for • language design • compiler/interpreter writers • programmers

  12. Denotational Semantics • Based on recursive function theory • The process: • Define a mathematical object for each language entity • Define a function that maps instances of the language entities onto instances of the corresponding mathematical objects • The meaning of language constructs are defined by the values of the program's variables • The state changes are defined by mathematical functions • (operational semantics defines them in program code)

  13. Denotational Semantics Evaluation • The most abstract method of describing semantics • Can be used to prove the correctness of programs • Provides a rigorous way to think about programs • Can aid language design • Has been used in compiler generators • Too complex to be useful to programmers

  14. Summary • EBNF is an established formalism to define the syntax of PLs • There is no single commonly used way to define the semantics – all formalisms have disadvantages

More Related