Syntax Specification and BNF © Allan C. Milne Abertay University v12.7.11.

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

ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
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.
Session 14 (DM62) / 15 (DM63) Recursive Descendent Parsing.
ISBN Chapter 3 Describing Syntax and Semantics.
Concepts of Programming Languages 1 Describing Syntax and Semantics Brahim Hnich Högskola I Gävle
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
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.
PSUCS322 HM 1 Languages and Compiler Design II Formal Semantics Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Programming Languages An Introduction to Grammars Oct 18th 2002.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
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.
1 Chapter 3 Context-Free Grammars and Parsing. 2 Parsing: Syntax Analysis decides which part of the incoming token stream should be grouped together.
ISBN Chapter 3 Describing Syntax and Semantics.
(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.
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.
CSI 3120, Grammars, page 1 Language description methods Major topics in this part of the course: –Syntax and semantics –Grammars –Axiomatic semantics (next.
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.
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.
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.
Chapter Describing Syntax and Semantics. Chapter 3 Topics 1-2 Introduction The General Problem of Describing Syntax Formal Methods of Describing.
Chapter 3 Part I Describing Syntax and Semantics.
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.
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.
Syntax Specification with YACC © Allan C. Milne Abertay University v
D Goforth COSC Translating High Level Languages.
Chapter 3 Describing Syntax and Semantics
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
ISBN Chapter 3 Describing Syntax and Semantics.
Syntax and Grammars.
Chapter 3 © 2002 by Addison Wesley Longman, Inc Introduction - Who must use language definitions? 1. Other language designers 2. Implementors 3.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
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.
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.
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.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
7.2 Programming Languages - An Introduction to Informatics WMN Lab. Hye-Jin Lee.
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:
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Chapter 3 Context-Free Grammar and Parsing
Chapter 3 – Describing Syntax
Syntax versus Semantics
Syntax vs Semantics Backus-Naur Form Extended BNF Derivations
Chapter 3 Describing Syntax and Semantics.
High-Level Programming Language
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

Syntax Specification and BNF © Allan C. Milne Abertay University v

Agenda. Language Sspecification. BNF. Derivation sequence. Examples.

Syntax and Semantics. A language must be defined in terms of both its syntax and semantics. Syntax defines the format of the programs, statements and structures. –What a program looks like. Semantics defines the meaning of language constructs. –What a program does.

Language Specifications. Syntax can be formally defined. –e.g. grammars, BNF, syntax diagrams. While semantics can also be formally defined these definitions are usually long-winded and inaccessible to programmers. –e.g. attribute grammars, denotational semantics.

Specifying Syntax. There are a number of approaches to defining the syntax of a language: –Informal (eg Basic manuals). An assignment statement has the name of an identifier followed by an equals sign and an arbitrary expression. –Syntax diagrams (eg Pascal). –BNF (eg Algol-60). ::= "=" id expression =

Backus Normal Form (BNF). Backus Naur Form or Backus Normal Form. First used for the specification of Algol-60. This is a formal specification method. Underlying formalism is grammar theory. –A grammar is a generative device that specifies how to generate valid sentences in the language. BNF is a metalanguage.

Naming of Parts. ::= "=" ; metasymbols a rule non-terminal symbols terminal symbol a production

The Parts. Metasymbols –part of the BNF language Not the language being specified – ::= " ; | ( ) ? + * Terminal Symbols –atomic components of the language being specified –may use "…" to remove ambiguity Non-Terminal Symbols –define syntactic components used in the specification –are part of the specification not the language –name surrounded by –are derived into smaller units

Productions. A rule in BNF can have 1 or more alternative productions: ::= true | false ; This rule is made up of the following two productions: ::= true ; ::= false ;

The Null Production. It is useful to have a notation for the null, or empty, production; –that is a production that derives to nothing. We use the notation <> to indicate the null production. Note this must be an entire production –it cannot be used with other terminal or non- terminal symbols in a production. Example ::= if "(" ")" ; ::= else | <> ;

Applying Productions. Productions of a rule are applied through substitution: … …  …  where the rule for has a production ::=  This is a derivation of. Example: assume a production ::= hello ; and a string a b c d apply the production to get a b c d  a b hello c d

Derivation Sequence. A derivation sequence is the sequence of single derivation steps starting from the distinguished symbol of the BNF and terminating in a string of terminal symbols. The distinguished or starter symbol is defined to be the non- terminal of the first rule of the BNF specification. The final string of terminals is our program, known formally as a sentence.

The Language. The language defined by a BNF specification is the set of all sentences that can be derived from the distinguished symbol of the specification. Putting it another way, a sentence (or program) of a language is syntactically valid if and only if a derivation sequence can be found.

An Example BNF Specification. A specification of the syntax of a parenthesised comma-separated list of animals: ::= "(" ")" | "(" ")" | "(", ")" ; ::= |, ; ::= ant | bat | cat | dog ;

Example Derivation Sequence. Here is one possible derivation sequence for the sentence “( dog,ant)” :  (, )  (, ant )  ( dog, ant )

Parsing. Parsing a program is finding a derivation sequence for the program. There are two general approaches to this: –Top-down; start from the distinguished symbol and find substitutions that result in the target program. –Bottom-up; start with the input programand try to reduce this back to the distinguished symbol. This latter is the approach used by Yacc.

Extended BNF. The standard BNF notation has been extended to include iterative constructs; –this is known as Extended BNF or EBNF notation; –it is a superset of standard BNF. For those interested refer to my version of this notation which includes a full specification of the EBNF notation in itself and supporting tools. Find this at –