Previous: , Up: Grammar methods   [Contents][Index]


13.8 Precomputing the Grammar

Function: int marpa_g_has_cycle (Marpa_Grammar g)

[Accessor] On success, returns a boolean which is 1 iff g has a cycle. Cycles make a grammar infinitely ambiguous, and are considered useless in current practice. Cycles make processing the grammar less efficient, sometimes considerably so. Applications will almost always want to treat cycles as mistakes on the part of the writer of the grammar. To determine which rules are in the cycle, marpa_g_rule_is_loop() can be used.

Return value: On success, a boolean. On hard failure, -2.

Function: int marpa_g_is_precomputed (Marpa_Grammar g)

[Accessor] Return value: On success, a boolean which is 1 iff grammar g is precomputed. On hard failure, -2.

Function: int marpa_g_precompute (Marpa_Grammar g)

[Mutator] On success, and on fully recoverable hard failure, precomputes the grammar g. Precomputation involves running a series of grammar checks and “precomputing” some useful information which is kept internally to save repeated calculations. After precomputation, the grammar is “frozen” in many respects, and many grammar mutators which succeed before precomputation will cause hard failures after precomputation. Precomputation is necessary for a recognizer to be generated from a grammar.

When called, clears any events already in the event queue. May return one or more events. The types of event that this method may return are A MARPA_EVENT_LOOP_RULES, MARPA_EVENT_COUNTED_NULLABLE, MARPA_EVENT_NULLING_TERMINAL. All of these events occur only on failure. Applications must be prepared for this method to return additional events, including events which occur on success. Events may be queried using the marpa_g_event() method. See marpa_g_event().

The fully recoverable hard failure is MARPA_ERR_GRAMMAR_HAS_CYCLE. Recall that for fully recoverable hard failures this method precomputes the grammar. Most appplications, however, will want to treat a grammar with cycles as if it were a library-recoverable error. A MARPA_ERR_GRAMMAR_HAS_CYCLE error occurs iff a MARPA_EVENT_LOOP_RULES event occurs. For more details on cycles, see marpa_g_has_cycle().

The error code MARPA_ERR_COUNTED_NULLABLE is library-recoverable. This failure occurs when a symbol on the RHS of a sequence rule is nullable, which Libmarpa does not allow in a grammar. Error code MARPA_ERR_COUNTED_NULLABLE occurs iff one or more MARPA_EVENT_COUNTED_NULLABLE events occur. There is one MARPA_EVENT_COUNTED_NULLABLE event for every symbol which is a nullable on the right hand side of a sequence rule. An application may use these events to inform the user of the problematic symbols, and this detail may help the user fix the grammar.

The error code item MARPA_ERR_NULLING_TERMINAL is library-recoverable. This failure occurs when a nulling symbol is also flagged as a terminal. Since terminals cannot be of zero length, this is a logical impossibility, and Libmarpa does not allow nulling terminals in a grammar. Error code item MARPA_ERR_NULLING_TERMINAL occurs iff one or more MARPA_EVENT_NULLING_TERMINAL events occur. There is one MARPA_EVENT_NULLING_TERMINAL events for every nulling terminal in the grammar. An application may use these events to inform the user of the problematic symbols, and this detail may help the user fix the grammar.

Among the other error codes which may case this method to fail are the following:

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

Return value: On success, a non-negative number, whose value is otherwise indeterminate. On hard failure, -2. For the error code MARPA_ERR_GRAMMAR_HAS_CYCLE, the hard failure is fully recoverable. For the error codes MARPA_ERR_COUNTED_NULLABLE and MARPA_ERR_NULLING_TERMINAL, the hard failure is library-recoverable.


Previous: , Up: Grammar methods   [Contents][Index]