ISBN 0-321-33025-0 Chapter 3 Describing Syntax and Semantics.

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

ICE1341 Programming Languages Spring 2005 Lecture #4 Lecture #4 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
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.
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.
S YNTAX. Outline Programming Language Specification Lexical Structure of PLs Syntactic Structure of PLs Context-Free Grammar / BNF Parse Trees Abstract.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
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.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
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.
Chapter 3 Part I Describing Syntax and Semantics.
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics 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.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
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.
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.
Syntax and Grammars.
Chapter 3 © 2002 by Addison Wesley Longman, Inc Introduction - Who must use language definitions? 1. Other language designers 2. Implementors 3.
Syntax and Semantics Form and Meaning of Programming Languages Copyright © by Curt Hill.
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.
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
Describing Syntax and Semantics
Chapter 3 – Describing Syntax
What does it mean? Notes from Robert Sebesta Programming Languages
Describing Syntax and Semantics
Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
Presentation transcript:

ISBN Chapter 3 Describing Syntax and Semantics

Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings of Programs: Dynamic Semantics

Copyright © 2006 Addison-Wesley. All rights reserved.3 Introduction Syntax: the form or structure of the expressions, statements, and program units Semantics: the meaning of the expressions, statements, and program units Syntax and semantics provide a language’s definition Users of a language definition include: –Other language designers –Implementers (e.g., compiler writers) –Programmers (the users of the language) need agreement!

Copyright © 2006 Addison-Wesley. All rights reserved.4 The General Problem of Describing Syntax: 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, begin ) A token is a category of lexemes (e.g., identifier)

Copyright © 2006 Addison-Wesley. All rights reserved.5 The General Problem of Describing Syntax: Terminology Example Example: index = 2 * count + 17; LexemesTokens indexidentifier =equal_sign 2int_literal *mult_op countidentifier +plus_op 17int_literal ;semi_colon

Copyright © 2006 Addison-Wesley. All rights reserved.6 Formal Definition of Languages Recognizers –A recognition system reads input strings of the language and decides whether the input strings belong to the language –Have a language L that uses alphabet  of characters – Construct a mechanism R capable of reading strings and indicating whether given string was or was not in L. If accepts only correct sentences, then R is a description of L. –Example: syntax analysis part of a compiler –Detailed discussion in Chapter 4

Copyright © 2006 Addison-Wesley. All rights reserved.7 Formal Definition of Languages Generators –A device that generates sentences of a language –One can determine if the syntax of a particular sentence is correct by comparing it to the structure of the generator –Cuts down on trial-and-error required by recognizer.

Copyright © 2006 Addison-Wesley. All rights reserved.8 Context-Free Grammars –Developed by noted linguist 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 –NOTE: forms of tokens of programming languages can be described by regular grammars

Copyright © 2006 Addison-Wesley. All rights reserved.9 Context-Free Grammar Example -> ->John ->Jill ->car ->hamburger ->a ->the -> ->eats ->drives ->slowly ->frequently Look at it intuitively here, we’ll get to formal definition in a minute….

Copyright © 2006 Addison-Wesley. All rights reserved.10 Context-Free Grammar Derivation Example -> *example from Sudkamp, Languages and Machines: An Introduction to the Theory of Computer Science

Copyright © 2006 Addison-Wesley. All rights reserved.11 Formal Methods of Describing Syntax Backus-Naur Form (BNF) and Context- Free Grammars –Most widely known method for describing programming language syntax Extended BNF –Improves readability and writability of BNF

Copyright © 2006 Addison-Wesley. All rights reserved.12 Backus-Naur Form (BNF) Backus-Naur Form (1959) –Invented by John Backus to describe Algol 58 –Updated by Peter Naur for Algol 60 –BNF is equivalent to context-free grammars –BNF is a metalanguage used to describe another language –In BNF, abstractions are used to represent classes of syntactic structures--they act like syntactic variables (also called nonterminal symbols)

Copyright © 2006 Addison-Wesley. All rights reserved.13 An Example Grammar  begin end  | ;  =  a | b | c | d  + | - |  | const non-terminal terminal

Copyright © 2006 Addison-Wesley. All rights reserved.14 BNF Fundamentals Terminals: lexemes and tokens Non-terminals: BNF abstractions, constructed by grouping smaller constructs. Each nonterminal has a rule to show how it is constructed. Grammar: a collection of rules –Examples of BNF rules: → identifier | identifier, → if then

Copyright © 2006 Addison-Wesley. All rights reserved.15 BNF Rules A rule has a symbol being defined (the left-hand side (LHS)), an arrow, and a right-hand side (RHS) A grammar is a finite nonempty set of rules An nonterminal symbol can have more than one RHS  | begin end

