Chapter 3 Part I 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

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
ISBN Chapter 3 More Syntax –BNF –Derivations –Practice.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
A basis for computer theory and A means of specifying languages
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.
1 Note As usual, these notes are based on the Sebesta text. The tree diagrams in these slides are from the lecture slides provided in the instructor resources.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
ISBN Chapter 3 Describing Syntax and Semantics.
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.
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.
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.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
Chpater 3. Outline The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax.
ISBN Chapter 3 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.
Syntax Specification and BNF © Allan C. Milne Abertay University v
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)
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Grammars CPSC 5135.
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 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
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
D Goforth COSC Translating High Level Languages.
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.
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 © 2004 Pearson Addison-Wesley. All rights reserved.3-1 Language Specification and Translation Lecture 8.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Syntax(1). 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical.
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
Chapter 3 – Describing Syntax
What does it mean? Notes from Robert Sebesta Programming Languages
Syntax versus Semantics
Syntax vs Semantics Backus-Naur Form Extended BNF Derivations
Chapter 3 Describing Syntax and Semantics.
Presentation transcript:

Chapter 3 Part I Describing Syntax and Semantics

Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax

1-3 Introduction “In a well-designed programming language, semantics should follow directly from syntax; that is, the appearance of a statement should strongly suggest what the statement is meant to accomplish.” –Robert W. Sebesta –A = A + 1; –A + 1  A

1-4 Describing Syntax: Terminology Language –Sentence Lexeme – Token –Example: a = 2 * c + 3; LexemesTokens aidentifier =equal_sign 2int_literal *mult_op cidentifier +plus_op 3int_literal ;semicolon Recognizer Generator

1-5 Backus-Naur Form (1959) BNF is a meta-language for programming languages; –A description of the syntax rules of the languages BNF Rule Examples – → identifier | identifier, – → if then – -> = –  | begin end Note: Backus’ grammar similar to Chomsky’s Context-free and regulart grammars

Copyright © 2007 Addison-Wesley. All rights reserved.1-6

Copyright © 2007 Addison-Wesley. All rights reserved.1-7 An example of a leftmost derivation of a program in the Small Language  begin end  begin ; end  begin = ; end  begin A = ; end  begin A = + ; end  begin A = B + ; end  begin A = B + C ; end  begin A = B + C ; = end  begin A = B + C ; B = end  begin A = B + C ; B = C end

1-8 BNF Grammar Example 3.2 for a Simple Assignment Statement and example derivation  =  A | B | C  + | * | ( ) | An example leftmost derivation of this grammar:  =.  A =.  A = *.  A = B *.  A = B * ( ).  A = B * ( + ).  A = B * ( A + ).  A = B * ( A + C )

Copyright © 2007 Addison-Wesley. All rights reserved.1-9 Figure 3.1 A parse tree for the simple statement A = B * (A + C)

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

1-11 Compare to Example 3.2 which is not ambiguous:  =  A | B | C  + | * | ( ) | A grammar is ambiguous if it can produce two distinct parse trees for the same expression Ambiguity

1-12 Ambiguous Grammars Figure 3.2 Two distinct parse trees for the same sentence, A = B + C * A

1-13 Unambiguous with consistent precedence Parse tree for: A = B + C * A Note that rightmost and leftmost derivations produce the same parse tree. Compare to Example 3.2 which is not ambiguous but does not enforce precedence.  =  A | B | C  + | * | ( ) |

Copyright © 2012 Addison-Wesley. All rights reserved.1-14 An Unambiguous Expression Grammar If we use the parse tree to indicate precedence levels of the operators, we cannot have ambiguity  - |  / const| const const / -

Copyright © 2012 Addison-Wesley. All rights reserved.1-15 Associativity of Operators Operator associativity can also be indicated by a grammar -> + | const (ambiguous) -> + const | const (unambiguous) const + +

1-16 Associativity of Operators A = B + C + A and Grammar 3.4 For right associativity:  ** |  ( ) | id …

Copyright © 2007 Addison-Wesley. All rights reserved.1-17 Figure 3.5 extra   if then | if then else Consider the sentential form: if then if then else Yields two distinct parse trees (seel right) and is therefore ambiguous Consider: if (done == true) then if (denom == 0) then quotient = 0; else quotient = num / denom; Problem: If the upper parse tree is used, then the outermost if will be associated with the else.

1-18 Unambiguous Grammar for the If Statement Revised Grammar  |  if then else | any non-if statement  if then | if then else Now just one possible parse tree for the following if then if then else

Copyright © 2007 Addison-Wesley. All rights reserved.1-19 EBNF Extended Backus-Naur Form –Uses Metasymbols [], {}, | to make the notation more concise. 1.Optional RHS denoted by brackets []  if ( ) [else ] 2.Zero or more repetitions denoted by braces {}  {, } 3.Choice of a single element from a group denoted by placing options in parentheses and separated by |  ( * | / | % ) Example 3.5 on page 133 (see next slide)

Copyright © 2007 Addison-Wesley. All rights reserved.1-20

Copyright © 2012 Addison-Wesley. All rights reserved.1-21 BNF and EBNF BNF  + | - |  * | / | EBNF  {(+ | -) }  {(* | /) }

Copyright © 2012 Addison-Wesley. All rights reserved.1-22 Recent Variations in EBNF Alternative RHSs are put on separate lines Use of a colon instead of => Use of opt for optional parts Use of oneof for choices

Copyright © 2007 Addison-Wesley. All rights reserved.1-23 Syntax Diagrams Syntax Diagrams (graphs) are used to represent the entire syntactic structure of a parse. Also used to represent the syntax of a single rule. Example: see next slide

Copyright © 2007 Addison-Wesley. All rights reserved.1-24 The syntax diagram (graph) and EBNF descriptions of the Ada if statement

Copyright © 2007 Addison-Wesley. All rights reserved.1-25 © 2003 Brooks/Cole - Thomson Learning ™ More Examples of Syntax Diagrams from Programming Languages Principles and Practice, 2nd ed Kenneth C. Louden Thompson Brooks/Cole ISBN:

Copyright © 2007 Addison-Wesley. All rights reserved.1-26 Grammar/Recognizer Connection Given a context-free grammar for a language, a recognizer (syntax analyzer) can be constructed. One of the first is yacc