Docsity

BNF and EBNF: The Evolution of Formal Grammar Notations, Lecture notes of Programming Languages

Partial preview of the text

Download BNF and EBNF: The Evolution of Formal Grammar Notations and more Lecture notes Programming Languages in PDF only on Docsity!

BNF and EBNF

What is BNF?

  • It stands for Backus-Naur Form• It is a formal, mathematical way to specify

context-free grammars

  • It is precise and unambiguous• Before BNF, people specified

programming languages ambiguously, i.e.,with English

Who was John Backus?

-^

Backus invented FORTRAN (“FORMulaTRANslator”), the first high-level language ever

, circa 1954

-^

Major influence on the invention offunctional programming in 1970’s

-^

Won the 1977 Turing Award for BNF andFORTRAN

Who was Peter Naur?

  • Danish astronomer turned computer

scientist

  • Born in 1928; picture on left is from 1968

What does BNF look like?

  • Like this: ::= | ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 • “::=” means “is defined as” (some variants use

“:=” instead)

  • “|” means “or”• Angle brackets mean a nonterminal• Symbols without angle brackets are terminals

More BNF Examples

  • ::= while ( )

  • ::= =

  • ::= |

  • ::= |

Origin of EBNF

  • Stands for “Extended Backus-Naur Form”• After BNF appeared with Algol 60, lots of

people added their own extensions

  • Niklaus Wirth wanted to see one form, so

he published “What Can We Do About theUnnecessary Diversity of Notation forSyntactic Definitions” in Communicationsof the ACM in 1977

  • He suggested the use of “[ .. ]” for optional

symbols (0 or 1 occurrences), “{ .. }” for 0or more occurrences.

  • Did not mention “EBNF” or Kleene cross

Who was Niklaus Wirth?

-^

Born in Switzerland, 1934

-^

Desgined Pascal (1970),Modula, Modula-2 (1980),and Oberon (1988)

-^

Won the Turing Award in 1984

-^

Won the IEEE ComputerPioneer Award in 1987

What are the Extensions?

  • They vary, but often are derived from

regular expression syntax

  • “

” (The Kleene Star): means 0 or more occurrences

  • “

” (The Kleene Cross): means 1 or more occurrences

  • “

”: means 0 or 1 occurrences (sometimes “[ … ]” used instead)

  • Use of parentheses for grouping

BNF vs EBNF

  • Grammar for decimal numbers in plain

BNF:

::= '-' | ::=

| '.'

::=

|

::= '0' | '1' | '2' | '3' |

'4' | '5' | '6' | '7' | '8' | '9'

Simple Conversions

  • If you have a rule such as: ::= ::=

|

-^

You can replace it with: ::= +

More Simple Conversions

  • If you have a rule such as: ::= | empty •^

You can replace it with: ::= *

EBNF for Lisp

s_expression ::= atomic_symbol

| "(" s_expression "." s_expression ")"| list

list ::= "(" s_expression ")"atomic_symbol ::= letter atom_partatom_part ::= empty*

| letter atom_part| number atom_part

letter ::= "a" | "b" | " ..." | "z"number ::= "1" | "2" | " ..." | "9"

Summary-BNF

-^

BNF uses following notations:

(i) Non-terminals enclosed in

<

and

>

.

(ii) Rules written as

X

::=

Y

(1)

X

is LHS of rule and can only be a NT.

(2)

Y

is RHS of rule:

Y

can be

(a) a terminal, nonterminal, or concatenation of terminal

and nonterminals, or (b) a set of strings separated by alternation symbol

|.

Example: < S >

::=

a < S > |a

-^ Notation

ε

: : Used to represent an empty string (a string of

length 0).