Van Wijngaarden grammar explained

In computer science, a Van Wijngaarden grammar (also vW-grammar or W-grammar[1]) is a formalism for defining formal languages. The name derives from the formalism invented by Adriaan van Wijngaarden[2] for the purpose of defining the ALGOL 68 programming language.The resulting specification[3] remains its most notable application.

Van Wijngaarden grammars address the problem that context-free grammars cannot express agreement or reference, where two different parts of the sentence must agree with each other in some way. For example, the sentence "The birds was eating" is not Standard English because it fails to agree on number. A context-free grammar would parse "The birds was eating" and "The birds were eating" and "The bird was eating" in the same way. However, context-free grammars have the benefit of simplicity whereas van Wijngaarden grammars are considered highly complex.[4]

Two levels

W-grammars are two-level grammars: they are defined by a pair of grammars, that operate on different levels:

The set of strings generated by a W-grammar is defined by a two-stage process:

  1. within each hyperrule, for each attribute that occurs in it, pick a value for it generated by the metagrammar; the result is a normal context-free grammar rule; do this in every possible way;
  2. use the resulting (possibly infinite) context-free grammar to generate strings in the normal way.

The consistent substitution used in the first step is the same as substitution in predicate logic, and actually supports logic programming; it corresponds to unification in Prolog, as noted by Alain Colmerauer.

W-grammars are Turing complete;[5] hence, all decision problems regarding the languages they generate, such as

are undecidable.

Curtailed variants, known as affix grammars, were developed, and applied in compiler construction and to the description of natural languages.

Definite logic programs, that is, logic programs that make no use of negation, can be viewed as a subclass of W-grammars.

Motivation and history

In the 1950s, attempts started to apply computers to the recognition, interpretation and translation of natural languages, such as English and Russian. This requires a machine-readable description of the phrase structure of sentences, that can be used to parse and interpret them, and to generate them. Context-free grammars, a concept from structural linguistics, were adopted for this purpose; their rules can express how sentences are recursively built out of parts of speech, such as noun phrases and verb phrases, and ultimately, words, such as nouns, verbs, and pronouns.

This work influenced the design and implementation of programming languages, most notably, of ALGOL 60, which introduced a syntax description in Backus–Naur form.

However, context-free rules cannot express agreement or reference (anaphora), where two different parts of the sentence must agree with each other in some way.

These can be readily expressed in W-grammars. (See example below.)

Programming languages have the analogous notions of typing and scoping.A compiler or interpreter for the language must recognize which uses of a variable belong together (refer to the same variable). This is typically subject to constraints such as:

W-grammars are based on the idea of providing the nonterminal symbols of context-free grammars with attributes (or affixes) that pass information between the nodes of the parse tree, used to constrain the syntax and to specify the semantics.

This idea was well known at the time; e.g. Donald Knuth visited the ALGOL 68 design committee while developing his own version of it, attribute grammars.[6]

By augmenting the syntax description with attributes, constraints like the above can be checked, ruling many invalid programs out at compile time.As Van Wijngaarden wrote in his preface:[2]

Quite peculiar to W-grammars was their strict treatment of attributes as strings, defined by a context-free grammar, on which concatenation is the only possible operation; complex data structures and operations can be defined by pattern matching. (See example below.)

After their introduction in the 1968 ALGOL 68 "Final Report", W-grammars were widely considered as too powerful and unconstrained to be practical.

This was partly a consequence of the way in which they had been applied; the 1973 ALGOL 68 "Revised Report" contains a much more readable grammar, without modifying the W-grammar formalism itself.

Meanwhile, it became clear that W-grammars, when used in their full generality, are indeed too powerful for such practical purposes as serving as the input for a parser generator.They describe precisely all recursively enumerable languages,[7] which makes parsing impossible in general: it is an undecidable problem to decide whether a given string can be generated by a given W-grammar.

Hence, their use must be seriously constrained when used for automatic parsing or translation. Restricted and modified variants of W-grammars were developed to address this, e.g.

After the 1970s, interest in the approach waned; occasionally, new studies are published.[8]

Examples

Agreement in English grammar

In English, nouns, pronouns and verbs have attributes such as grammatical number, gender, and person, which must agree between subject, main verb, and pronouns referring to the subject:

are valid sentences; invalid are, for instance:

Here, agreement serves to stress that both pronouns (e.g. I and myself) refer to the same person.

A context-free grammar to generate all such sentences: ::= ::= I | You | He | She | We | They ::= wash | washes ::= myself | yourself | himself | herself | ourselves | yourselves | themselvesFrom <nowiki><sentence></nowiki>, we can generate all combinations:

