1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 4: Syntax Specification COMP 144 Programming Language Concepts Spring 2002 Felix.

Slides:



Advertisements
Similar presentations
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Advertisements

Chapter 2 Syntax. Syntax The syntax of a programming language specifies the structure of the language The lexical structure specifies how words can be.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 3: Lexical Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
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.
Context-Free Grammars Lecture 7
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 5: Syntax Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
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
Specifying Languages CS 480/680 – Comparative Languages.
COP4020 Programming Languages
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
1 Syntax Specification Regular Expressions. 2 Phases of Compilation.
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.
Context-Free Grammars
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Grammars CPSC 5135.
PART I: overview material
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.
COMP3190: Principle of Programming Languages DFA and its equivalent, scanner.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
1 Syntax Specification (Sections ) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos at UNC.
1 Lecture 5: Syntax Analysis (Section 2.2) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC.
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.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
ISBN Chapter 3 Describing Syntax and Semantics.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
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.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
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.
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.
Spring 16 CSCI 4430, A Milanova 1 Announcements HW1 will be out this evening Due Monday, 2/8 Submit in HW Server AND at start of class on 2/8 A review.
COMP3190: Principle of Programming Languages DFA and its equivalent, scanner.
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.
CS 3304 Comparative Languages
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
CS 326 Programming Languages, Concepts and Implementation
Chapter 2 :: Programming Language Syntax
Lexical Analysis (Sections )
Chapter 3 – Describing Syntax
CS314 – Section 5 Recitation 3
Formal Language Theory
Department of Software & Media Technology
Programming Language Syntax 2
Chapter 2: A Simple One Pass Compiler
CSC 4181Compiler Construction Context-Free Grammars
CS 3304 Comparative Languages
Lecture 4: Lexical Analysis & Chomsky Hierarchy
CS 3304 Comparative Languages
Chapter 2 :: Programming Language Syntax
CSC 4181 Compiler Construction Context-Free Grammars
Chapter 2 :: Programming Language Syntax
High-Level Programming Language
Context Free Grammars-II
Presentation transcript:

1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 4: Syntax Specification COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos Jan 16 The University of North Carolina at Chapel Hill

2 COMP 144 Programming Language Concepts Felix Hernandez-Campos Phases of Compilation

3 COMP 144 Programming Language Concepts Felix Hernandez-Campos Syntax Analysis Syntax:Syntax: –Webster’s definition: 1 a : the way in which linguistic elements (as words) are put together to form constituents (as phrases or clauses) The syntax of a programming languageThe syntax of a programming language –Describes its form »i.e. Organization of tokens (elements) –Formal notation »Context Free Grammars (CFGs)

4 COMP 144 Programming Language Concepts Felix Hernandez-Campos Review: Formal definition of tokens A set of tokens is a set of strings over an alphabetA set of tokens is a set of strings over an alphabet –{read, write, +, -, *, /, :=, 1, 2, …, 10, …, 3.45e-3, …} A set of tokens is a regular set that can be defined by comprehension using a regular expressionA set of tokens is a regular set that can be defined by comprehension using a regular expression For every regular set, there is a deterministic finite automaton (DFA) that can recognize itFor every regular set, there is a deterministic finite automaton (DFA) that can recognize it –i.e. determine whether a string belongs to the set or not –Scanners extract tokens from source code in the same way DFAs determine membership

5 COMP 144 Programming Language Concepts Felix Hernandez-Campos Review: Regular Expressions A regular expression (RE) is:A regular expression (RE) is: –A single character –The empty string,  –The concatenation of two regular expressions »Notation: RE 1 RE 2 (i.e. RE 1 followed by RE 2 ) –The union of two regular expressions »Notation: RE 1 | RE 2 –The closure of a regular expression »Notation: RE* »* is known as the Kleene star »* represents the concatenation of 0 or more strings

6 COMP 144 Programming Language Concepts Felix Hernandez-Campos Review: Token Definition Example Numeric literals in PascalNumeric literals in Pascal –Definition of the token unsigned_number digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 unsigned_integer  digit digit* unsigned_number  unsigned_integer ( (. unsigned_integer ) |  ) ( ( e ( + | – |  ) unsigned_integer ) |  ) Recursion is not allowed!Recursion is not allowed!

7 COMP 144 Programming Language Concepts Felix Hernandez-Campos Exercise digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 unsigned_integer  digit digit* unsigned_number  unsigned_integer ( (. unsigned_integer ) |  ) ( ( e ( + | – |  ) unsigned_integer ) |  ) Regular expression forRegular expression for –Decimal numbers number  …

8 COMP 144 Programming Language Concepts Felix Hernandez-Campos Exercise digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 unsigned_integer  digit digit* unsigned_number  unsigned_integer ( (. unsigned_integer ) |  ) ( ( e ( + | – |  ) unsigned_integer ) |  ) Regular expression forRegular expression for –Decimal numbers number  ( + | – |  ) unsigned_integer ( (  unsigned_integer ) |  )

