Greibach normal form

from Wikipedia, the free encyclopedia

The Greibach normal form is a theoretical computer science term that is of interest in connection with context-free languages . It is named after the US computer scientist Sheila A. Greibach and describes a normal form of context-free grammars . Every context-free grammar, from which the empty word cannot be derived, can be transformed into a Greibach normal form. The outstanding property of the Greibach normal form is that exactly one terminal character is created for each derivation step . This makes it the natural intermediate step in transforming a context-free grammar into an equivalent nondeterministic push-down automaton without transitions.

Another normal form for context-free grammars is the Chomsky normal form .

Formal definition

Let be a context-free grammar (see Chomsky hierarchy ), i.e. , with . It should be the amount of non-terminal symbols , the set of terminal symbols , the set of production rules and the start symbol . Be the empty element .

is in Greibach normal form ( GNF for short ), if all productions from have the form with , where is a terminal symbol and and are for non-terminals. The special thing about this form is that there is exactly one terminal symbol on the right-hand side of each production, followed by any number of non-terminals. In particular, it is possible that there is only one terminal symbol on the right side of the production.

With you get a regular grammar as a special case of a context-free grammar in Greibach normal form.

For everyone with there is a , with , in Greibach normal form.

However , it must never appear on the right side of a production. This ensures that languages ​​that contain the empty word can also be generated from a grammar in Greibach normal form.

Construction of the GNF

The following algorithm converts a grammar from the Chomsky normal form to the Greibach normal form. The algorithm is of theoretical importance because it shows that any context-free grammar, from which the empty word cannot be derived, can be transformed into a Greibach normal form. The generated Greibach normal form is not minimal and there are algorithms with better running times that calculate small Greibach normal forms.

notation

The following are:

  • Non-terminals (here represents existing non-terminal symbols and those newly introduced in the scheme)
  • Terminals and
  • the vocabulary of grammar
  • Consequences of non-terminals (e.g. )
  • Sequences of terminals and non-terminals (e.g. )

preparation

First, you bring the grammar into Chomsky normal form. An arbitrary total order on the nonterminal is required for the scheme given below . To do this, you can rename the occurring nonterminal in with . To do this, proceed as follows:

  • The first nonterminal that occurs is renamed to.
  • The second occurring nonterminal is renamed to.
  • This scheme continues until all nonterminals that occur have been replaced.

Example:

  • The first variable that occurs is , and is therefore renamed to.
  • The second variable that occurs is , and is therefore renamed to.
  • if you continue this scheme, you come to

Phase 1

At this stage, the algorithm uses the following two forms of rule substitution. After this step , that applies to all the rules of grammar .

Start of productions

With this replacement rule, the algorithm removes all rules of the . The prerequisite is that there are no recursive rules for the non-terminal . The rules of form are then replaced by replacing the nonterminal with its productions.

Regel1()
 für alle 
    für alle 
       Füge  hinzu
    ende
    Entferne 
 ende

Example: with becomes to .

Replace left recursive rules

Left recursive rules are of the form ; H. a variable can again derive on itself. Repeated insertion makes it easy to see that, through left-recursive rules, exactly the regular expression

can be generated. This can easily be achieved differently:

The rules for left-recursive are replaced by:

and adds new rules for :

Regel2()
 für alle 
    Füge  hinzu
 ende
 für alle 
    Füge  hinzu
    Füge  hinzu
    Entferne 
 ende

algorithm

The algorithm applies the above two replacement rules for to , so that rules of the From are always replaced first and then left-recursive rules are also eliminated.

 für i:=1 bis m
    für j:=1 bis i-1
       Regel1()
    ende
    Regel2()
 ende

From now on there are only rules of form or form . In particular, all rules start with a terminal symbol on the right.

Phase 2

In this phase all rules are transformed into the form via the non-terminals . The algorithm starts with the rules and replaces rules that will start with a nonterminal by employing the nonterminal's productions. This makes use of the fact that when rules are considered, the rules for are already in the desired form.

 für i:=m-1 bis 1
    für j:=i+1 bis m
         Regel1()
    ende
 ende

Phase 3

In the last step, all rules are transformed into the Greibach normal form via the new non-terminals . Rules that begin with a nonterminal are replaced by using the productions of that nonterminal.

 für alle 
    für alle 
       Füge  hinzu
    ende
    Entferne 
 ende

This makes use of the fact that the rules are all already in Greibach normal form and that they never appear as the first symbol on the right-hand side of a rule.

A stricter variant of the Greibach normal form

It is also possible to transform the productions of a context-free grammar into Greibach normal form so that a maximum of 2 variables appear on each right-hand side. The resulting productions then have the form , or .

Construction of a basement automat from the GNF

To construct a push-down automaton from a grammar in GNF , choose the state set of as , the basement alphabet , the band alphabet , the basement start symbol and the set of end states . Choose as transition relation . accepted over empty basement. Proof by induction.

literature

  • Uwe Schöning : Theoretical Computer Science - in a nutshell . 5th edition. Spektrum Akademischer Verlag, Heidelberg, ISBN 978-3-8274-1824-1 , 1.3 Context-Free Languages.
  • Ingo Wegener : Theoretical Computer Science . An algorithm-oriented introduction. BG Teubner, Stuttgart, ISBN 3-519-02123-4 , 7.1 The Greibach normal form for context-free grammars.

Individual evidence

  1. ^ Norbert Blum, Robert Koch: Greibach Normal Form Transformation Revisited . In: Information and Computation . 150, No. 1, 1999, pp. 112-118. doi : 10.1006 / inco.1998.2772 .
  2. ^ Proof of the construction of the push-down automaton from the GNF