1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.

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
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.
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.
1 CSE305 Programming Languages Syntax What is it? How is it specified? Who uses it? Why is it needed?
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
1 Introduction: syntax and semantics Syntax: a formal description of the structure of programs in a given language. Semantics: a formal description of.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
Chapter 3: Formal Translation Models
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
Chpater 3. Outline The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax.
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.
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.
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 Syntax and Semantics.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
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,
CPS 506 Comparative Programming Languages Syntax Specification.
D Goforth COSC Translating High Level Languages.
ISBN Chapter 3 Describing Syntax and Semantics.
C HAPTER 3 Describing Syntax and Semantics. T OPICS Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
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
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.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
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:
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
Concepts of Programming Languages
What does it mean? Notes from Robert Sebesta Programming Languages
Syntax versus Semantics
Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
Presentation transcript:

1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter 3

2 What is Syntax and Semantics Syntax and Semantics define a PL Syntax –form or structure of program units expressions, statements, declarations, etc. Semantics –meaning of program units expressions, statements, declarations, etc. Why do we need language definitions? –to design a language –to implementer a compiler/interpreter –to write a program (use the language)

3 Syntax Elements 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., *, public, totalCount A token is –a category of lexemes e.g., identifier

4 Describing Syntax Recognizers –read an input string in the alphabet of the language (a sentence) and decide whether it belongs to the language used in compilers –see Chapter 4 for details Generators –produce sentences in a language a sentence is syntactically correct if it can be generated by the generator

5 Backus-Naur Form (BNF) BNF is a meta-language –i.e. a language used to describe another language –invented by John Backus to describe ALGOL 58 –used by Peter Naur to describe ALGOL 60 BNF is equivalent to context-free grammars a BNF grammar is defined by –a set of terminal symbols, –a set of nonterminal symbols –a set of rules –a start symbol (one of the terminal symbols)

6 BNF Elements terminal symbols –are the lexemes of the target PL e.g., while, (, ) nonterminal symbols –represent classes of syntactic structures they act like syntactic variables e.g., rules –define how a nonterminal symbol can by developed into a sequence of nonterminal and terminal symbols e.g.,  while ( )

7 BNF Rules A rule has –a left-hand side (LHS) –then  –a right-hand side (RHS) There can be several rules for one LHS   begin end Syntactic lists are described using recursion  ident  ident, A grammar is –a finite nonempty set of rules

8 EBNF Extended BNF (EBNF) –is most often used –avoids having numerous rules for the same LHS Extra meta-symbols (in addition to  ) –[… ] enclosed symbols are optional (1 or 0 times) –e.g.,  if ( ) [ else ] –{…} enclosed symbols can be repeated (0 to n times) –e.g.,  ident {, ident } –…|… choice of one of the symbol sequences separated by | –e.g.,  | begin end –(…) groups enclosed symbols

9 BNF  +  -   *  /   **   ( )  id EBNF  { ( + | - ) }  { ( * | / ) }  [ ** ]  ( ) | id BNF vs. EBNF

10 Augmented EBNF another meta-symbol = (equal) instead of  meta-symbols for repetitions + means one or more times * means zero or more times = + ( | ) * rules can use iteration instead of recursion –e.g.:  | ; –can be formulated as = ( ; ) *

11 Context-Free Grammar Context-Free Grammars (CFG) –defined by Noam Chomsky –meant to describe the syntax of natural languages Context-Free Grammar G = (S, T, N, P) S = start symbol T = set of terminal symbols – lexemes and tokens N = set of non-terminal symbols - abstractions P = production rules – definition of a LHS abstraction using RHS A sentence –a sequence of terminal symbols

12 A Small Language in EBNF  begin end  | ;  =  + | -  | const  a | b | c

13 Derivation A derivation is –a repeated application of rules starting with the start symbol substitution of a nonterminal LHS by the RHS of a rule ending with a sentence (all terminal symbols) Every string of symbols in the derivation is –a sentential form A sentence is –sentential form with only terminal symbols

14 Derivation Types A leftmost derivation –leftmost nonterminal in each sentential form is expanded first A rightmost derivation –rightmost nonterminal is expanded first A mixed derivation –an arbitrary nonterminal is expanded

15 Derivation Example  begin end  | ;  =  + | -  | const  a | b | c => begin end => begin = end => begin a = end => begin a = + end => begin a = b + end => begin a = b + const end

16 Questions In the preceding slide: 1.Is the derivation a leftmost or a rightmost derivation? 2.State the "opposite" derivation. I.e. if it is a leftmost derivation give rightmost one or vice versa 3.What are the terminal symbols of the language, what are the nonterminal symbols and what is the start symbol? 4.Change a rule so that begin a = - b + const end is a legal sentence

17 Parse Tree Parse Tree is –a hierarchical representation of a derivation const a = b + beginend

18 EBNF Grammar  =  + | * | ( ) |  a | b | c Parse tree of the sentence: a = b * (a + c) Simple Assignment Language a = c * b () a +

19 Ambiguous Grammars A grammar is ambiguous –if and only if it generates a sentential form that has two or more distinct parse trees –e.g.  =  + | * | ( ) |  a | b | c

20 add-first parse tree a = b + c * d multiply-first parse tree a = b + c * d Two Distinct Parse Trees a = d * b + c a = * b + c d

21 An Unambiguous Expression Grammar The same language can be defined with an unambiguous grammar!  =  + |  * |  ( ) |  a | b | c

22 Precedence Through Grammar A grammar can enforce the precedence of operators –The parse tree shows how (low levels are evaluated first) –e.g.,  + |  * const | const * const + const const