9/15/2010CS485, Lecture 2, Fall 20101 Lecture 2: Introduction to Syntax (Revised based on the Tucker’s slides)

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.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax Lecture 2 - Syntax, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai.
ISBN Chapter 3 More Syntax –BNF –Derivations –Practice.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Programming Languages 2nd edition Tucker and Noonan
Context-Free Grammars Lecture 7
Chapter 3 Describing Syntax and Semantics Sections 1-3.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 2: Syntax Fall 2009 Marco.
30-Jun-15 BNF. Metalanguages A metalanguage is a language used to talk about a language (usually a different one) We can use English as its own metalanguage.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
Specifying Languages CS 480/680 – Comparative Languages.
COP4020 Programming Languages
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.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
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.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Syntax (Slides mainly based on Tucker.
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.
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.
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.
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.
Dr. Philip Cannata 1 Lexical and Syntactic Analysis Chomsky Grammar Hierarchy Lexical Analysis – Tokenizing Syntactic Analysis – Parsing Hmm Concrete Syntax.
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
Introduction to Parsing
CPS 506 Comparative Programming Languages Syntax Specification.
CSCE 330 Programming Language Structures Syntax (Slides mainly based on Tucker and Noonan) Fall 2012 A language that is simple to parse for the compiler.
Syntax and Semantics Structure of programming languages.
ISBN Chapter 3 Describing Syntax and Semantics.
LESSON 04.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
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.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 3.
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.
Structure of programming languages
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
PROGRAMMING LANGUAGES
Chapter 3 – Describing Syntax
Syntax (1).
CSE 3302 Programming Languages
Lecture 3: Introduction to Syntax (Cont’)
Programming Languages 2nd edition Tucker and Noonan
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Theory of Computation Lecture #
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

9/15/2010CS485, Lecture 2, Fall Lecture 2: Introduction to Syntax (Revised based on the Tucker’s slides)

9/15/2010CS485, Lecture 2, Fall Thinking about Syntax The syntax of a programming language is a precise description of all its grammatically correct programs. Precise syntax was first used with Algol 60, and has been used ever since. Three levels: –Lexical syntax –Concrete syntax –Abstract syntax

9/15/2010CS485, Lecture 2, Fall Levels of Syntax Lexical syntax = all the basic symbols of the language (names, values, operators, etc.) Concrete syntax = rules for writing expressions, statements and programs. Abstract syntax = internal representation of the program, favoring content over form. E.g., –C: if ( expr )...discard ( ) –Ada: if ( expr ) thendiscard then

9/15/2010CS485, Lecture 2, Fall Grammars A metalanguage is a language used to define other languages. A grammar is a metalanguage used to define the syntax of a language. Our interest: using grammars to define the syntax of a programming language.

9/15/2010CS485, Lecture 2, Fall Backus-Naur Form (BNF) Stylized version of a context-free grammar (cf. Chomsky hierarchy) Sometimes called Backus Normal Form First used to define syntax of Algol 60 Now used to define syntax of most major languages

9/15/2010CS485, Lecture 2, Fall BNF Grammar G = {P,T,N S} where set of productions: P terminal symbols: T nonterminal symbols: N start symbol: A production has the form where and

9/15/2010CS485, Lecture 2, Fall Example: Binary String A binary string is a string consisting of any number of binary digits (0 or 1). So G_ {binary} ={P, {binary, binaryDigit}, {0,1}, binary} –P consists of binary  binaryDigit } *, where {..}* are metacharacters that represent the repetition. binaryDigit  0 binaryDigit  1 Or the last two are equivalent to: binaryDigit  0 | 1 Here, | is a metacharacter that separates alternatives.

9/15/2010CS485, Lecture 2, Fall Derivations Consider the grammar: Integer  Digit | Integer Digit Digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 We can derive any unsigned integer, like 352, from this grammar.

9/15/2010CS485, Lecture 2, Fall Derivation of 352 as an Integer A 6-step process, starting with: Integer

9/15/2010CS485, Lecture 2, Fall Derivation of 352 (step 1) Use a grammar rule to enable each step: Integer  Integer Digit

9/15/2010CS485, Lecture 2, Fall Derivation of 352 (steps 1-2) Replace a nonterminal by a right-hand side of one of its rules: Integer  Integer Digit  Integer 2

9/15/2010CS485, Lecture 2, Fall Derivation of 352 (steps 1-3) Each step follows from the one before it. Integer  Integer Digit  Integer 2  Integer Digit 2

9/15/2010CS485, Lecture 2, Fall Derivation of 352 (steps 1-4) Integer  Integer Digit  Integer 2  Integer Digit 2  Integer 5 2

9/15/2010CS485, Lecture 2, Fall Derivation of 352 (steps 1-5) Integer  Integer Digit  Integer 2  Integer Digit 2  Integer 5 2  Digit 5 2

9/15/2010CS485, Lecture 2, Fall Derivation of 352 (steps 1-6) You know you’re finished when there are only terminal symbols remaining. Integer  Integer Digit  Integer 2  Integer Digit 2  Integer 5 2  Digit 5 2  3 5 2

9/15/2010CS485, Lecture 2, Fall A Different Derivation of 352 Integer  Integer Digit  Integer Digit Digit  Digit Digit Digit  3 Digit Digit  3 5 Digit  This is called a leftmost derivation, since at each step the leftmost nonterminal is replaced. (The first one was a rightmost derivation.)

9/15/2010CS485, Lecture 2, Fall Notation for Derivations Integer  * 352 Means that 352 can be derived in a finite number of steps using the grammar for Integer. 352  L(G) Means that 352 is a member of the language defined by grammar G. (You ONLY need to find ONE derivation) L(G) = {   T* | Integer  *  } Means that the language defined by grammar G is the set of all symbol strings  that can be derived as an Integer.