Previous: , Up: Grammar methods   [Contents]


11.9 Precomputing the Grammar

Function: int marpa_g_precompute (Marpa_Grammar g)

Precomputation is necessary for a recognizer to be generated from a grammar. On success, marpa_g_precompute returns a non-negative number to indicate that it precomputed the grammar without issues. On failure, marpa_g_precompute returns -2.

Precomputation may return one or more events, which may be queried using the marpa_g_event() method. At this point events only occur when failure is reported, and events always report issues. But application writers should expect future versions to have events which are reported on success, as well as events which do not represent issues.

A MARPA_EVENT_LOOP_RULES event occurs when there are infinite loop rules (cycles) in the grammar. The presence of one or more of these will cause failure to be reported, but will not prevent the grammar from being precomputed.

Each MARPA_EVENT_COUNTED_NULLABLE event is a symbol which is a nullable on the right hand side of a sequence rule — a “counted” symbol. The presence of one or more of these will cause failure to be reported, and will prevent the grammar from being precomputed. So that the programmer can fix several at once, these failures are delayed until events are created for all of the counted nullables.

Each MARPA_EVENT_NULLING_TERMINAL event is a nulling symbol which is also flagged as a terminal. Since terminals cannot be of zero length, this is a logical impossibility. The presence of one or more of these will cause failure to be reported, and will prevent the grammar from being precomputed. So that the programmer can fix several at once, the failure is delayed until events are created for all of the counted nullables.

Precomputation involves freezing and then thoroughly checking the grammar. Among the reasons for precomputation to fail are the following:

More details of these can be found under the description of the appropriate code. See External error codes.

marpa_g_precompute() is unusual in that it is possible to treat one of its failures as “advisory”, and to proceed with parsing. If marpa_g_precompute() fails with an error code of MARPA_ERR_GRAMMAR_HAS_CYCLE, parsing can proceed, just as it typically would for success. The grammar will have been precomputed, as calling the marpa_g_is_precomputed() method will confirm.

Most applications, however, will want to simply treat failure with MARPA_ERR_GRAMMAR_HAS_CYCLE, as simply another failure, and fix the cycles before parsing. Cycles make a grammar infinitely ambiguous, and are considered useless in current practice. Cycles make processing the grammar less efficient, sometimes considerably so. Detection of cycles is returned as failure because that is by far the convenient thing to do for the vast majority of applications.

Return value: On success, a non-negative number. On failure, -2.

Function: int marpa_g_is_precomputed (Marpa_Grammar g)

Return value: On success, 1 if grammar g is already precomputed, 0 otherwise. On failure, -2.

Function: int marpa_g_has_cycle (Marpa_Grammar g)

This function allows the application to determine if grammar g has a cycle. As mentioned, most applications will want to treat these as fatal errors. To determine which rules are in the cycle, marpa_g_rule_is_loop() can be used.

Return value: On success, 1 if the grammar has a cycle, 0 otherwise. On failure, -2.


Previous: , Up: Grammar methods   [Contents]