Backus–Naur Form

Backus-Naur Form: BNF

In computer science, BNF (Backus Normal Form or Backus–Naur Form) is a notation technique for context-free grammars, often used to describe the syntax of languages used in computing, such as computer programming languages, document formats, instruction sets and communication protocols. It is applied wherever exact descriptions of languages are needed, for instance, in official language specifications, in manuals, and in textbooks on programming language theory.

Good reference: http://cuiwww.unige.ch/db-research/Enseignement/analyseinfo/BNFweb.html

Augmented BNF for Syntax Specifications: ABNF

( A bit of history Original STANDARD FOR THE FORMAT OF ARPA INTERNET TEXT MESSAGES)

The definition of ABNF are http://www.ietf.org/rfc/rfc2234.txt and http://tools.ietf.org/html/rfc4234

Example: BASIC 64

! -----------------------------------------------------------------------
! BASIC '64
!
! Beginner's All-purpose Symbolic Instruction Code
!
!    "It is practically impossible to teach good programming style to students
!     that have had prior exposure to BASIC; as potential programmers they are
!     mentally mutilated beyond hope of regeneration."
!
!     - Edsger W. Dijkstra
!
! BASIC is one of the oldest programming language and one of the most popular.
! It was developed in 1964 by John G. Kemeny and Thomas E. Kurtz to teach
! students the basics of programming concepts. At the advent of the microcomputer,
! BASIC was implemented on numerous platforms such as the Commodore, Atari,
! Apple II, Altair, IBM PC computers. Over time, BASIC evolved into GW-BASIC,
! QBasic, Visual Basic, and recently Visual Basic .NET.
!
! In practically all programming languages, the reserved word/symbol that denotes
! a comment is treated as a form of whitespace - having no effect in the manner in
! which the program runs. Once such type of comment is used to indicate the remainder
! of the line is to be ignored. These can be added to the end of any line without
! changing the meaning of the program. In C++, it is the '//' symbol;
! in BASIC '64 it is 'REM'.
!
! However, in the BASIC programming language, the line comment is treated like a
! statement. For instance, if 'REM' was a normal line comment:
!
!    10  PRINT "Hello World" REM Common first program
!
! would be a valid statement. However, in BASIC, this is illegal. In the example
! above, the comment must be added as an additional statement.
!
!    10  PRINT "Hello World" : REM Common first program
!
! As a result, the Line Comment terminal that is used in the GOLD Parser cannot be
! used here. In the grammar below, a 'Remark' terminal was created that accepts all
! series of printable characters starting with the characters "REM ". In some
! implementations of BASIC, any string starting with "REM" is a comment statement.
! Examples include "REMARK", "REMARKABLE" and "REMODEL". This grammar requires the space.
!
! This grammar does not include the editor statements such as NEW, SAVE, LOAD, etc...
!
! Note: This is an ad hoc version of the language. If there are any flaws, please
! e-mail GOLDParser@DevinCook.com and I will update the grammar. Most likely I have
! included features not available in BASIC '64.
! -----------------------------------------------------------------------

"Name"    = 'BASIC (Beginners All-purpose Symbolic Instruction Code)'
"Author"  = 'John G. Kemeny and Thomas E. Kurtz'
"Version" = '1964 - Original - before Microsoft enhanced the language for the IBM PC.'
"About"   = 'BASIC is one of most common and popular teaching languages ever created. '

"Case Sensitive" = False
"Start Symbol"   = <Lines>

{String Chars} = {Printable} - ["]
{WS}           = {Whitespace} - {CR} - {LF}

NewLine        = {CR}{LF}|{CR}
Whitespace     = {WS}+

Remark         = REM{Space}{Printable}*
ID             = {Letter}[$%]?
String         = '"'{String Chars}*'"'
Integer        = {digit}+
Real           = {digit}+.{digit}+ 

<Lines>       ::= Integer <Statements> NewLine <Lines>
                | Integer <Statements> NewLine

<Statements>  ::= <Statement> ':' <Statements>
                | <Statement>

<Statement>   ::= CLOSE '#' Integer
                | DATA <Constant List>
                | DIM ID '(' <Integer List> ')'
                | END
                | FOR ID '=' <Expression> TO <Expression>
                | FOR ID '=' <Expression> TO <Expression> STEP Integer
                | GOTO <Expression>
                | GOSUB <Expression>
                | IF <Expression> THEN <Statement>
                | INPUT <ID List>
                | INPUT '#' Integer ',' <ID List>
                | LET Id '=' <Expression>
                | NEXT <ID List>
                | OPEN <Value> FOR <Access> AS '#' Integer
                | POKE <Value List>
                | PRINT <Print list>
                | PRINT '#' Integer ',' <Print List>
                | READ <ID List>
                | RETURN
                | RESTORE
                | RUN
                | STOP
                | SYS <Value>
                | WAIT <Value List>
                | Remark

<Access>   ::= INPUT
             | OUPUT

<ID List>  ::= ID ',' <ID List>
             | ID 

<Value List>      ::= <Value> ',' <Value List>
                    | <Value> 

<Constant List>   ::= <Constant> ',' <Constant List>
                    | <Constant> 

<Integer List>    ::= Integer ',' <Integer List>
                    | Integer

<Expression List> ::= <Expression> ',' <Expression List>
                    | <Expression> 

<Print List>      ::= <Expression> ';' <Print List>
                    | <Expression>
                    |  

<Expression>  ::= <And Exp> OR <Expression>
                | <And Exp> 

<And Exp>     ::= <Not Exp> AND <And Exp>
                | <Not Exp> 

<Not Exp>     ::= NOT <Compare Exp>
                | <Compare Exp> 

<Compare Exp> ::= <Add Exp> '='  <Compare Exp>
                | <Add Exp> '<>' <Compare Exp>
                | <Add Exp> '><' <Compare Exp>
                | <Add Exp> '>'  <Compare Exp>
                | <Add Exp> '>=' <Compare Exp>
                | <Add Exp> '<'  <Compare Exp>
                | <Add Exp> '<=' <Compare Exp>
                | <Add Exp> 

<Add Exp>     ::= <Mult Exp> '+' <Add Exp>
                | <Mult Exp> '-' <Add Exp>
                | <Mult Exp> 

<Mult Exp>    ::= <Negate Exp> '*' <Mult Exp>
                | <Negate Exp> '/' <Mult Exp>
                | <Negate Exp> 

<Negate Exp>  ::= '-' <Power Exp>
                | <Power Exp> 

<Power Exp>   ::= <Power Exp> '^' <Value>
                | <Value> 

<Value>       ::= '(' <Expression> ')'
                | ID
                | ID '(' <Expression List> ')'
                | <Constant> 

<Constant> ::= Integer
             | String
             | Real

Other Examples

Java, C, ISO 7185 STANDARD PASCAL, SQL (various flavours)

About henry416
I am a computer technology explorer and an university student based on Toronto. If you have any question, please feel free to discuss and comment here

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s