I wash myself I wash yourself I wash himself [...] They wash yourselves They wash themselves

A W-grammar to generate only the valid sentences: > ::= > > >

1st> ::= I 2nd> ::= You ::= He ::= She 1st> ::= We 3rd> ::= They

::= wash ::= wash ::= washes > ::= wash

1st> ::= myself 2nd> ::= yourself ::= himself ::= herself 1st> ::= ourselves 2nd> ::= yourselves 3rd> ::= themselves

::

singular | plural ::

male | female ::

1st | 2nd | 3rd

A standard non-context-free language

A well-known non-context-free language is

\{anbnan|n\ge1\}.

A two-level grammar for this language is the metagrammar

N ::= 1 | N1

X ::= a | btogether with grammar schema

Start ::= (a)(b)(a)

(X) ::= (X) X

(X) ::= X

Questions. If one substitutes a new letter, say C, for N1, is the language generated by the grammar preserved? Or N1 should be read as a string of two symbols, that is, N followed by 1?End of questions.

Requiring valid use of variables in ALGOL

The Revised Report on the Algorithmic Language Algol 60[9] defines a full context-free syntax for the language.

Assignments are defined as follows (section 4.2.1): ::= := | := ::= | ::= | A <nowiki><variable></nowiki> can be (amongst other things) an <nowiki><identifier></nowiki>, which in turn is defined as:

::= | |

Examples (section 4.2.2):

s:=p[0]:=n:=n+1+s n:=n+1 A:=B/C-v-q×S S[v,k+2]:=3-arctan(sTIMESzeta) V:=Q>Y^Z

Expressions and assignments must be type checked: for instance,

  • in <nowiki>n:=n+1</nowiki>, n must be a number (integer or real);
  • in <nowiki>A:=B/C-v-q×S</nowiki>, all variables must be numbers;
  • in <nowiki>V:=Q>Y^Z</nowiki>, all variables must be of type Boolean.

The rules above distinguish between <nowiki><arithmetic expression></nowiki> and <nowiki><Boolean expression></nowiki>, but they cannot verify that the same variable always has the same type.

This (non-context-free) requirement can be expressed in a W-grammar by annotating the rules with attributes that record, for each variable used or assigned to, its name and type.

This record can then be carried along to all places in the grammar where types need to be matched, and implement type checking.

Similarly, it can be used to checking initialization of variables before use, etcetera.

One may wonder how to create and manipulate such a data structure without explicit support in the formalism for data structures and operations on them. It can be done by using the metagrammar to define a string representation for the data structure and using pattern matching to define operations: > ::= > := | > := > ::= > is added to sorted > | > > is added to sorted > > ::= > > | > > is added to sorted > ::= is added to sorted > ::= is added to sorted > is lexicographically before > is added to sorted > ::= is added to sorted > is lexicographically before > is added to sorted > is lexicographically before > ::= is followed by > is lexicographically before > ::= is followed by > is followed by > is lexicographically before > is lexicographically before > ::= is followed by > is followed by > precedes+ precedes+ ::= precedes precedes+ ::= precedes+ precedes+ :

:

[...] ::

real | integer | Boolean ::

| | ::

| ::= ::= ::= ::

a | b | c | [...] ::

0 | 1 | 2 | [...] ::

::

::

::

::

| ::

::

( ) ::

::

::

When compared to the original grammar, three new elements have been added:

  • attributes to the nonterminals in what are now the hyperrules;
  • metarules to specify the allowable values for the attributes;
  • new hyperrules to specify operations on the attribute values.

The new hyperrules are -rules: they only generate the empty string.

ALGOL 68 examples

The ALGOL 68 reports use a slightly different notation without .

ALGOL 68 as in the 1968 Final Report §2.1

a) program : open symbol, standard prelude, library prelude option, particular program, exit, library postlude option, standard postlude, close symbol. b) standard prelude : declaration prelude sequence. c) library prelude : declaration prelude sequence. d) particular program : label sequence option, strong CLOSED void clause. e) exit : go on symbol, letter e letter x letter i letter t, label symbol. f) library postlude : statement interlude. g) standard postlude : strong void clause train

ALGOL 68 as in the 1973 Revised Report §2.2.1, §10.1.1

