BNF A CFL Metalanguage Some Variations Particular View to SLK Copyright © 2015 – Curt Hill.

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.
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.
A basis for computer theory and A means of specifying languages
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.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
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.
ISBN Chapter 3 Describing Syntax and Semantics.
UMBC Introduction to Compilers CMSC 431 Shon Vick 01/28/02.
(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.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
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.
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.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
Course: ICS313 Fundamentals of Programming Languages. Instructor: Abdul Wahid Wali Lecturer, College of Computer Science and Engineering.
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.
Copyright © Curt Hill Languages and Grammars This is not English Class. But there is a resemblance.
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 3 Describing Syntax and Semantics. Chapter 3: Describing Syntax and Semantics - Introduction - The General Problem of Describing Syntax - Formal.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
Chapter 3 Describing Syntax and Semantics
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.
Syntax and Grammars.
Overview of Previous Lesson(s) Over View  In our compiler model, the parser obtains a string of tokens from the lexical analyzer & verifies that the.
Parser Generation Using SLK and Flex++ Copyright © 2015 Curt Hill.
Syntax and Semantics Form and Meaning of Programming Languages Copyright © by Curt Hill.
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.
Organization of Programming Languages Meeting 3 January 15, 2016.
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
Describing Syntax and Semantics
A Simple Syntax-Directed Translator
CS510 Compiler Lecture 4.
Chapter 3 – Describing Syntax
Concepts of Programming Languages
Syntax (1).
What does it mean? Notes from Robert Sebesta Programming Languages
Automata and Languages What do these have in common?
Syntax versus Semantics
CS 363 Comparative Programming Languages
Programming Language Syntax 2
R.Rajkumar Asst.Professor CSE
CS 3304 Comparative Languages
BNF 23-Feb-19.
Chapter 3 Describing Syntax and Semantics.
BNF 9-Apr-19.
Programming Languages 2nd edition Tucker and Noonan
COMPILER CONSTRUCTION
Presentation transcript:

BNF A CFL Metalanguage Some Variations Particular View to SLK Copyright © 2015 – Curt Hill

Metalanguage A language that describes languages English is both a language and a metalanguage –It is able to describe itself BNF is a metalanguage for describing Context Free Languages –Most programming languages are Context Free

Origins Name is acronym for Backus Normal Form Developed in late 1950s to describe FORTRAN Used to describe ALGOL 60 by Peter Naur –BNF is also Backus Naur Form Many variations, but we have nothing that is substantially better Copyright © 2015 – Curt Hill

Purpose BNF describe productions Recall that a production is a part of a Chomsky grammar A production is a rewrite rule The left hand symbol may be rewritten as the items on the right The left hand item must be a non- terminal –A construction from the language The right hand side is zero or more terminals and non-terminals Copyright © 2015 – Curt Hill

Form Productions in BNF take the form: NT ::= S 1 S 2 … S n –Where: NT is a non-terminal S k is either a non-terminal or terminal symbol A terminal is lexeme that will be seen –Such as punctuation, reserved words, identifiers A non-terminal is a construction –Such as for-loop, expression, function Copyright © 2015 – Curt Hill

Non-terminals Some systems prefer to see a non- terminal marked in some way –Such as enclosed in angle brackets Others do not – they think the only difference between a terminal and non-terminal is that the terminal never appears on the left hand side of the production Copyright © 2015 – Curt Hill

Alternation Many non-terminals may be rewritten in multiple ways This may be handled in a variety of ways –Duplicated productions –Alternation symbol – often | Lets consider an example Copyright © 2015 – Curt Hill

C Statement types Duplicated ::= ::= ::= ::= Alternation symbol ::= | | | Both of these use angle brackets to highlight non-terminals Copyright © 2015 – Curt Hill

Repetition BNF has historically used recursion to express repetition –Particularly odd in that recursion was unavailable in FORTRAN and difficult in assembly A non-terminal is then defined in terms of itself Consider lists Copyright © 2015 – Curt Hill

Lists in C Compound statement: ::= | Function parameters: ::= identifier ( ) ::= |, | _empty_ Copyright © 2015 – Curt Hill

Empty It is possible for a production to be empty –Replaced by nothing This is the usual way to specify an optional item It is also the way to end recursion Sometimes it has its own symbol like _empty_ Other times it is just empty on RHS or after a | Consider C types Copyright © 2015 – Curt Hill

C Types ::= ::= short | long | signed | unsigned | _empty_ ::= bool | char | int | float Copyright © 2015 – Curt Hill

Statements Most modern languages have a recursive statement construction ::= | | | … ::= if ( ) ::= { } ::= | | _empty_ What statements are not recursive? Copyright © 2015 – Curt Hill

EBNF Extended BNF Add something that makes the specification easier Things enclosed in [ and ] are optional, they may be present zero or one times Some times things enclosed in { and } may be present 0 or more times Each flavor of EBNF may choose a slightly different notation Copyright © 2015 – Curt Hill

Syntax Graph More graphic and easier to read than regular BNF Same expressive power May be programmatically generated Cannot use these to drive parser generators Copyright © 2015 – Curt Hill term + -

What Can BNF Not Do? Cannot consider the source in any other place Thus it cannot check: –Variables are defined –Expressions are of the correct type In a compiler this is usually done with extra checking –Referencing the symbol table BNF knows nothing about meaning –That is semantics Copyright © 2015 – Curt Hill

Derivations We derive strings by starting with the start symbol, and repeatedly replacing the left hand side by the right side of one of the productions If we can derive a string it is a legal part of the language Consider the following example Copyright © 2015 – Curt Hill

Example 1 Copyright © 2015 – Curt Hill Productions: ::= begin end ::= | ; ::= ident = ::= + | - | ::= ident | number

Example 2 Is the following a sentence in the above language? begin x = 3; y = x end If it is we should be able to find a derivation Copyright © 2015 – Curt Hill

Derivation ::= begin end ::= ; ::= ident = ::= ::= number ::= ident = ::= ::= ident Copyright © 2015 – Curt Hill

Parse Trees A parse tree is a derivation Each interior node represents a production The leaves are the actual input string Copyright © 2015 – Curt Hill

Derivation Copyright © 2015 – Curt Hill pgm beginend S_list stmt S_list ; Ident(x) = var Number(3) stmt Ident(y) = var Ident(x)

Exercise Generate the BNF for a C family switch case Assume is already defined Assume that the switch expression is only an identifier Assume that the case selector is a number Every case has a break There must be one or more case The last case must be a default Copyright © 2015 – Curt Hill

SLK This parser generator takes its own variation of BNF The LHS is on first line May be followed by several symbols –::= –-> –Among others Each subsequent line is an alternative No identification of non-terminals Blank line terminates Copyright © 2015 – Curt Hill

Example This production given earlier ::= | ; Would be in SLK s_list ::= stmt stmt ; s_list Copyright © 2015 – Curt Hill

Repetition SLK does accept a few EBNF constructs Braces enclose something repeated zero or more times –Thus braces, such as C compound statements must be escaped –Following the braces with + makes it one or more times Brackets enclose something optional –Zero or one times Copyright © 2015 – Curt Hill

Other Things SLK accepts both C++ comment –// and /* */ The reserved symbol _epsilon_ is the empty symbol Copyright © 2015 – Curt Hill

Conclusion BNF and its variations are the standard way to define a Context Free Language Using BNF and a parser generator like SLK and a lexical analyzer generator like Flex++ is the fastest way to construct a working recognizer or language processor Copyright © 2015 – Curt Hill