Advanced programming language theory. week 2. Attribute grammars and semantics.

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type.
CS 330 Programming Languages 09 / 19 / 2006 Instructor: Michael Eckmann.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
CS 355 – Programming Languages
Chapter 1: Preliminaries
Describing Syntax and Semantics
Concepts of Programming Languages 1 Describing Syntax and Semantics Brahim Hnich Högskola I Gävle
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Reasons to study concepts of PL
ISBN Chapter 1 Preliminaries. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter 1 Topics Motivation Programming Domains.
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
ISBN Lecture 01 Preliminaries. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Lecture 01 Topics Motivation Programming.
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
Describing Syntax and Semantics
ISBN Chapter 1 Topics Motivation Programming Domains Language Evaluation Criteria Influences on Language Design Language Categories Language.
ISBN Chapter 3 Describing Syntax and Semantics.
CS 355 – Programming Languages
ISBN Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 3 Syntax - the form or structure of the expressions, statements, and program units Semantics.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages.
Copyright © 2007 Addison-Wesley. All rights reserved.1-1 Reasons for Studying Concepts of Programming Languages Increased ability to express ideas Improved.
ISBN Chapter 3 Describing Syntax and Semantics.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Chapter Describing Syntax and Semantics. Chapter 3 Topics 1-2 Introduction The General Problem of Describing Syntax Formal Methods of Describing.
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
ISBN Chapter 3 Describing Syntax and Semantics.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
Describing Syntax and Semantics
CS 363 Comparative Programming Languages Semantics.
Muhammad Idrees Lecturer University of Lahore 1. Outline Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
ISBN Chapter 3 Describing Semantics.
Chapter 3 Part II Describing Syntax and Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics. Copyright © 2012 Addison-Wesley. All rights reserved. 1-2 Chapter 3 Topics Introduction The General Problem.
Semantics In Text: Chapter 3.
Syntax and Semantics CIS 331 Syntax: the form or structure of the expressions, statements, and program units. Semantics: the meaning of the expressions,
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
CCSB 314 Programming Language Department of Software Engineering College of IT Universiti Tenaga Nasional Chapter 3 Describing Syntax and Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
Chapter 3 © 2002 by Addison Wesley Longman, Inc Introduction - Who must use language definitions? 1. Other language designers 2. Implementors 3.
Describing Syntax and Semantics Session 2 Course : T Programming Language Concept Year : February 2011.
Chapter 3 Describing Syntax and Semantics. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 3 Topics Introduction The General Problem.
ISBN Chapter 3 Describing Syntax and Semantics.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
C HAPTER 3 Describing Syntax and Semantics. D YNAMIC S EMANTICS Describing syntax is relatively simple There is no single widely acceptable notation or.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03CX - Language semantics Programming Language Design.
Describing Syntax and Semantics
Describing Syntax and Semantics
PROGRAMMING LANGUAGES
Describing Syntax and Semantics
Syntax Questions 6. Define a left recursive grammar rule.
Describing Syntax and Semantics
Semantics In Text: Chapter 3.
Describing Syntax and Semantics
- Who must use language definitions?
Presentation transcript:

Advanced programming language theory. week 2. Attribute grammars and semantics.

Remainder. First quiz will be given next Monday (February 6, 2006)

Today's agenda. ● Summary from last lesson ● Exercises ● Translating BNF to Prolog DCG ● Attribute Grammars & static semantics (3.4) ● Dynamic semantics (3.5)

1.1 Reasons for Studying Concepts of Programming Languages ● Increased ability to express ideas ● Improved background for choosing appropriate languages ● Increased ability to learn new languages ● Better understanding of significance of implementation ● Overall advancement of computing

1.6 Language Design Trade-Offs ● Reliability vs. cost of execution – Conflicting criteria – Example: Java demands all references to array elements be checked for proper indexing but that leads to increased execution costs ● Readability vs. writability – Another conflicting criteria – Example: APL provides many powerful operators (and a large number of new symbols), allowing complex computations to be written in a compact program but at the cost of poor readability ● Writability (flexibility) vs. reliability – Another conflicting criteria – Example: C++ pointers are powerful and very flexible but not reliably used

