3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #5 Lecture #5 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

ISBN Chapter 3 Describing Syntax and Semantics.
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.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Slide 1 Chapter 2-b Syntax, Semantics. Slide 2 Syntax, Semantics - Definition The syntax of a programming language is the form of its expressions, statements.
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
ISBN Chapter 3 Describing Syntax and Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
Describing Syntax and Semantics
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
ISBN Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 3 Syntax - the form or structure of the expressions, statements, and program units Semantics.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
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)
Grammars CPSC 5135.
Chapter Describing Syntax and Semantics. Chapter 3 Topics 1-2 Introduction The General Problem of Describing Syntax Formal Methods of Describing.
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
ISBN Chapter 3 Describing Syntax and 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
March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Programming Languages (ICE 1341) Lecture #4 Programming Languages (ICE 1341)
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Muhammad Idrees Lecturer University of Lahore 1. Outline Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
CPS 506 Comparative Programming Languages Syntax Specification.
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.
Chapter 3 Describing Syntax and Semantics. Chapter 3: Describing Syntax and Semantics - Introduction - The General Problem of Describing Syntax - Formal.
C HAPTER 3 Describing Syntax and Semantics. T OPICS Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
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.
Chapter 3 Describing Syntax and Semantics
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.
C H A P T E R T W O Syntax and Semantic. 2 Introduction Who must use language definitions? Other language designers Implementors Programmers (the users.
1 CS Programming Languages Class 04 September 5, 2000.
Lecture 3 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03CX - Language semantics Programming Language Design.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Syntax Questions 6. Define a left recursive grammar rule.
Describing Syntax and Semantics
Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
Presentation transcript:

3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing the Meanings of Programs: Dynamic Semantics

3-2 Introduction Syntax: the form or structure of the expressions, statements, and program units, e.g., DD/DD/ DDDD –lexical specification –grammar Semantics: the meaning of the expressions, statements, and program units, e.g., 先月後日 Syntax and semantics provide a language’s definition – Users of a language definition Other language designers Implementers Programmers (the users of the language)

3-3 Terminology A sentence is a string of characters over some alphabet A language is a set of sentences A lexeme is the lowest level syntactic unit of a language (e.g., *, sum, x ), given by the lexical specification A token is a category of lexemes (e.g., identifier)

3-4 Formal Methods of Describing Syntax Context-Free Grammars –Developed by Noam Chomsky in the mid-1950s –meant to describe the syntax of natural languages –Define a class of languages called context-free languages Backus-Naur Form (1959) –Invented by John Backus to describe Algol 58 –BNF is equivalent to context-free grammars –The Most widely known metalanguage, which is used to describe another language Extended BNF –Improves readability and writability of BNF

3-5 Four parts of a Context-Free Grammar a set of terminals: lexemes and tokens, the atomic symbols in the language, a set of nonterminals: abstractions, used to represent constructs in the language; they act like syntactic variables a set of rules (or called productions): –identifying the components of a construct –A rule has a nonterminal as the left-hand side (LHS), and the right-hand side (RHS) may consist of terminal and nonterminal symbols –Examples of a BNF rule: → if then A nonterminal chosen as the starting nonterminal.

3-6 BNF Rules Nonterminals are enclosed between symbols “ ”. An abstraction (or nonterminal symbol) can have more than one RHS. Each alternative separated by “|” is a distinct rule. “  ” is read as “can be”. “|” is read as “or”. Example:  | begin end It sometimes uses subscripts, like [1], on the right side to distinguish between occurrences of a construct.

3-7 Describing Lists Syntactic lists are described using recursion  ident | ident, Example: BNF rules for real numbers .  |  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

3-8 Derivation A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols) Every string of symbols in the derivation is a sentential form, which may consist of nonterminals. A leftmost derivation is one in which the leftmost nonterminal in each sentential form is the one that is expanded A derivation may be neither leftmost nor rightmost

3-9 Example An Example Grammar   | ;  =  a | b | c | d  + | -  | const Example of deviation: => => => = => a = => a = + => a = b + => a = b + const

3-10 Parse Tree A hierarchical representation of a derivation const a = b + => => = => a = => a = + => a = b + => a = b + const

3-11 Parse Tree (cont) Each leaf is labeled with a terminal. Each non leaf node is labeled with a nonterminal. The label of a non leaf node is the left side of some rule, and the labels of the children of the node, from left to right, form the right side of that production. The root is labeled with the starting nonterminal. A parse tree generates the sentence formed by reading the terminals at its leaves from left to right. The construction of a parse tree is called parsing.

3-12 Parser top - down parser : from the root of a parse tree toward the leaves; bottom - up parser: from leaves of a parse tree toward the root;

3-13 Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has two or more distinct parse trees Ambiguity can be resolved by establishing conventions. Example: dangling-else ambiguity Consider the following grammar:  if then  if then else Consider the sentential form: if E1 then if E2 then S1 else S2

