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.

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

BNF. What is BNF? BNF stands for “Backus-Naur Form,” after the people who invented it BNF is a metalanguage--a language used to describe another language.
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 2 Syntax. Syntax The syntax of a programming language specifies the structure of the language The lexical structure specifies how words can be.
ISBN Chapter 3 Describing Syntax and Semantics.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
Honors Compilers An Introduction to Grammars Feb 12th 2002.
16-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
17-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Programming Languages An Introduction to Grammars Oct 18th 2002.
28-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
1 Introduction: syntax and semantics Syntax: a formal description of the structure of programs in a given language. Semantics: a formal description of.
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.
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.
Programming Languages
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
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.
Syntax Specification and BNF © Allan C. Milne Abertay University v
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.
Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.
CS 331, Principles of Programming Languages Chapter 2.
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.
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.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
CSE 425: Syntax II Context Free Grammars and BNF In context free grammars (CFGs), structures are independent of the other structures surrounding them Backus-Naur.
CPS 506 Comparative Programming Languages Syntax Specification.
22-Nov-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
Chapter 3 Describing Syntax and Semantics
ISBN Chapter 3 Describing Syntax and Semantics.
CS 331, Principles of Programming Languages Chapter 2.
Syntax and Grammars.
The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
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.
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.
Parser: CFG, BNF Backus-Naur Form is notational variant of Context Free Grammar. Invented to specify syntax of ALGOL in late 1950’s Uses ::= to indicate.
©SoftMoore ConsultingSlide 1 Context-Free Grammars.
1 Context Free Grammars Xiaoyin Wang CS 5363 Spring 2016.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
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:
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
Context-Free Grammars
R.Rajkumar Asst.Professor CSE
Context-Free Grammars 1
Recognizers 1-Jan-19.
Recognizers 16-Jan-19.
Recognizers 22-Feb-19.
BNF 23-Feb-19.
BNF 9-Apr-19.
High-Level Programming Language
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

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 (e.g. describing English grammar in English) We need a formal, precise means of describing the syntax of programming languages For decades, BNF has met that need One of the irritating things about Java is that it has no official BNF definition—hence, it’s sometimes hard to tell what is legal syntax It is essential to distinguish between the metalanguage terms and the object language terms For example, BNF uses the | symbol, as do many programming languages—so if we see a |, what is it?

BNF BNF stands for either Backus-Naur Form or Backus Normal Form BNF is a metalanguage that is frequently used to describe the grammar of a programming language BNF is formal and precise BNF is essential in compiler construction If you know about “context-free grammars (CFGs),” BNF is one way of defining CFGs There are many dialects of BNF in use, but… …the differences are almost always minor

BNF metasymbols Anything enclosed in is a nonterminal that needs to be further expanded, e.g. That is, if you see in the description of a programming language, that means “a variable goes here” Symbols not enclosed in are terminals; they represent themselves, e.g. if, while, ( That is, if you see while in the description of a programming language, that means “the actual word ‘ while ’ goes here” The symbol ::= means is defined as The symbol | means or; it separates alternatives, for example, ::= + | - That is, if you see an, it means “either a ‘ + ’ or a ‘ - ’ goes here”

BNF uses recursion ::= | or ::= | Many people find recursion confusing “Extended BNF” (which we’ll talk about shortly) allows repetition as well as recursion Repetition is often easier to implement (with a loop) than recursion, so Extended BNF has become popular

BNF Examples I ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 This defines the metasymbol ‘ ’ What it means is, “If the description of the syntax says ‘ ’, you need an actual digit in this location” ::= if ( ) | if ( ) else The symbols if, (, ), and else are terminals—they stand for themselves,, and are metasymbols If you see an, you should replace it by either if ( ) or with if ( ) else Next, you need to replace and with their definitions

BNF Examples II ::= | ::= | + | -

BNF Examples III ::= | | ::= { } ::= |

BNF Examples IV ::= | | | | | | | |...

Extended BNF Dialects differ, but the following are pretty standard: [ ] enclose an optional part of the rule Example: ::= if ( ) [ else ] { } mean the enclosed can be repeated any number of times (including zero) Example: ::= ( ) | ( {, } )

Variations The preceding notation is the original and most common notation BNF was designed before we had boldface, color, more than one font, etc. A typical modern variation might: Use boldface to indicate multi-character terminals Quote single-character terminals (because boldface isn’t so obvious in this case) Example: if_statement ::= if "(" condition ")" statement [ else statement ]

Limitations of BNF No easy way to impose length limitations, such as maximum length of variable names No way to impose distributed requirements, such as, a variable must be declared before it is used Describes only syntax, not semantics Nothing clearly better has been devised

The End