1 / 48 Formal a Language Theory and Describing Semantics 886340 Principles of Programming Languages 4.

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.
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.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
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
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 / 18 / 2007 Instructor: Michael Eckmann.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Operational Semantics.
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 3 Describing Syntax and Semantics.
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.
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.
Course: ICS313 Fundamentals of Programming Languages. Instructor: Abdul Wahid Wali Lecturer, College of Computer Science and Engineering.
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.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Semantics In Text: Chapter 3.
Chapter 3 Describing Syntax and Semantics. Chapter 3: Describing Syntax and Semantics - Introduction - The General Problem of Describing Syntax - Formal.
Syntax and Semantics CIS 331 Syntax: the form or structure of the expressions, statements, and program units. Semantics: the meaning of the expressions,
Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has.
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.
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.
Advanced programming language theory. week 2. Attribute grammars and semantics.
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
Describing Syntax and Semantics
Describing Syntax and Semantics
Syntax Questions 6. Define a left recursive grammar rule.
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
- Who must use language definitions?
Presentation transcript:

1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4

2 / 48 Content Formal a Language Theory Regular Expressions Finite State Automaton (FA) Describing Semantics Operation Denotational axiomatic

3 / 48 Review A grammar can be ambiguous – i.e. more than one parse tree for same string of terminals – in a PL we want to base meaning on parse so – ambiguous parse -> ambiguous meaning -> Especially grammars for expressions – precedence associativity

4 / 48 Review

5 / 48 Review Context Free Grammars (CFGs) are used to specify the overall structure of a programming language: – if/then/else,... – brackets: ( ), { }, begin/end,... Regular Grammars (RGs) are used to specify the structure of tokens: – identifiers, numbers, keywords,...

6 / 48 Formal a Language Theory Offers a way to describe computation problems formulated as language recognition problems – Enables proofs of relative difficulty of certain computational problems Provides a mechanism to aid description of programming language constructs – Regular expressions ~ PL tokens (e.g., keywords) - Finite state automata (FSAs) – Context-free grammars ~ PL statements

7 / 48 Formal a Language Theory Recognizers for languages are more complex as the languages themselves become more complex – Simple constructs correspond to FSAs Keywords, numerical constants – More complex constructs correspond to Push-down Automata If statements, looping statements, declarations – Even more complex constructs correspond to more complex automata Type checking of use with declared type

8 / 48 Regular Expressions Formalism for describing simple PL constructs – reserved words – identifiers – numbers Simplest sort of structure Recognized by a finite state automaton Defined recursively

9 / 48 Regular Expressions PL constructRE NotationLanguage an empty{ } symbol aa{a} null symbol R,S regular exprs a,b terminals R | S a|b (alternation) R,S regular exprs a,b terminals RS ab (concatenation) L R L S {ab}

10 / 48 Regular Expressions PL constructRE NotationLanguage R regular exprs a R* a* R regular exprs a R+a+R+a+

11 / 48 Regular Expressions

12 / 48 RE’s for PLs Let letter stand for a|b|c|...|z and digit stand for 0|1|2|3|4|5|6|7|8|9 – letter (letter | digit)* is identifier – digit + is an integer constant – digit*. digit + is real number Which identifiers are described by – letter (letter | digit)* ? ABC 0C B% X1 Louden uses [0-9] [a-z]([a-z] | [0-9])* [0-9] + [0-9]*. [0-9] +

13 / 48 Examples Which of the following are legal real numbers described by – digit *. digit + ? Can see that simple PL constructs can be defined as regular expressions – Can you define a number in scientific notation as an RE? (e.g., 1.25e+20,.05e-3)

14 / 48 Finite State Automaton (FA) Described by Example:

15 / 48 Finite State Automaton (FA) FA accepts or recognizes an input string if there is a path from its start state to a final state such that the labels on the path are the terminals in that string. What strings are recognized?

16 / 48 Finite State Automaton (FA) Binary numbers containing a pair of adjacent 1’s: Recognizes: (0 | 1) * 1 1 (0 | 1) *

17 / 48 Finite State Automaton (FA) Binary numbers containing a pair of adjacent 1’s: Recognizes: (0 | 1) * 1 1 (0 | 1) * FA1 and FA2 recognize the same set of strings, i.e., the same language! Therefore, FAs are not unique.

18 / 48 Finite State Automaton (FA) Exponent in scientific notation: Recognizes: E (+ | -) digit + | E digit +

19 / 48 Finite State Automaton (FA) Binary numbers which begin and end with a 1: Recognizes: 1(0|1)*1

20 / 48 Finite State Automaton (FA) Binary numbers containing at least one digit, in which all the 1’s precede all the 0’s: Recognizes: 0 + | 1 + 0*

21 / 48 Recognition of a string – Is this given string in the language described (recognized) by this given RE (FA)? Description of a language – Given an RE (FA), what language does it describe (recognize)? Codification of a language – Given a language, find an RE and an FA that corresponds to it Tasks for REs and FAs

