EBNF: A Notation for Describing Syntax n Languages and Syntax n EBNF Descriptions and Rules n More Examples of EBNF n Syntax and Semantics n EBNF Description.

Slides:



Advertisements
Similar presentations
15-Dec-14 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.
Advertisements

C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Chapter Chapter Summary Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output Language Recognition Turing.
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.
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.
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
PZ02A - Language translation
Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars.
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?
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.
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.
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.
Copyright © Cengage Learning. All rights reserved.
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.
Definition A string is a sequence of symbols. Examples “Hi, Mom.” “YAK” “abbababba” Question In what ways do programmers use strings?
Syntax and Backus Naur Form
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
Languages, Grammars, and Regular Expressions Chuck Cusack Based partly on Chapter 11 of “Discrete Mathematics and its Applications,” 5 th edition, by Kenneth.
CS 331, Principles of Programming Languages Chapter 2.
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.
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 3 Describing Syntax and Semantics
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
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.
CS 331, Principles of Programming Languages Chapter 2.
Syntax and Grammars.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Syntax and Semantics Form and Meaning of Programming Languages Copyright © by Curt Hill.
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.
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.
BNF A CFL Metalanguage Some Variations Particular View to SLK Copyright © 2015 – Curt Hill.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Topic 3: Automata Theory 1. OutlineOutline Finite state machine, Regular expressions, DFA, NDFA, and their equivalence, Grammars and Chomsky hierarchy.
Chapter 3 – Describing Syntax
PROGRAMMING LANGUAGES
CSC 8310 Programming Languages
Chapter 3 – Describing Syntax
Concepts of Programming Languages
Automata and Languages What do these have in common?
Syntax versus Semantics
What are the names of the Meta Languages you have used?
High-Level Programming Language
Programming Languages 2nd edition Tucker and Noonan
COMPILER CONSTRUCTION
Presentation transcript:

EBNF: A Notation for Describing Syntax n Languages and Syntax n EBNF Descriptions and Rules n More Examples of EBNF n Syntax and Semantics n EBNF Description of Sets n Advanced EBNF (recursion)

Quote of the Day “When teaching a rapidly changing technology, perspective is more important than content.”

Why Study EBNF EBNF is a notation for formally describing syntax: how to write symbols in a language. We will use EBNF to describe the syntax of Java. But there is a more compelling reason to begin our study of programming with EBNF: it is a microcosm of programming. There is a strong similarity between the control forms of EBNF and the control structures of Java: sequence, decision, repetition, recursion, and the ability to name descriptions. There is also a strong similarity between the process of writing EBNF descriptions and writing Java programs. Finally studying EBNF introduces a level of formality that will continue throughout the semester.

Languages and Syntax n EBNF: Extended Backus-Naur Form n John Backus (IBM) invented a notation called BNF l He used it to describe FORTRAN’s syntax (1956) n Peter Naur popularized BNF l He used it to describe ALGOL's syntax (1958) s Niklaus Wirth used and Extended form of BNF (called EBNF) to describe the syntax of his Pascal programming language (1976) n Noam Chomsky (MIT linguist and philospher) l Invented a Hierarchy of Notations for Natural Languages l 4 levels: 0-3 with 0 being the most powerful l BNF is at level 2; programming languages are at level 0 n Formal Languages and Computability l is the study of different families of notations and their power

EBNF Descriptions and Rules n Each Description is a list of Rules Rule Form: LHS  RHS (read  as “is defined as”) n Rule Names (LHS) are italicized, hyphenated words n Control Forms in RHS l SequenceItems appear left to right; order is important ChoiceAlternatives separated by | (stroke); exactly one item is chosen from the alternatives OptionOptional item enclosed between [ and ] ; it can be included or discarded Repetition Repeatable item enclosed between { and } ; it can be repeated 0 or more times

An EBNF Description of Integers A symbol (sequence of characters) is classified legal by an EBNF rule if we can process all the characters in the symbol when we reach the end of the right hand side of the EBNF rule. digit  0|1|2|3|4|5|6|7|8|9 integer  [+|-]digit{digit} digit is defined as any of the alternatives 0 through 9 integer is defined as a sequence of three items: (1) an optional sign (if it is included, it must be the alternative + or - ), followed by (2) any digit, followed by (3) a repetition of zero or more digit s. The integer RHS combines and illustrates all EBNF control forms: sequence, option, alternative, repetition.

Proofs In English Is the symbol 7 an integer ? Yes, the proof: In the integer EBNF rule, start with the optional sign; discard the option. Next in the sequence is a digit : choose the 7 alternative. Next in the sequence is a repetition; choose 0 repetitions. End of symbol & integer reached. Is the symbol +127 an integer ? Yes, the proof. In the integer EBNF rule, start with the optional sign; include the option; choose the + alternative. Next in the sequence is a digit : choose the 1 alternative. Next in the sequence is a repetition; choose 2 repetitions; choose the 2 alternative for the first; choose the 7 alternative for the second. End of symbol & integer reached. Are the symbols 1,024 A an integer ?

Tabular Proof Tabular Proof Replacement Rules (1) Replace a name (LHS) by its definition (RHS) (2) Choose an alternative (3) Include or Discard an Option (4) Choose the number of repetitions StatusReason integer Given [+|-]digit{digit} Replace LHS by RHS (1) [+]digit{digit} Choose + alternative (2) +digit{digit} Include option (3) +1{digit} Replace digit by 1 alternative (1&2) +1digit digit Choose two repetitions (4) +12digit Replace digit by 2 alternative (1&2) +127 Replace digit by 7 alternative (1&2)