Evaluation Criteria: Cost ● Training programmers to use language ● Writing programs (closeness to particular applications) ● Compiling programs ● Executing programs ● Language implementation system: availability of free compilers ● Reliability: poor reliability leads to high costs ● Maintaining programs

Evaluation Criteria: Others ● Portability – The ease with which programs can be moved from one implementation to another ● Generality – The applicability to a wide range of applications ● Well-definedness – The completeness and precision of the language’s official definition

1.5 Language Categories ● Imperative – Central features are variables, assignment statements, and iteration – Examples: C, Pascal ● Functional – Main means of making computations is by applying functions to given parameters – Examples: LISP, Scheme ● Logic – Rule-based (rules are specified in no particular order) – Example: Prolog ● Object-oriented – Data abstraction, inheritance, late binding – Examples: Java, C++ ● Markup – New; not a programming per se, but used to specify the layout of information in Web documents – Examples: XHTML, XML

Layered View of Computer The operating system and language implementation are layered over Machine interface of a computer

Compilation ● Translate high-level program (source language) into machine code (machine language) ● Slow translation, fast execution ● Compilation process has several phases: – lexical analysis: converts characters in the source program into lexical units – syntax analysis: transforms lexical units into parse trees which represent the syntactic structure of program – Semantics analysis: generate intermediate code – code generation: machine code is generated

The Compilation Process

3.3 Formal Methods of Describing Syntax ● Backus-Naur Form and Context-Free Grammars – Most widely known method for describing programming language syntax ● Extended BNF – Improves readability and writability of BNF ● Grammars and Recognizers

BNF and Context-Free Grammars ● Context-Free Grammars – Developed by Noam Chomsky in the mid-1950s – Language generators, meant to describe the syntax of natural languages – Define a class of languages called context-free languages

BNF Fundamentals ● Non-terminals: BNF abstractions ● Terminals: lexemes and tokens ● Grammar: a collection of rules – Examples of BNF rules: → identifier | identifer, → if then

An Example Grammar   | ;  =  a | b | c | d  + | -  | const

An example derivation => => => = => a = => a = + => a = b + => a = b + const

Parse trees/ abstract syntax trees. ● A hierarchical representation of a derivation const a = b +

An Ambiguous Expression Grammar  | const  / | - const --//

X Y = + Y Z +

X Y = + Y Z +

Associativity of Operators ● Operator associativity can also be indicated by a grammar -> + | const (ambiguous) -> + const | const (unambiguous) const + +

BNF and EBNF ● BNF  + | - |  * | / | ● EBNF  {(+ | -) }  {(* | /) }

BNF and EBNF The two notations can express the same grammars/ languages.

Exercises Probelem sets (p.170): 1-5, 8, 10, 11, 15

Translating BNF to Prolog ● using Prolog as a “gramma tool” to evaluate gramma (BNF) rules. ● will be used in examples, exercises and assignments. ● download from ● follow the hand-out manual (by me).

Follow the instructions from the handout-manual, page 2 and page 3. Repeat the exercise with grammar Example 3.4 (p. 134, text book). Homework for next Monday

ISBN Chapter 3 Describing Syntax and Semantics

3.4 Attribute Grammars ● Context-free grammars (CFGs) cannot describe all of the syntax of programming languages ● Additions to CFGs to carry some semantic info along parse trees ● Primary value of attribute grammars (AGs): – Static semantics specification – Compiler design (static semantics checking)

Attribute Grammars : Definition ● An attribute grammar is a context-free grammar G with the following additions: – For each grammar symbol x there is a set A(x) of attribute values – Each rule has a set of functions that define certain attributes of the nonterminals in the rule – Each rule has a (possibly empty) set of predicates to check for attribute consistency