Copyright © 2006 Addison-Wesley. All rights reserved.16 Describing Lists Syntactic lists are described using recursion  ident | ident,

Copyright © 2006 Addison-Wesley. All rights reserved.17 Derivation A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols) => begin end => begin = end => begin a = end => begin a = + end => begin a = b + end => begin a = b + const end

Copyright © 2006 Addison-Wesley. All rights reserved.18 Derivation Every string of symbols in the derivation is a sentential form A sentence is a sentential form that has only terminal symbols 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 Most languages are infinite

Copyright © 2006 Addison-Wesley. All rights reserved.19 Parse Tree A hierarchical representation of a derivation const a = b + beginend

Copyright © 2006 Addison-Wesley. All rights reserved. Correspondence to Code begin a = b + const end begin c = a + b; d = b - const end begin c = a * b; d = b + const; end  begin end  | ;  =  a | b | c | d  + | - |  | const Which ones match the grammar?

Copyright © 2006 Addison-Wesley. All rights reserved. BNF Exercise Add rules to the example expression grammar to create a BNF for the following constructs: –while loop –for loop Use the BNF to create a derivation and parse tree for: begin a = const; while (a > const) b = b + a; a = a – const; end –NOTE: depending on how you defined your while, you may need to add { } or begin/end to this code, remove ; etc.

Copyright © 2006 Addison-Wesley. All rights reserved. Review – where are we? Context-Free Grammars –describes a language by specifying how a sentence can be derived from a set of rules or productions –rules consist of terminals and non-terminals, and a specific start symbol BNF –formal notation for representing context-free grammars Why do we care? –we can use context-free grammars to describe many (but not all!) constructs of programming languages

Copyright © 2006 Addison-Wesley. All rights reserved.23 Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has two or more distinct parse trees Compiler chooses code to generate based on parse tree, if ambiguous may not be able to parse correctly. Characteristics of grammar to determine if ambiguous: –generates sentence with > 1 leftmost derivation –generates sentence with > 1 rightmost derivation What constructs do you think might lead to ambiguity?

Copyright © 2006 Addison-Wesley. All rights reserved.24 Two Grammars for Assignment -> = -> A|B|C -> + | * | ( ) | -> = -> A|B|C -> + | * | ( ) | Allows growth on both left and right.

Copyright © 2006 Addison-Wesley. All rights reserved.25 An Unambiguous Grammar A * B + C A B C * + -> = -> A|B|C -> + | * | ( ) |

Copyright © 2006 Addison-Wesley. All rights reserved.26 An Ambiguous Expression Grammar AB C A B C* * + + A * B + C -> = -> A|B|C -> + | * | ( ) | NOTE: these are two leftmost derivations?

Copyright © 2006 Addison-Wesley. All rights reserved.27 Another issue: Operator Precedence x + y * z –Must ensure that operators with higher precedence are performed first –Higher precedence operations should be lower in the parse tree –Unambiguous grammar does not yield correct precedence. Rightmost operator will be lowest in the parse tree –Must specify separate nonterminal operands with different precedence

Copyright © 2006 Addison-Wesley. All rights reserved.28 Grammar with Precedence -> = -> A|B|C -> + | -> * factor> | -> ( ) | Notice lower precedence listed in earlier rule…

Copyright © 2006 Addison-Wesley. All rights reserved.29 Operator Precedence Derivation A * B + C + * + A * + A * B + A * B + C -> = -> A|B|C -> + | -> * | -> ( ) |

Copyright © 2006 Addison-Wesley. All rights reserved.30 Operator Precedence Parse Tree A * B + C + + * C B A

Copyright © 2006 Addison-Wesley. All rights reserved.31 Associativity of Operators When two operators have the same precedence, the associativity determines the order of evaluation: –A / B * C could be (A/B) * C or A / (B * C) Addition is typically associative, meaning left and right associative orders have same value. Not necessarily true if floating point. Subtraction and division are not associative.

Copyright © 2006 Addison-Wesley. All rights reserved.32 Associativity of Operators When a grammar has its LHS appearing at beginning of RHS, it is left recursive. Specifies left associativity. Left recursion disallows some important syntax analysis algorithms. Grammars must be modified to remove left recursion. Compilers must then enforce, even though not dictated by grammar. Right associativity is specified by right recursion. We’ll do an exercise in Chapter 4 to remove left recursion

Copyright © 2006 Addison-Wesley. All rights reserved.33 Associativity of Operators Right operator associativity can also be indicated by a grammar -> ** | -> ( ) | LHS appears on right, so right associativity