program : strong void new closed clause A) EXTERNAL :: standard ; library ; system ; particular. B) STOP :: label letter s letter t letter o letter p. a) program text : STYLE begin token, new LAYER1 preludes, parallel token, new LAYER1 tasks PACK, STYLE end token. b) NEST1 preludes : NEST1 standard prelude with DECS1, NEST1 library prelude with DECSETY2, NEST1 system prelude with DECSETY3, where (NEST1) is (new EMPTY new DECS1 DECSETY2 DECSETY3). c) NEST1 EXTERNAL prelude with DECSETY1 : strong void NEST1 series with DECSETY1, go on token ; where (DECSETY1) is (EMPTY), EMPTY. d) NEST1 tasks : NEST1 system task list, and also token, NEST1 user task PACK list. e) NEST1 system task : strong void NEST1 unit. f) NEST1 user task : NEST2 particular prelude with DECS, NEST2 particular program PACK, go on token, NEST2 particular postlude, where (NEST2) is (NEST1 new DECS STOP). g) NEST2 particular program : NEST2 new LABSETY3 joined label definition of LABSETY3, strong void NEST2 new LABSETY3 ENCLOSED clause. h) NEST joined label definition of LABSETY : where (LABSETY) is (EMPTY), EMPTY ; where (LABSETY) is (LAB1 LABSETY1), NEST label definition of LAB1, NEST joined label definition of$ LABSETY1. i) NEST2 particular postlude : strong void NEST2 series with STOP.

A simple example of the power of W-grammars is clause

a) program text : STYLE begin token, new LAYER1 preludes, parallel token, new LAYER1 tasks PACK, STYLE end token.

This allows BEGIN ... END and { } as block delimiters, while ruling out BEGIN ... } and { ... END.

One may wish to compare the grammar in the report with the Yacc parser for a subset of ALGOL 68 by Marc van Leeuwen.

Implementations

Anthony Fisher wrote yo-yo,[10] a parser for a large class of W-grammars, with example grammars for expressions, eva, sal and Pascal (the actual ISO 7185 standard for Pascal uses extended Backus–Naur form).

Dick Grune created a C program that would generate all possible productions of a W-grammar.[11]

Applications outside of ALGOL 68

The applications of Extended Affix Grammars (EAG)s mentioned above can effectively be regarded as applications of W-grammars, since EAGs are so close to W-grammars.[12]

W-grammars have also been proposed for the description of complex human actions in ergonomics.

A W-Grammar Description has also been supplied for Ada.

See also

Further reading

Notes and References

  1. Book: Grammars for Programming Languages. Cleaveland. J. Craig. Uzgalis. Robert C.. Elsevier. 1977. 978-0-444-00199-3.
  2. Adriaan. van Wijngaarden. Adriaan van Wijngaarden. MR 76: Orthogonal design and description of a formal language. dead. en. Amsterdam. CWI. Premature and preliminary edition 1965-10-22. 1972-04-04. https://web.archive.org/web/20171002212915/http://www.eah-jena.de/~kleine/history/languages/VanWijngaarden-MR76.pdf. 2017-10-02. PDF.
  3. Web site: Revised Report on the Algorithmic Language ALGOL 68. https://web.archive.org/web/20020124152423/http://burks.bton.ac.uk/burks/language/other/a68rr/rrtoc.htm. 24 January 2002. A.. van Wijngaarden. etal.
  4. Book: Koster, C.H.A. Cornelis H. A. Koster

    . 1181. Perspectives of System Informatics . The making of Algol 68 . Lecture Notes in Computer Science . Cornelis H. A. Koster. Bjørner. D. Broy. M.. Pottosin. I.V.. 1996. 55–67 . Berlin. Springer. 10.1007/3-540-62064-8_6. 978-3-540-62064-8 .

  5. Sintzoff. M.. Existence of van Wijngaarden syntax for every recursively enumerable set. Annales de la Société Scientifique de Bruxelles. 2. 1967. 115–118.
  6. .
  7. M. . Sintzoff . Michel Sintzoff . Existence of a van Wijngaarden syntax for every recursively enumerable set . Annales de la Société scientifique de Bruxelles . 1967 . 81 . 115–118.
  8. Augusto. L. M.. 2023. Two-level grammars: Some interesting properties of van Wijngaarden grammars. Omega - Journal of Formal Languages. 1. 3–34.
  9. Backus. etal. J.W.. Revised report on the algorithmic language ALGOL 60. The Computer Journal. 5. 4. 1963. 349–367. 10.1093/comjnl/5.4.349. free.
  10. .
  11. .
  12. Book: Attribute Grammars, Applications and Systems. Henk. Alblas. Borivoj. Melichar. Lecture Notes in Computer Science. 545. Springer. 371. 1991. 978-3540545729.