22 / 48 Tasks for REs and FAs Recognition of a string – Given 10*, which of these strings is described by it: 1, 00, 10, 1000, 01 – Given the following FA: which of these strings is recognized by it: 1, 00, 10, 1000, 01

23 / 48 Tasks for REs and FAs Description of a language – What language is described by the following RE: (01) + | (10) + – What language is recognized by the following FA:

24 / 48 Tasks for REs and FAs Description of a language – What language is recognized by the following FA:

25 / 48 Tasks for REs and FAs Codification of a language – Complex constants are parenthesized pairs of integers – Let digit = 0|1|2|3|4|5|6|7|8|9 – The RE for complex constants is: ( digit +, digit + ) – The FA for complex constants is:

26 / 48 Describing Semantics

27 / 48 Copyright © 2012 Addison- Wesley. All rights reserved. Attribute Grammars Attribute grammars (AGs) have additions to CFGs to carry some semantic info on parse tree nodes Primary value of AGs: Static semantics specification Compiler design (static semantics checking)

28 / 48 Copyright © 2012 Addison- Wesley. All rights reserved. Attribute Grammars : Definition Def: An attribute grammar is a context-free grammar G = (S, N, T, P) 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

29 / 48 Copyright © 2012 Addison- Wesley. All rights reserved. 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 n )), for i <= j <= n, define inherited attributes Initially, there are intrinsic attributes on the leaves

30 / 48 Copyright © 2012 Addison- Wesley. All rights reserved. Attribute Grammars: An Example Syntax  =  + |  A | B | C actual_type : synthesized for and expected_type : inherited for

31 / 48 Attribute Grammars: An Example

32 / 48 A parse tree of the sentence A = A + B Attribute Grammars: An Example

33 / 48 Attribute Grammars: An Example

34 / 48 The flow of attributes in the tree In this example, A is defined as a real and B is defined as an int. Attribute Grammars: An Example

35 / 48 A fully attributed parse tree Attribute Grammars: An Example

36 / 48 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 To use operational semantics for a high-level language, a virtual machine is needed Copyright © 2012 Addison- Wesley. All rights reserved.

37 / 48 Copyright © 2012 Addison- Wesley. All rights reserved. Operational Semantics 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 A better alternative: A complete computer simulation The process: Build a translator (translates source code to the machine code of an idealized computer) Build a simulator for the idealized computer

38 / 48 Denotational Semantics Based on recursive function theory The process of building a denotational specification for a language: - 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 only the values of the program's variables Copyright © 2012 Addison- Wesley. All rights reserved.

39 / 48 The meaning of binary numbers using denotational semantics, we associate the actual meaning (a decimal number) The semantic function, named M bin M bin ('0') = 0 M bin ('1') = 1 M bin ( '0') = 2 *M bin ( ) M bin ( '1') = 2 *M bin ( ) + 1 Binary Numbers

40 / 48 Decimal Numbers  '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | ('0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9') M dec ('0') = 0, M dec ('1') = 1, …, M dec ('9') = 9 M dec ( '0') = 10 * M dec ( ) M dec ( '1’) = 10 * M dec ( ) + 1 … M dec ( '9') = 10 * M dec ( ) + 9 Copyright © 2012 Addison- Wesley. All rights reserved.

41 / 48 Copyright © 2012 Addison- Wesley. All rights reserved. Axiomatic Semantics Based on formal logic (predicate calculus) Original purpose: formal program verification Axioms or inference rules are defined for each statement type in the language The logic expressions are called assertions

42 / 48 Copyright © 2012 Addison- Wesley. All rights reserved. Axiomatic Semantics (continued) 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

43 / 48 Copyright © 2012 Addison- Wesley. All rights reserved. Axiomatic Semantics Form Pre-, post form: {P} statement {Q} examples a = b + 1 {a > 1} One possible precondition: {b > 10} Weakest precondition: {b > 0} sum = 2 * x + 1 {sum > 1} Weakest precondition: ???

44 / 48 examples a = b / {a < 10} Weakest precondition:?? x = x + y - 3 {x > 10} Weakest precondition: {y > 13 – x} Axiomatic Semantics Form

45 / 48 examples y = 3 * x + 1; x = y + 3; {x < 10} The precondition for the second assignment statement is y < 7 The precondition for the first assignment statement 3 * x + 1 < 7 x < 2 So, {x < 2} is the precondition of both Axiomatic Semantics Form

46 / 48 Copyright © 2012 Addison- Wesley. All rights reserved. Program Proof Process The postcondition for the entire 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 specification, the program is correct.

47 / 48 Copyright © 2012 Addison- Wesley. All rights reserved. Summary BNF and context-free grammars are equivalent meta- languages Well-suited for describing the syntax of programming languages An attribute grammar is a descriptive formalism that can describe both the syntax and the semantics of a language Three primary methods of semantics description Operation, denotational, axiomatic

48 / 48 Conclusion and Question