Download Introduction to Grammars & BNF: Generating Strings with Production Rules and more Study notes Computer Science in PDF only on Docsity!
More Productions
Remember that a grammar can use production rules to generate strings that are valid in the language. So far, we have examined simple productions of the type X Y. We now examine some more productions. NOTE: In this lecture all terminal symbols are lower case and all non–terminal symbols are UPPER CASE. The start symbol is denoted by S. Here is an example that will be explored later when we discuss BNF. S D | N We can have either a number or a digit N D | N D A number is either a digit or a number followed by a digit D 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 The decimal digits The language is the set of all non–negative integers, written in decimal form.
Example: Producing the Integer 01234
Notation: X Y is a production rule in a grammar X Y is used to say either “Y is derived from X” or “X derives Y” according to a production rule. Here the derivation of 1234 is as follows S N N4 N34 N234 N1234 01234 This is often written as The “* notation” implies that the string “01234” is validly derived from the start symbol by application of zero or more production rules.
The Grammar for This Language
Again, the language is L = { n 1 n | n 0}. The grammar for this language is G = (V, T, S, P) with V = {S} // The start symbol is the only variable T = {0, 1} // The terminals are 0 and 1.
P: S | 0S1 // Only one production rule.
Examples of this grammar.
Derive 01 S 0S1 0 1 = 01
Derive 000111 S 0S1 00S11 000S111 000 111 = 000111
Note that the production rule S gives the empty string, while
while the production rule S 0S1 puts a 0 on the front and a 1 on the back.
Recognizing L = {
n
n
| n 0}
Later, we shall prove that L cannot be recognized by any FSM (Finite State Machine) that we have defined at this point. The language is recognized by a stack machine. Here is a pseudocode description of that machine. If you don’t know stacks, ignore this slide. Clear the stack Read a symbol from the input. Call it x. While ( x = 0) AND (NOT End–Of–File) Do Push x onto the stack. Read x End While While ( x = 1) AND (NOT End–Of–File) DO Pop y from stack. Discard it. Read x End While If the stack is empty, accept the string. Otherwise, reject it.
Another Example of a Language
Consider the language L = { m 1 n | m 0, n 0} This is just a bunch of 0’s followed by a bunch of 1’s. The only rule in this language seems to be that a 0 cannot follow a 1. Here is one grammar that generates the language. There is at least one more. G = (V, T, S, P) with V = {S} T = {0, 1}
P: S | 0S | S
Note: The production either puts a 0 on the start of the string or a 1 on the end.
Finite State Machine to Recognize L
This FSM recognizes L = { m 1 n | m 0, n 0}. The leftmost state is an accept state for two reasons.
- The empty string is accepted.
- A string of all 0’s is accepted. The rightmost state is a reject state because no 0 is allowed to follow a 1. The first 0 following a 1 causes the string to be rejected independent of any further input.
BNF for Real Numbers (No Exponents)
We now consider a rather complete BNF description of real numbers. There are two unusual conditions on this grammar.
- Exponential forms are ignored in this grammar.
- If a decimal point is used, it must be followed by at least one digit. Begin by examining the first production. ::= | + | – This is read as follows. A real number is either
- An unsigned real, or
- A “+” followed by an unsigned real, or
- A “–” followed by an unsigned real. Note that the “+” and “–” are terminal symbols; each represents itself and must appear “as is” in the string being generated.
BNF for Real Numbers (No Exponents) Part 2
Let’s start over. Here is the complete grammar in BNF. ::= | + | – ::= | < unsigned integer>. < unsigned integer> ::= | ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 NOTE: The second production is written on two lines only because it is too long for one line. The second part of the second production calls for an unsigned integer followed by a decimal point followed by an unsigned integer. Real numbers with leading and trailing decimals cannot be generated by this grammar. “23.” and “.50” are each invalid. These two invalid examples would be generated by a grammar that completely describes the real numbers.
Interpretation of BNF for C++ (Part 1)
::= | , This states that an expression is defined to be either an expression or the sequence of an expression followed by a comma followed by an assignment expression. Note that the comma in the above BNF production is a terminal symbol in the grammar and is to be read literally. This allows expressions such as the following x = 0 , y = 1 ::= | Here we state that an assignment expression is either a conditional expression or a unary expression followed by an assignment operator followed by an assignment expression.
Interpretation of BNF for C++ (Part 2)
* ::= = | = | /= | %= | += | –= | >>= | <<= | &= | ^= | |= Here we list the valid assignment expressions. There are 11 of them. Some, such as “–=” and “|=” are hard to display in the font used in this document. ::= | ? : Here we begin to define the conditional expressions. It may seem strange that the non-terminal is the only specific conditional expression listed, but that is the document’s way of specifying the hierarchy of logical operations. Note again that the second part of this production has the non terminals “?” and “:”, used as a part of the conditional expression operator “?:” that seems to be unique to C and C++.