SourceForge.net Logo BNF Compiler for Java: "Hello, World"


"Hello, World" Example

Here is a BNF program that parses that familiar greeting, Hello, World

"Hello, World" Scenario

When a user (such as me) joins the Project, I might say "Hello". To address somebody in particular, I'd say your name, such as "Hello, Dan". To get everybody's attention, I can say "Hello, World".

"Hello, World" Use Case

        ___                         ______________________
       (. .)                       /                      \
        -+-                       /                        \
         |                       /    User types "Hello"    \
      +--+--+   --------------->(     to the Project's       )
         |                       \    sign-in text area,    /
        / \                       \   then hits "Enter".   /
       /   \                       \______________________/
      /     \
       Member
       Joins

"Hello, World" Principle Objects

    ( )      +----------------+        +-------------+       +------------+
    -+-      | "Hello World": |        |  parser:    |       | login:     |
     |       |  BNF Grammar   |        |  BNFParser  |       |  TextArea  |
    / \      +--------+-------+        +------+------+       +------+-----+
   /   \              |                       |                     |
   :Member            |                       |                     |
     |                |                       |                     |
     || type          |                       |                     |
     |+------------------------------------------------------------>|
     |   <<data>>     |                       |                     |
     |                |                       |                     |
     |                |                       |                     |
     |                |                       |                     |
     || login         |                       |                     |
     |+-------------->|| parse()              |                     |
     ||  <<event>>    |+--------------------->||  get()             |
     ||               ||                      |+------------------->||
     ||               ||                      ||                    ||
     ||               ||                      ||             string ||
     ||               ||             succeed  ||<-------------------+|
     ||      welcome  ||<---------------------+|                    |
     ||<--------------+|                      .                     .
     ..               ..               or     ||                    .
     ||         or    ||              fail    ||                    .
     ||      go away  ||<---------------------+|                    .
     ||<--------------+|                      |                     .
     |                |                       .                     .
     .                .                       .                     .
     .                .                       .                     .

"Hello, World" BNF Grammar

(*   First, declare the overall structure or appearance of a greeting. *)
(*   The first word, "Hello", is not optional -- it must be "Hello".   *)
(*   The rest of the greeting is defined by its structure,             *)
(*   and the structure is further defined by the terminals,            *)
(*   or tokens, that we scan in the source text.                       *)

greeting = hello, comma, name;

(* "Hello" is those exact 5 characters, all in a row *)
hello = 'H', 'e', 'l', 'l', 'o';

(*   What's in a name? We're never really sure,                        *)
(*   but we can start with a Capital Letter,                           *)
(*   followed by zero or more (any number) of small letters.           *)

name = capital letter, { small letter };

(*   A capital letter can be any single one of the following terminals,*)
(*   BTW, if the character in the text is not one of these (A..Z),     *)
(*        then the 'capital letter' definition fails!                  *)

capital letter = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I'
               | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R'
               | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z';

(*   And small letters must be defined, too:                          *)
(*   BTW, if the character in the text is not one of these (a..z),    *)
(*        then the 'small letter' definition fails!                   *)

small letter   = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i'
               | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r'
               | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z';

(*   Finally, we'll declare that a comma is actually a comma,          *)
(*   and it may be followed by an optional space,                      *)

comma = ",", [ " " ];

(* Note: The word  comma  is a meta-identifier.                         *)
(*       It names a definition, and is a goal in the grammar.           *)
(*       The string  ","  is a terminal.                                *)
(*       It identifies the actual character expected in the input text. *)

That's a BNF grammar! The parser is a top-down, left-to-right scanner. It starts with a goal, which is defined by a series of definitions. Each definition can be a terminal, or it can be more definitions that must be satisfied in turn. As the parser scans the input text, each definition must succeed and define the structure of the text. Otherwise the definition must fail, and the program must try a different alternative, or fail also. When the parser has completely analyzed the source text, the text has been matched to the grammar.

This example can only handle a single two-word text area, but BNF can be used to define a complete programming language.


Valid XHTML 1.1!