Attribute Grammars: Definition ● Let X 0  X 1... X n be a rule ● Functions of the form S(X 0 ) = f(A(X 1 ),..., A(X n )) define synthesized attributes ● Functions of the form I(X j ) = f(A(X 0 ),... A(X j-1 )), for i <= j <= n, define inherited attributes ● Initially, there are intrinsic attributes on the leaves

3.4 Attribute Grammars (cont.) ● How are attribute values computed? – If all attributes were inherited, the tree could be decorated in top-down order. – If all attributes were synthesized, the tree could be decorated in bottom-up order. – In many cases, both kinds of attributes are used, and it is some combination of top-down and bottom-up that must be used.

Attribute Grammars: Example ● Syntax -> = -> + | A | B | C actual_type : synthesized for and expected_type : inherited for

An Attribute grammar for simple Assignment Statement. Consider Example 3.6, p. 145.

Parse tree for A=A+B Figure 3.6 (p.146)

3.4 Attribute Grammars (A = A + B) 1..actual_type  lookup (A) (Rule 4) 2..expected_type.actual_type (Rule 1) 3. [2].actual_type  lookup (A) (Rule 4) [3].actual_type <-- lookup (B) (Rule 4) 4..actual_type  either int or real (Rule 2) 5..expected_type ==.actual_type is either TRUE or FALSE (Rule 2)

Dynamic Semantics The “meaning” og programs, statements, expressions, program units at run-time.

Semantics. ● There is no single widely acceptable notation or formalism for describing semantics ● Operational Semantics – Describe the meaning of a program by executing its statements on a machine, either simulated or actual. The change in the state of the machine (memory, registers, etc.) defines the meaning of the statement

3.5 Semantics (cont.) ● To use operational semantics for a high-level language, a virtual machine is needed ● A hardware pure interpreter would be too expensive ● A software pure interpreter also has problems: – The detailed characteristics of the particular computer would make actions difficult to understand – Such a semantic definition would be machine- dependent

3.5 Semantics ● 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

Axiomatic Semantics ● An assertion before a statement (a precondition) states the relationships and constraints among variables that are true at that point in execution ● An assertion following a statement is a postcondition ● A weakest precondition is the least restrictive precondition that will guarantee the postcondition

Axiomatic Semantics ● An axiom for assignment statements (x = E): {Q x->E } x = E {Q} ● The Rule of Consequence:

Axiomatic Semantics ● Pre-post form: {P} statement {Q} ● An example: a = b + 1 {a > 1} One possible precondition: {b > 10} Weakest precondition: {b > 0}

Axiomatic Semantics Program proof process: The postcondition for the whole program is the desired result. Work back through the program to the first statement. If the precondition on the first statement is the same as the program spec, the program is correct.

Axiomatic Semantics ● An inference rule for sequences For a sequence S1;S2: {P1} S1 {P2} {P2} S2 {P3} the inference rule is:

Axiomatic Semantics ● An inference rule for logical pretest loops For the loop construct: {P} while B do S end {Q} the inference rule is: where I is the loop invariant (the inductive hypothesis)

Axiomatic Semantics ● Characteristics of the loop invariant I must meet the following conditions: – P => I (the loop invariant must be true initially) – {I} B {I} (evaluation of the Boolean must not change the validity of I) – {I and B} S {I} (I is not changed by executing the body of the loop) – (I and (not B)) => Q (if I is true and B is false, Q is implied) – The loop terminates (this can be difficult to prove)

Axiomatic Semantics ● The loop invariant I is a weakened version of the loop postcondition, and it is also a precondition. ● I must be weak enough to be satisfied prior to the beginning of the loop, but when combined with the loop exit condition, it must be strong enough to force the truth of the postcondition

3.5 Semantics (cont.) ● Evaluation of axiomatic semantics: – Developing axioms or inference rules for all of the statements in a language is difficult – It is a good tool for correctness proofs, and an excellent framework for reasoning about programs, but it is not as useful for language users and compiler writers – Its usefulness in describing the meaning of a programming language is limited for language users or compiler writers

See you next week. The quiz is on chap 1, chap (incl). The lecture is on Chap 3.5 (cont).