1 Introduction: syntax and semantics Syntax: a formal description of the structure of programs in a given language. Semantics: a formal description of.

Slides:



Advertisements
Similar presentations
Chapter Chapter Summary Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output Language Recognition Turing.
Advertisements

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.
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.
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.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
PZ02A - Language translation
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?
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.
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.
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.
Chpater 3. Outline The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax.
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.
A sentence (S) is composed of a noun phrase (NP) and a verb phrase (VP). A noun phrase may be composed of a determiner (D/DET) and a noun (N). A noun phrase.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Grammars CPSC 5135.
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
ProgrammingLanguages Programming Languages Language Syntax This lecture introduces the the lexical structure of programming languages; the context-free.
C H A P T E R TWO Syntax and Semantic.
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.
Copyright © by Curt Hill Grammar Types The Chomsky Hierarchy BNF and Derivation Trees.
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,
CPS 506 Comparative Programming Languages Syntax Specification.
D Goforth COSC Translating High Level Languages.
Chapter 3 Describing Syntax and Semantics. Chapter 3: Describing Syntax and Semantics - Introduction - The General Problem of Describing Syntax - Formal.
Chapter 3 Describing Syntax and Semantics
1 Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
ISBN Chapter 3 Describing Syntax and Semantics.
Syntax and Grammars.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Chpater 3. Outline The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax.
9/15/2010CS485, Lecture 2, Fall Lecture 2: Introduction to Syntax (Revised based on the Tucker’s slides)
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.
Organization of Programming Languages Meeting 3 January 15, 2016.
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
Describing Syntax and Semantics
Introduction to Formal Languages
Concepts of Programming Languages
Chapter 3 – Describing Syntax
Concepts of Programming Languages
What does it mean? Notes from Robert Sebesta Programming Languages
Automata and Languages What do these have in common?
September 13th Grammars.
Plan for Lecture Review code and trace output
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Presentation transcript:

1 Introduction: syntax and semantics Syntax: a formal description of the structure of programs in a given language. Semantics: a formal description of the meaning programs in a given language. Together the syntax and semantics define a language. Who uses a language definition? –Those who design a language –Those who implement a language (e.g. write compilers for it) –The who use the language (i.e. software developers) –Those who make tools for developers (e.g. JDT in Eclipse) A given language can have more than one grammar which describes it. –Grammar presented to users not necessarily grammar used in implementation!

2 Syntax and semantics of programming languages I have cautioned against getting too hung up on the syntax of a programming language. But, you still need to learn the syntax of any language you work with so that you can read and write programs in the language. To understand the meaning of programs expressed in a language you also have know the semantics of the language.

3 Chomsky hierarchy Noam Chomsky defined a hierarchy of grammars and languages known as the Chomsky hierarchy: –regular languages (most restrictive) –context-free languages –context-sensitive languages –unrestricted languages (least restrictive)

4 Context-free (CF) grammar A CF grammar is formally presented as a 4-tuple G=(T,NT,P,S), where: –T is a set of terminal symbols –NT is a set of non-terminal symbols –P is a set of productions (or rules), where P  NT  (N  NT)* –S  NT

5 General examples Example 1 L = { 0, 00, 1, 11 } G = ( {0,1}, {S}, { S  0, S  00, S  1, S  11 }, S )

6 General examples Example 2 L = { the dog chased the dog, the dog chased a dog, a dog chased the dog, a dog chased a dog, the dog chased the cat, … } G = ( {a, the, dog, cat, chased}, {S,NP,VP,Det,N,V}, { S  NP VP, NP  Det N, Det  a|the, N  dog|cat, VP  V | VP NP, V  chased }, S )

7 Language terminology (from Sebesta, p. 125) A language is a set of strings of symbols, drawn from some finite set of symbols (called the alphabet of the language). “The strings of a language are called sentences” “Formal descriptions of the syntax […] do not include descriptions of the lowest-level syntactic units […] called lexemes.” “A token of a language is a category of its lexemes.” Syntax of a programming language is often presented in two parts: –regular grammar for token structure (e.g. structure of identifiers) –context-free grammar for sentence structure

8 Examples of lexemes and tokens LexemesTokens fooidentifier i sumidentifier -3integer_literal 10integer_literal 1 ;statement_separator =assignment_operator

9 Backus-Naur Form (BNF) Backus-Naur Form (1959) –Invented by John Backus to describe ALGOL 58, modified by Peter Naur for ALGOL 60 –BNF is equivalent to context-free grammars –BNF is a metalanguage used to describe another language, the object language –Extended BNF: adds syntactic sugar to produce more readable descriptions

10 BNF Fundamentals Sample rules [p. 128] → = → if then → if then else non-terminals/tokens surrounded by lexemes are not surrounded by keywords in language are in bold → separates LHS from RHS | expresses alternative expansions for LHS → if then | if then else = is in this example a lexeme

11 BNF Rules A rule has a left-hand side (LHS) and a right- hand side (RHS), and consists of terminal and nonterminal symbols A grammar is often given simply as a set of rules (terminal and non-terminal sets are implicit in rules, as is start symbol)

12 Describing Lists There are many situations in which a programming language allows a list of items (e.g. parameter list, argument list). Such a list can typically be as short as empty or consisting of one item. Such lists are typically not bounded. How is their structure described?

13 Describing lists The are described using recursive rules. Here is a pair of rules describing a list of identifiers, whose minimum length is one:  ident | ident, Notice that ‘, ’ is part of the object language

14 Derivation of sentences from a grammar A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols) Example: derivation of the dog chased a cat S  NP VP  Det N VP  the N VP  the dog VP  the dog V NP  the dog chased NP  the dog chased Det N  the dog chased a N  the dog chased a cat