3-14 Dangling-else ambiguity (a) corresponds to: if E1 then (if E2 then S1 else S2) (b) corresponds to: if E1 then (if E2 then S1) else S2 It is resolved by matching an else with the nearest unmatched if.

3-15 Expression notations prefix notation: op E1 E2, e.g., * = * = 3000; easy to decode during a left-to-right scan of an expression. postfix notation: E1 E2 op, e.g., *=50 60 *=3000; can be mechanically evaluated with a stack data structure.

3-16 Expression notations (cont) infix notation: E1 op E2 –familiar and easy to read; –without rules for specifying the relative “precedence” of operators, parentheses would be needed in expressions to make explicit the operands of an infix operator, e.g., a+b*c  a+(b*c). An operator is “ left associate” if subexpressions containing multiple occurrences of the operator are grouped from left to right, –e.g  (4-2)-1 An operator is “ right associate” if subexpressions containing multiple occurrences of the operator are grouped from right to left, –e.g. x=y=3  x=(y=3)

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

3-18 An Unambiguous Expression Grammar If we use the parse tree to indicate precedence levels of the operators, we cannot have ambiguity  - |  / const| const const / -

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

3-20 Associativity of Operators (cont) (1) -> + number (2) -> number + | - number | number - | number | number Although both grammars are unambiguous, (1) is more suitable for left associate operators, because its parse tree grows down and to the left, which is close to the semantics. L L L number 1 number 2 number R R R - - number 1 number 2 number 4

3-21 Handling Associativity and Precedence The syntax of expressions in a language can be characterized by a table giving the associativity and precedence of operators. Suppose we have a table, where all operators on the same line have the same associativity and precedence. (see the next page) A grammar for expressions can be designed by choosing a nonterminal for each precedence level, and an additional nonterminal for the smallest subexpression (factors).

3-22 Handling Associativity and Precedence (cont) Example of three-level operators: [A] = right associative [E] + - left associative [T] * / left associative [F] factors The grammar is: -> = | -> + | – | -> * | / | -> ( ) | name | number

3-23 Extended BNF Optional parts are placed in brackets [ ] -> ident [( )] Alternative parts of RHSs are placed inside parentheses and separated via vertical bars → (+|-) const Repetitions (0 or more) are placed inside braces { } → letter {letter|digit}

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

3-25 Attribute Grammars Context-free grammars (CFGs) cannot describe all of the syntax of programming languages. For example, all variables must be declared before they are referenced. attribute grammars (AGs): additions to CFGs to carry some static semantic information along parse trees Static semantics are related to the legal form of a program, not directly related to the meaning of programs during execution. Many static semantic rules state the type constraints of a language. Primary value of attribute grammars (AGs) –Static semantics specification –Compiler design (static semantics checking)

3-26 Attribute Grammars : Definition An attribute grammar is a context-free grammar with the following additions: –For each grammar symbol x there is a set A(x) of attribute values –Each rule has a set of semantic 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

3-27 Attribute Grammars: Definition (cont) 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, which are used to pass semantic information up a parse tree. Functions of the form I(X j ) = f(A(X 0 ),..., A(X n )), for 1<= j <= n, define inherited attributes, which pass semantic information down and across a tree. Initially, there are intrinsic attributes on the leaves, whose values are determined outside the parse tree. For example, the types of variables come from the symbol table.

3-28 Attribute Grammars: An Example Syntax -> = -> + | -> A | B | C Attributes –actual_type : synthesized for and –expected_type : inherited for We assume the variables can be one of two types: int or real. In the next page, the look-up function looks up a given variable name in the symbol table and returns the type.

3-29 Example of an Attribute Grammar (cont) 1.Syntax rule:  = Semantic rules:.expected_type .actual_type 2. Syntax rule:  [2] + [3] Semantic rules:.actual_type  if ( [2].actual_type == int) and ( [3].actual_type == int) then int else real end if Predicate:.actual_type ==.expected_type 3. Syntax rule:  Semantic rules:.actual_type .actual_type Predicate:.actual_type ==.expected_type 4. Syntax rule:  A | B | C Semantic rule:.actual_type  lookup (.string)

3-30 Computing Attribute Values 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.

3-31 Example of Computing Attribute Values For the sentence: A = A + B 1..actual_type  look-up(A) (Rule4) 2..expected_type .actual_type (Rule1) 3. [2].actual_type  lookup (A) (Rule4) [3].actual_type  lookup (B) (Rule4) 4..actual_type  either int or real (Rule2) 5..expected_type ==.actual_type is either TRUE or FALSE (Rule2)

3-32 Example of Computing Attribute Values (cont)

3-33 Semantics There is no single widely acceptable notation or formalism for describing semantics Axiomatic Semantics –Based on formal logic (predicate calculus) –Axioms or inference rules are defined for each statement type in the language, to state the meaning of statements and programs. –The main purpose is for formal program verification. We will talk about this in Chapter 8.