Programming Languages 2nd edition Tucker and Noonan

Slides:



Advertisements
Similar presentations
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
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 2nd edition Tucker and Noonan
Chapter 3 Describing Syntax and Semantics Sections 1-3.
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.
COP4020 Programming Languages
S YNTAX. Outline Programming Language Specification Lexical Structure of PLs Syntactic Structure of PLs Context-Free Grammar / BNF Parse Trees Abstract.
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.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
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.
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
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.
CPS 506 Comparative Programming Languages Syntax Specification.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
LESSON 04.
Chapter 3 Context-Free Grammars Dr. Frank Lee. 3.1 CFG Definition The next phase of compilation after lexical analysis is syntax analysis. This phase.
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)
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.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
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.
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
PROGRAMMING LANGUAGES
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.
Describing Syntax and Semantics
CS510 Compiler Lecture 4.
Chapter 3 Context-Free Grammar and Parsing
Chapter 3 – Describing Syntax
Syntax (1).
What does it mean? Notes from Robert Sebesta Programming Languages
Context-Free Grammars
Syntax versus Semantics
Compiler Construction (CS-636)
CSE 3302 Programming Languages
Lecture 3: Introduction to Syntax (Cont’)
Compiler Design 4. Language Grammars
Context-Free Grammars
Programming Languages 2nd edition Tucker and Noonan
Context-Free Grammars
Programming Language Syntax 2
Programming Languages 2nd edition Tucker and Noonan
CSC 4181Compiler Construction Context-Free Grammars
R.Rajkumar Asst.Professor CSE
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Programming Languages 2nd edition Tucker and Noonan
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CSC 4181 Compiler Construction Context-Free Grammars
Theory of Computation Lecture #
Context-Free Grammars
BNF 9-Apr-19.
Discrete Maths 13. Grammars Objectives
Context-Free Grammars
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
COMPILER CONSTRUCTION
Faculty of Computer Science and Information System
Presentation transcript:

Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth

Contents 2.1 Grammars 2.1.1 Backus-Naur Form 2.1.2 Derivations 2.1.3 Parse Trees 2.1.4 Associativity and Precedence 2.1.5 Ambiguous Grammars 2.2 Extended BNF 2.3 Syntax of a Small Language: Clite 2.3.1 Lexical Syntax 2.3.2 Concrete Syntax 2.4 Compilers and Interpreters 2.5 Linking Syntax and Semantics 2.5.1 Abstract Syntax 2.5.2 Abstract Syntax Trees 2.5.3 Abstract Syntax of Clite

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

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 ) then discard then

2.1 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.

2.1.1 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

BNF Grammar Set of productions: P terminal symbols: T nonterminal symbols: N start symbol: A production has the form where and

Example: Binary Digits Consider the grammar: binaryDigit  0 binaryDigit  1 or equivalently: binaryDigit  0 | 1 Here, | is a metacharacter that separates alternatives.

2.1.2 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.

Derivation of 352 as an Integer A 6-step process, starting with: Integer

Use a grammar rule to enable each step: Integer  Integer Digit Derivation of 352 (step 1) Use a grammar rule to enable each step: Integer  Integer Digit

Derivation of 352 (steps 1-2) Replace a nonterminal by a right-hand side of one of its rules: Integer  Integer Digit  Integer 2

Derivation of 352 (steps 1-3) Each step follows from the one before it. Integer  Integer Digit  Integer 2  Integer Digit 2

Derivation of 352 (steps 1-4) Integer  Integer Digit  Integer 2  Integer Digit 2  Integer 5 2

Derivation of 352 (steps 1-5) Integer  Integer Digit  Integer 2  Integer Digit 2  Integer 5 2  Digit 5 2

Derivation of 352 (steps 1-6) You know you’re finished when only terminal symbols remain. Integer  Integer Digit  Integer 2  Integer Digit 2  Integer 5 2  Digit 5 2  3 5 2

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

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. 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.

2.1.3 Parse Trees A parse tree is a graphical representation of a derivation. Each internal node of the tree corresponds to a step in the derivation. Each child of a node represents a symbol on the right- hand side of a production. Each leaf node represents a symbol of the derived string, reading from left to right.

E.g., The step Integer  Integer Digit appears in the parse tree as:

Parse Tree for 352 as an Integer Figure 2.1

Arithmetic Expression Grammar The following grammar defines the language of arithmetic expressions with 1-digit integers, addition, and subtraction. Expr  Expr + Term | Expr – Term | Term Term  0 | ... | 9 | ( Expr )

Parse of the String 5-4+3 Figure 2.2