9 COMP 144 Programming Language Concepts Felix Hernandez-Campos Exercise digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 unsigned_integer  digit digit* unsigned_number  unsigned_integer ( (. unsigned_integer ) |  ) ( ( e ( + | – |  ) unsigned_integer ) |  ) Regular expression forRegular expression for –Identifiers identifier  …

10 COMP 144 Programming Language Concepts Felix Hernandez-Campos Exercise digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 unsigned_integer  digit digit* unsigned_number  unsigned_integer ( (. unsigned_integer ) |  ) ( ( e ( + | – |  ) unsigned_integer ) |  ) Regular expression forRegular expression for –Identifiers identifier  letter ( letter | digit |  )* letter  a | b | c | … | z

11 COMP 144 Programming Language Concepts Felix Hernandez-Campos Context Free Grammars CFGsCFGs –Add recursion to regular expressions »Nested constructions –Notation expression  identifier | number | - expression | ( expression ) | ( expression ) | expression operator expression | expression operator expression operator  + | - | * | / »Terminal symbols »Non-terminal symbols »Production rule (i.e. substitution rule) terminal symbol  terminal and non-terminal symbols

12 COMP 144 Programming Language Concepts Felix Hernandez-Campos Backus-Naur Form Backus-Naur Form (BNF)Backus-Naur Form (BNF) –Equivalent to CFGs in power –CFG expression  identifier | number | - expression | ( expression ) | ( expression ) | expression operator expression | expression operator expression operator  + | - | * | / –BNF  expression    identifier  |  number  | -  expression  | (  expression  ) | (  expression  ) |  expression   operator   expression  |  expression   operator   expression   operator   + | - | * | /

13 COMP 144 Programming Language Concepts Felix Hernandez-Campos Extended Backus-Naur Form Extended Backus-Naur Form (EBNF)Extended Backus-Naur Form (EBNF) –Adds some convenient symbols »Union| »Kleene star* »Meta-level parentheses( ) –It has the same expressive power

14 COMP 144 Programming Language Concepts Felix Hernandez-Campos Extended Backus-Naur Form Extended Backus-Naur Form (EBNF)Extended Backus-Naur Form (EBNF) –It has the same expressive power BNF  digit   0  digit   1 …  digit   9  unsigned_integer    digit   unsigned_integer    digit   unsigned_integer  EBNF  digit   0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9  unsigned_integer    digit   digit  *

15 COMP 144 Programming Language Concepts Felix Hernandez-Campos Derivations A derivation shows how to generate a syntactically valid stringA derivation shows how to generate a syntactically valid string –Given a CFG –Example: »CFG expression  identifier | number | - expression | ( expression ) | ( expression ) | expression operator expression | expression operator expression operator  + | - | * | / »Derivation of slope * x + intercept

16 COMP 144 Programming Language Concepts Felix Hernandez-Campos Derivation Example Derivation of slope * x + interceptDerivation of slope * x + intercept expression  expression operator expression  expression operator intercept  expression operator intercept  expression + intercept  expression + intercept  expression operator expression + intercept  expression operator expression + intercept  expression operator x + intercept  expression operator x + intercept  expression * x + intercept  expression * x + intercept  slope * x + intercept  slope * x + intercept expression  * slope * x + intercept »Identifiers were not derived for simplicity

17 COMP 144 Programming Language Concepts Felix Hernandez-Campos Parse Trees A parse is graphical representation of a derivationA parse is graphical representation of a derivation ExampleExample

18 COMP 144 Programming Language Concepts Felix Hernandez-Campos Ambiguous Grammars Alternative parse treeAlternative parse tree –same expression –same grammar This grammar is ambiguousThis grammar is ambiguous

19 COMP 144 Programming Language Concepts Felix Hernandez-Campos Designing unambiguous grammars Specify more grammatical structureSpecify more grammatical structure –In our example, left associativity and operator precedence »10 – 4 – 3 means (10 – 4) – 3 »3 + 4 * 5 means 3 + (4 * 5)

20 COMP 144 Programming Language Concepts Felix Hernandez-Campos Example Parse tree for * 5Parse tree for * 5 Exercise: parse tree forExercise: parse tree for - 10 / 5 * 8 – / 5 * 8 – 4 - 5

21 COMP 144 Programming Language Concepts Felix Hernandez-Campos Java Language Specification Available on-lineAvailable on-line – le.doc.html le.doc.htmlhttp://java.sun.com/docs/books/jls/second_edition/html/j.tit le.doc.html ExamplesExamples –Comments: ical.doc.html# ical.doc.html# ical.doc.html#48125 –Multiplicative Operators: pressions.doc.html# pressions.doc.html# pressions.doc.html# –Unary Operators: pressions.doc.html# pressions.doc.html# pressions.doc.html#4990

22 COMP 144 Programming Language Concepts Felix Hernandez-Campos Reading Assignment Scott’s Chapter 2Scott’s Chapter 2 –Section –Section Java language specificationJava language specification –Chapter 2 (Grammars) –Glance at chapter 3 –Glance at sections 15.17, and 15.15