Graphical Proof integer [+|-]digit{digit} [+]1digit +27 A graphical proof replaces multiple (equivalent) tabular proofs, since the order of rule application (which is unimportant) is often absent in graphical proofs.

Identical vs Equivalent Descriptions sign  +|- digit  0|1|2|3|4|5|6|7|8|9 integer  [sign]digit{digit} x  +|- y  0|1|2|3|4|5|6|7|8|9 z  [x]y{y} These two descriptions are not identical but they are equivalent: Although they use different EBNF rule names (consistently), asking whether a symbol is an integer is the same as asking whether the symbol is a z.

Two Problematical Descriptions A “simplified but equivalent” definition of integer ? sign  +|- digit  0|1|2|3|4|5|6|7|8|9 integer  [sign]{digit} A “good” definition of integers with commas ( 1,024 )? sign  +|- comma-digit  0|1|2|3|4|5|6|7|8|9|, comma-integer  [sign]comma-digit{comma-digit} Both definitions classify “non-obvious” symbols as legal integer or comma-integer. Find such symbols.

Syntax and Semantics n Syntax = Form n Semantics = Meaning n Key Questions l Can two different symbols have the same meaning? l Can a symbol have many meanings (depending on context)? n Do the following symbols have the same meaning? 1 and +1, and and 9.0 Rich and rich n EBNF specifies syntax, not semantics l Semantics is supplied informally: English, examples,... l Formal semantics is a research area in CS, AI, Linguistics,...

Structured Integers Allow non-adjacent embedded underscores to add a special structure to a number 2_10_54 1_800_555_1212 1_000_000 (compared to ; figure each value fast) Define structured-integer digit  0|1|2|3|4|5|6|7|8|9 structured-integer  [sign]digit{[_]digit} Semantically, the underscore is ignored 1_2 has the same meaning as 12 How can we fix the date problem: 12_5_1987 and 1_25_1987

Syntax Charts ABCD A B C D A [A] ABCDABCD A|B|C|D A {A} Sequence Option Choice Repetition

Syntax Charts for integer and digit digit + integer -

A Syntax Chart with no other names integer Which Syntax chart for integer is simpler? The previous one (because it is smaller) or this one (because it it doesn’t need another name for digit )?

Interesting Rules & Their Charts A B {A|B} AB C AB| C A B {A}|{B}

Description of Sets n Set syntax Sets start with ( and end with ) Sets contain 0 or more integer s A comma appears between every pair of integer s integer-list  integer{,integer} integer-set  ([integer-list]) n Set semantics l Order is unimportant  (1,3,5) is equivalent to (5,1,3) and any other permutation l Duplicate elements are unimportant  (1,3,5,1,3,3,5) is equivalent to (1,3,5)

Proof: (5,-2,11 ) is an integer-set StatusReason integer-set Given ([integer-list]) Replace integer-set by its RHS (integer-list) Include option (integer{,integer}) Replace integer-list by its RHS (5{,integer}) Lemma: 5 is an integer (5,integer,integer) Choose two repetitions (5,-2,integer) Lemma: -2 is an integer (5,-2,11) Lemma: 11 is an integer

Description of Sets with Ranges n Ranges syntax A range is a single integer or a pair separated by.. integer-range  integer[..integer] integer-list  integer-range{,integer-range} integer-set  ([integer-list]) Range semantics X.. Y X  Y: all integers from X up to Y (inclusive)  1..5 is equivalent to 1,2,3,4,5 ; 5..5 is equivalent just to 5 X  Y: a null range; it contains no values  (1..4,10,5..4,11..13) is equivalent to (1,2,3,4,10,11,12,13 )

Recursive Descriptions A directly recursive EBNF rule has its LHS in its RHS r1  | Ar1 We read this as r1 is defined as the choice of nothing or an A followed by an r1. The symbols recognized as an r1 are of the form A n, n  0. Proof that AAA is an r1 r1 Given Ar1 Replace r1 by the second alternative in its RHS AAr1 Replace r1 by the second alternative in its RHS AAAr1 Replace r1 by the second alternative in its RHS AAA Replace r1 by the first (empty) alternative in its RHS This rule is equivalent to r1  {A}

The Power of Recursion To recognize symbols of the form form A n B n, n  0 we cannot write r1  {A}{B}, because nothing constrains us choosing different repetitions of A and B : AAB The recursive rule r1  | Ar1B works, because each choice of the second alternative uses exactly one A and one B. Proof that AAABBB is an r1 r1 Given Ar1B Replace r1 by the second alternative in its RHS AAr1BB Replace r1 by the second alternative in its RHS AAAr1BBB Replace r1 by the second alternative in its RHS AAABBB Replace r1 by the first (empty) alternative in its RHS Symbols of the form form A n B n, n  0

Problems Read the EBNF Handout (all but Section 2.7) n Study and Understand the Review Questions l 2 (page 10), 2&3 (page 12), 1 (page 16), 2 (page 18) n Be prepared to discuss in class solutions to the following Exercises (starting on page 23) l 1, 2, 4, and expecially 8 See next slide for more problems

Problems (continued) n Translate the following RHS of an EBNF rule into its equivalent syntax chart. Then, classify each of the examples below as legal or illegal according to this rule (or its equivalent chart). A{BA}Z  AZ BZ ABZ ABAZ  ABABZ ABA AAAZ ABABBZ A{B[C]}Z  BZ ABC ABBBZ ACCZ  ABCZ ABCBCZ ABBCBBZ ABCZBCZ A{B|C}Z  AB ABC ABBBZ BBZ  ABBCCZ ACCBBZ ACBBCZ ABCZBCZ