Grammar and torture
Article Index
Grammar and torture
BNF rules
Syntax and semantics

Computational grammar is a subject that is sometimes viewed as a form of torture by computer science students, but understanding something about it really does help ....

Computational Grammar

In other looks into Babbage’s Bag we have been concerned with the development of computer languages. We have seen how assembly languages evolved into high level languages and the problems caused by the apparently simple “arithmetic expression”.

What we haven’t looked at is the impact this practical problem had on theoretical computer science.

 

Banner

 

To deal with the whole problem of describing and compiling artificial languages they had to invent a new subject – computational grammar.

This is a subject that has over the years been used as a form of torture for thousands of computer science students but it deserves better!

BNF

In the early days of high level languages the only really difficult part was the translation of arithmetic expressions to machine code and much of the theory of grammar was invented to deal with just this problem. 

At the core of this theory, and much used in the definition of programming languages, is BNF, or Backus Normal Form. The only problem is that there isn’t one standard notation for BNF but it is always easy enough to understand. For example, using arrow notation you might write:

<additive expression> ->
 <variable> + <variable>

You can read this as saying that an additive expression is formed by taking a variable, a plus sign and another variable. This rule only fits expressions like A+B, and not A+B+C, but you get the general idea.

Quantities in angle brackets like

<additive expression> 

are called “non-terminal” symbols because they are defined by a BNF rule and don’t appear in the final language.

The plus sign on the other hand is a “terminal” symbol because it isn’t further defined by a BNF rule.

You might notice that there is a slight cheat going on in that the non-terminal <variable> was replaced by a terminal in the examples but without a rule allowing this to happen. Well in proper BNF you have to have a rule that defines every non-terminal in terms of terminal symbols but in the real world this becomes so tedious that we often don’t bother and rely on common sense.

In this case the missing rule is something like:

<variable> -> A|B|C|D etc .. |Z

where the vertical bar is read as “OR”. If you wanted to define a variable that was allowed to have more than a one-letter name you might use:

1 <variable> -> 
<variable> <letter>| <letter>
2  <letter> -> A|B|C|D etc .. |Z

This is the BNF equivalent of a repeating operation. For example, to use this definition you start with:

<variable>

and use rule one to replace it by:

<variable><letter>

then use rule two to replace <letter> by A to give:

<variable>A

Next we use the first rule to replace <variable> to get:

<variable><letter>A

and so on building up the variable name one character at a time.

Boring isn’t it, but it is an ideal way to tell a computer what the rules of a language are.

Q) Just to check that you are following – why does rule one include the alternative “|<letter>”?

A) The reason is that this is the version of the rule we use to stop a variable name growing forever because once it is selected the next step it to pick a letter and then we have no non-terminals left in the result.

Notice also that the BNF definition of a variable name cannot restrict its length. Rules like “fewer than 8 characters” have to be imposed as notes that are supplied outside of the BNF grammar.

Extended BNF

To make repeats easier you will often find that an extended BNF notation is used where curly brackets are used to indicate that the item can occur as many times as you like – including zero. In this notation:

variable -> <letter> {<letter>}

means “a variable is a letter followed by any number of letters”. Another extension is the use of square brackets to indicate an optional item. For example:

<integer constant> -> 
[+|-] <digit> {<digit>}

means that you can have a plus or minus sign  or no sign at all in front of a sequence of at least one digit.

But the notation isn't universal by any means.

<ASIN: 0763741493>

<ASIN:0321712943>

 



 
 
Banner

   
RSS feed of all content
I Programmer - full contents
Copyright © 2012 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.