Copyright © 2006 Addison-Wesley. All rights reserved.34 Associativity of Operators A + B + C showing associativity ++ + C B A Notice A added to B, then sum added to C Why? – because is leftmost in rule

Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity Exercise Consider the following If-Else Grammar => if then | if then else | … (assume other types of stmts are possible) Show that this grammar is ambiguous. Hint: try if Expr1 then if Expr2 then Stmt1 else Stmt2 Rewrite the grammar to remove the ambiguity. Hint: Add non-terminals to distinguish two cases (with and without else)

Copyright © 2006 Addison-Wesley. All rights reserved.36 Extended BNF Extensions to BNF enhance readability, not its descriptive power 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}

Copyright © 2006 Addison-Wesley. All rights reserved.37 BNF and EBNF Example BNF  + | - |  * | / | EBNF  {(+ | -) }  {(* | /) }

Copyright © 2006 Addison-Wesley. All rights reserved.38 Attribute Grammars Context-free grammars (CFGs) and BNF cannot easily describe all of the structure of programming languages –type compatibility (e.g., float = int OK, int = float not OK) would require too many rules –declaring all variables before they are referenced can’t be specified in BNF Static semantics –legal form of program (syntax not runtime semantics) –static because checked at compile time

Copyright © 2006 Addison-Wesley. All rights reserved.39 Attribute Grammars Knuth designed Attribute Grammars to describe both syntax & static semantics Basic concepts used at least informally in every compiler* CFGs plus: –attributes (values assigned to grammar symbols) –attribute computation functions (how to compute attribute values) –predicate functions (static semantic rules) * ad hoc methods more often used in compiler implementation, but AG used in other applications, such as natural-language processing

Copyright © 2006 Addison-Wesley. All rights reserved.40 Attribute Grammars : Definition An attribute grammar is a context-free grammar G = (V t, V n, P, S) with the following additions: –For each grammar symbol x there is a set A(x) of attribute values –A(x) consists of two disjoint sets, synthesized attributes S(x) and inherited attributes I(x) –Each grammar 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

Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars: Attributes Synthesized attributes: pass semantic information up a parse tree (e.g., calculated at a child node and passed to parent) Inherited attributes: pass semantic information down and across tree (e.g., passed from parent to child) Intrinsic attributes: synthesized attributes of leaf nodes whose values are determined outside the parse tree (e.g., type info stored in a symbol table)

Copyright © 2006 Addison-Wesley. All rights reserved.42 Attribute Grammars: An Example Idea: define an attribute grammar that can enforce the type rules of an assignment statement Syntax -> = -> + | -> A | B | C Attributes actual_type : synthesized for lhs and expected_type : inherited for Type Requirements Variables can be either int or real. Mixed expression type is always real. If operands are the same, result type matches types of operands. (from child) (from parent – think about declaration)

Copyright © 2006 Addison-Wesley. All rights reserved.43 Attribute Grammar (Example cont) 1.Syntax rule:  = Semantic rules:.expected_type .actual_type 2.Syntax rule:  [2] + [3] Semantic rules:.actual_type .actual_type if ( [2].actual_type = int) and ( [3].actual_type = int) then int else real Predicate:.expected_type ==.actual_type

Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammar (Example cont) 3.Syntax rule:  Semantic rules:.actual_type .actual_type Predicate:.expected_type ==.actual_type 4.Syntax rule:  A|B|C Semantic rules:.actual_type  look-up(.string) (intrinsic)

Copyright © 2006 Addison-Wesley. All rights reserved. Example Parse Tree A = A + B [2] [3] A A B = +

Copyright © 2006 Addison-Wesley. All rights reserved.46 Attribute Grammars (continued) 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.

Copyright © 2006 Addison-Wesley. All rights reserved. Decorated Parse Tree A = A + B Order: 1.Rule 4 (lookup) 2.Rule 1 (var=expr) 3.Rule 4 (lookup) 4.Rule 2 (expr = var + var) 5.Rule 2 (predicate) [2] [3] A A B = + actual type expected type lookup actual type expected type = actual_type

Copyright © 2006 Addison-Wesley. All rights reserved. Evaluation Checking static semantics is essential part of compilers Large number of attributes and rules make attribute grammars difficult to read and write Attribute values on large parse tree are costly to evaluate This is one area where formalism is good to understand, but ad-hoc methods (e.g., symbol table) have prevailed

Copyright © 2006 Addison-Wesley. All rights reserved. EBNF Exercise Do the EBNF COBOL Exercise

Copyright © 2006 Addison-Wesley. All rights reserved.50 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