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.

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

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
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.
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.
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.
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.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
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.
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)
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.
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.
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.
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.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
9/15/2010CS485, Lecture 2, Fall Lecture 2: Introduction to Syntax (Revised based on the Tucker’s slides)
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.
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
Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Concepts of Programming Languages
Syntax (1).
What does it mean? Notes from Robert Sebesta Programming Languages
Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

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 of the language) Syntax - the form or structure of the expressions, statements, and program units Semantics - the meaning of the expressions, statements, and program units

3 Introduction Language description: syntax and semantic Syntax: how program in language are built up Semantic: what program mean Dates represented by D (digit) and Symbol (/) DD/DD/DDDD -> syntax 01/02/2001 -> US Jan 2, 2001 Others Feb 1, 2001 Same syntax, different semantic

4 Organization of Language Description Tutorials Reference Manuals Formal Definition

5 Tutorials What the main constructs of the language are How they are meant to be used Useful examples for imitating and adapting Introduce syntax and semantics gradually

6 Reference Manuals Describing the syntax and semantics Organized around the syntax Formal syntax and informal semantic Informal semantic : English explanations and examples to the syntactic rules Began with the Algol60 : free of ambiguities

7 Formal Definition Precise description of the syntax and semantics Aimed at specialists Attaches semantic rules to the syntax Conflicting interpretations from English explanation Precise formal notation for clarifying subtle point

8 Describing Syntax 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)

9 A Program Fragment Viewed As a Stream of Tokens

10 Describing Syntax Formal approaches to describing syntax: Recognizers - used in compilers Generators – generate the sentences of a language (focus of this lecture)

11 Formal Methods of Describing Syntax Context-Free Grammars Developed by 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

12 CFG for Thai -> -> ฉัน | เธอ | เรา -> กิน | ตี -> ข้าว | สุนัข -> ฉัน กิน ข้าว เธอ ตี ข้าว

13 Formal Methods of Describing Syntax Backus-Naur Form (1959) Invented by Backus and Naur to describe Algol 58 BNF is equivalent to context-free grammars A metalanguage is a language used to describe another language.

14 Backus-Naur Form (1959) Def: A grammar production has the form A -> ω where A is a nonterminal symbol ω is a string of nonterminal and terminal symbols This is a rule; it describes the structure of a while statement  while ( )

15 Formal Methods of Describing Syntax A rule has a left-hand side (LHS) and a right-hand side (RHS), and consists of terminal and nonterminal symbols A grammar is a finite nonempty set of rules An abstraction (or nonterminal symbol) can have more than one RHS  | begin end

16 BNF Nonterminal Identifier Integer Expression Statement Program Terminal The basic alphabet from which programs are constructed binaryDigit -> 0 binaryDigit -> 1 binaryDigit -> 0 | 1 Integer -> Digit | Integer Digit Digit-> 0|1|2|3|4|5|6|7|7|8|9 Integer -> Digit Integer -> Integer Digit Integer -> Integer Integer Digit Integer -> Digit Digit

17 Formal Methods of Describing Syntax Syntactic lists are described using recursion  ident | ident, A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols)

18 Formal Methods of Describing Syntax An example grammar:   | ;  =  a | b | c | d  + | -  | const

19 Derivation Grammar Integer -> Digit | Integer Digit Digit-> 0|1|2|3|4|5|6|7|7|8|9 Is 352 an Integer? Integer => Integer Digit => Integer Integer Digit => Digit Digit Digit => 3 Digit Digit => 35 Digit => 352

20 Formal Methods of Describing Syntax An example derivation: => => = => a = => a = + => a = b + => a = b + const

21 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

22 Parse Tree A hierarchical representation of a derivation const a = b +

23 Parse Tree for the Expression x+2*y

24 Formal Methods of Describing Syntax A grammar is ambiguous if it generates a sentential form that has two or more distinct parse trees

25 An Ambiguous Grammar  | - Two Different Parse Trees for the AmbExp 2 – 3 – 4

26 Is the Grammar Ambiguous?  | const  / | -

27 Is the Grammar Ambiguous? Yes  | const  / | - const --//

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

29 Formal Methods of Describing Syntax Derivation: => - => - => const - => const - / const => const - const / const

30 An Ambiguous If Statement The “Dangling Else” Grammatical Ambiguity

31 Formal Methods of Describing Syntax Extended BNF (just abbreviations): Notation used in the course textbook Optional parts: -> ident ( ) opt Alternative parts: -> [+ | -] const Put repetitions (0 or more) in braces ({ }) -> letter {letter | digit}*

32 Formal Methods of Describing Syntax Extended BNF (just abbreviations): Another frequently used notation Optional parts: -> ident [ ( )] Alternative parts: -> (+ | -) const Put repetitions (0 or more) in braces ({ }) -> letter {letter | digit}

33 BNF and EBNF BNF:  + | - |  * | / | EBNF:  {[+ | -] }*  {[‘*’ | /] }*

34 The Way of Writing Grammars The productions are rules for building string Parse Trees : show how a string can be built Notation to write grammar Backus-Naur Form (BNF) Extended BNF (EBNF) Syntax charts : graphical notation

35 BNF ::= + | - | ::= * | / | ::= number | name | ( )

36 Extended BNF ::= { ( + | - ) } ::= { ( * | / ) } ::= ( ) | name | number

37 Can be used to visualize rules Syntax Diagram expression + - term * / factor name number expression factor term ( )

38 Conventions for Writing Regular Expressions

39 Assignment Draw a parse tree with respect to the BNF grammar on page ( ) * 5 ( ) * ( 3 * 5 )

40 Assignment (cont.) Draw parse trees using the following grammar S ::= id := expr | if expr then S | if expr then S else S | while expr do s | begin SL end SL ::= S ; | S ; SL a. while expr do id := expr b. begin id := expr end c. if expr then if expr then S else S

41 Assignment (cont.) Write the following grammar in Python or your chosen language by using BNF or EBNF or syntax diagram statement While statement (with some examples) If statement (with some examples) Write the keywords in that languages Deadline : next class (28/01/2552)

42 Formal Semantic Static semantic : “compile-time” properties - type correctness, translation - determined from the static text of a program, without running the program on actual data. Dynamic semantic : “run-time” properties - value of expression - the effect of statement - determined by actually doing a computation

43 Semantics or meaning Semantic : any property of a construct The semantic of an expression 2+3 Point of view Semantic An expression evaluatorits value : 5 A type checkertype integer An infix-to-postfix translatorstring: + 2 3

44 Semantic Methods Several method for defining semantics The approaches are used for different purposes by different communities. The values of variables a and b in a+b depend on the environment. An environment consists of bindings from variable to values.

45 References Books Programming Languages: Principles and Paradigms – Allen B. Tucker & Robert E. Noonan Concepts of Programming languages – Robert W. Sebesta Java BNF research/Enseignement/analyseinfo/JAVA/ BNFindex.html research/Enseignement/analyseinfo/JAVA/ BNFindex.html