Example: String Terminal

 

The following example declares the string terminal commonly used in programming languages. It is declared as a series of zero or more printable characters (not including the double-quotes used for delimiters).

{String Char} = {Printable} - ["]

String = '"'{String Char}*'"'

<Literal> ::= Identifier
            | String

However, this definition does not allow the programmer to specify the double-quote character. Two distinct approaches are used in modern programming languages. The first approach is to use another structure or constant to represent the double-quote. The second approach is the use of an "override" character that allows the double-quote to be placed directly in the string. The latter approach is used in the C/C++ programming language family.

The following contains a much more complex definition for strings, but implements the second approach mentioned above. The backslash character '\' now acts as an override and can be used to represent the double-quote character. Essentially, the string terminal is now a series of any printable character (not including the double-quote and backslash) and any printable character preceded by the backslash.

{String Char} = {Printable} - ["\]

String = '"' ({String Char} | '\'{Printable})* '"'

<Literal> ::= Identifier
            | String