问题
I looked at the ISO spec pdf for EBNF here (I couldn't find the official one except on the ISO website, where it appears to cost money), and I don't quite understand the whitespace.
Does anyone know whether the official standards allow spaces in meta identifiers? Based off the meta identifier in the standards document (section 8.1), I would say no, but if you take a look at that same example in that same standards document, it appears to be saying yes.
In the comment at the top of 8.1 in that document, it appears to be saying that it is defining EBNF using itself. What I'm confused about this is lines such as this:
meta identifier = letter, {letter | decimal digit}
(* A <meta identifier> is the name of a
syntactic element of the language being
defined *);
meta identifier should be a meta identifier in itself, but as far as I can see, it doesn't fit a meta identifier (letter then any amount of numbers or letters).
Does anyone know what's going on?
回答1:
Actually, the official ISO EBNF spec (ISO/IEC 14977:1996) can be freely downloaded from the ISO website here. That actually defines meta identifier as following:
meta identifier = letter, {meta identifier character};
meta identifier character = letter | decimal digit;
It refers to 4.14 and 4.15 in this definition. There it says clearly that a meta identifier character consists only of letters and decimal digits.
It is indeed strange that the EBNF they use to specify EBNF does allow spaces in identifiers. I always believed EBNF allows whitespace in identifiers. This is also what Wikipedia says (although the grammar there is incomplete in other ways):
identifier = letter , { letter | digit | " " } ;
So perhaps they have overlooked this in the standard? Or we are both missing something. Anyway, I hope you can maybe figure it out now that you have the official standard.
回答2:
Section 6 of the standard provides your answer:
From 6.1:
6.2 to 6.4 define that a non-printing character such as space or new-line has no formal effect on a syntax if the character is outside a terminal-string or pair of characters forming a single terminal-character.
So essentially, white space is ignored.
Section 8.1 breaks the syntax down into 4 parts. Meta identifiers are defined in the 3rd part, which is prefixed with the following comment (My bold edit):
The third part of the syntax defines the removal of bracketed-textual-symbols from gap-free-symbols that form a syntax.
The way 8.1 works is that part 1 of the section defines terminal characters and gaps which are fed into part 2 which then removes gap separators which feeds into part 3 which removes comments which feeds into part 4 which builds your grammar.
回答3:
ISO/IEC 14977:1996(E) provides the following intent:
Introduction, Page viii,
d) Meta-identifier. A meta-identifier (the name of a non-terminal symbol in the language) need not be a single word or enclosed in brackets because there is an explicit concatenate symbol. This also ensures that the layout of a syntax (except in a terminal symbol) does not affect the language being defined.
Applicable definitions and rules:
4.14 Meta-identifier
A meta-identifier consists of an ordered list of one or more meta-identifier-characters subject to the condition that the first meta-identifier-character is a letter.4.15 Meta-identifier-character
A meta-identifier-character is a letter or a decimal-digit.6.2 Terminal-character
A terminal-character of Extended BNF is one of the following:
a) a letter;
b) a decimal-digit;
etc.6.3 Gap-free-symbol
A gap-free-symbol is either:
a) a terminal-character that is neither a first-quote-symbol nor a second-quote-symbol, or
b) a terminal-string.6.4 Gap-separator
A gap-separator is one of the non-printing characters: space, horizontal-tabulation, new-line, vertical-tabulation, or form-feed.
One or more gap-separators may be placed:
a) before a syntax, and
b) between any two gap-free-symbols of a syntax, and
c) after a syntax
without affecting the language defined by the syntax.
A meta-identifier
is not a gap-free-symbol
(6.3). However, the letters and digits comprising the meta-identifier
are (6.2a, 6.2b, and 6.3a). Therefore, spaces may be inserted (6.4b) to separate the words of that meta-identifier
.
After removing each gap-separator
(6.4) the resulting meta-identifier
will consist of a letter followed by zero or more letters and digits thus meeting the requirements of 4.14 and 4.15.
来源:https://stackoverflow.com/questions/15268229/ebnf-whitespacing-in-meta-identifiers