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


13.5 Rule methods

Function: int marpa_g_highest_rule_id (Marpa_Grammar g)

[Accessor] Return value: On success, the numerically largest rule ID of g. On hard failure, -2.

Function: int marpa_g_rule_is_accessible (Marpa_Grammar g, Marpa_Rule_ID rule_id)

[Accessor] A rule is accessible if it can be reached from the start symbol. A rule is accessible if and only if its LHS symbol is accessible. The start rule is always an accessible rule.

Soft fails if rule_id is well-formed (a non-negative integer), but a rule with that ID does not exist. A common hard failure is calling this method with a grammar that is not precomputed.

Return value: On success 1 or 0: 1 if rule with ID rule_id is accessible, 0 if not. On soft failure, -1. On hard failure, -2.

Function: int marpa_g_rule_is_nullable ( Marpa_Grammar g, Marpa_Rule_ID ruleid)

[Accessor] A rule is nullable if it sometimes produces the empty string. A nulling rule is always a nullable rule, but not all nullable rules are nulling rules.

Soft fails if rule_id is well-formed (a non-negative integer), but a rule with that ID does not exist. A common hard failure is calling this method with a grammar that is not precomputed.

Return value: On success 1 or 0: 1 if the rule with ID rule_id is nullable, 0 if not. On soft failure, -1. On hard failure, -2.

Function: int marpa_g_rule_is_nulling (Marpa_Grammar g, Marpa_Rule_ID ruleid)

[Accessor] A rule is nulling if it always produces the empty string.

Soft fails if rule_id is well-formed (a non-negative integer), but a rule with that ID does not exist. A common hard failure is calling this method with a grammar that is not precomputed.

Return value: On success 1 or 0: 1 if the rule with ID rule_id is nulling, 0 if not. On soft failure, -1. On hard failure, -2.

Function: int marpa_g_rule_is_loop (Marpa_Grammar g, Marpa_Rule_ID rule_id)

[Accessor] A rule is a loop rule if it non-trivially produces the string of length one which consists only of its LHS symbol. Such a derivation takes the parse back to where it started, hence the term “loop”. “Non-trivially” means the zero-step derivation does not count — the derivation must have at least one step.

The presence of a loop rule makes a grammar infinitely ambiguous, and applications will typically want to treat them as fatal errors. But nothing forces an application to do this, and Marpa will successfully parse and evaluate grammars with loop rules.

Soft fails if rule_id is well-formed (a non-negative integer), but a rule with that ID does not exist. A common hard failure is calling this method with a grammar that is not precomputed.

Return value: On success 1 or 0: 1 if the rule with ID rule_id is a loop rule, 0 if not. On soft failure, -1. On hard failure, -2.

Function: int marpa_g_rule_is_productive (Marpa_Grammar g, Marpa_Rule_ID rule_id)

[Accessor] A rule is productive if it can produce a string of terminals. A rule is productive if and only if all the symbols on its RHS are productive. The empty string counts as a string of terminals, so that a nullable rule is always a productive rule. For that same reason, an empty rule is considered productive.

Soft fails if rule_id is well-formed (a non-negative integer), but a rule with that ID does not exist. A common hard failure is calling this method with a grammar that is not precomputed.

Return value: On success 1 or 0: 1 if the rule with ID rule_id is productive, 0 if not. On soft failure, -1. On hard failure, -2.

Function: int marpa_g_rule_length ( Marpa_Grammar g, Marpa_Rule_ID rule_id)

[Accessor] The length of a rule is the number of symbols on its RHS.

Soft fails if rule_id is well-formed (a non-negative integer), but a rule with that ID does not exist.

Return value: On success, the length of the rule with ID rule_id. On soft failure, -1. On hard failure, -2.

Function: Marpa_Symbol_ID marpa_g_rule_lhs ( Marpa_Grammar g, Marpa_Rule_ID rule_id)

[Accessor] Soft fails if rule_id is well-formed (a non-negative integer), but a rule with that ID does not exist.

Return value: On success, the ID of the LHS symbol of the rule with ID rule_id. On soft failure, -1. On hard failure, -2.

Function: Marpa_Rule_ID marpa_g_rule_new (Marpa_Grammar g, Marpa_Symbol_ID lhs_id, Marpa_Symbol_ID *rhs_ids, int length)

[Mutator] On success, creates a new external BNF rule in grammar g. The ID of the new rule will be a non-negative integer, which will be unique to that rule. In addition to BNF rules, Marpa also allows sequence rules, which are created by the marpa_g_sequence_new() method. See marpa_g_sequence_new().

Sequence rules and BNF rules are both rules: They share the same series of rule IDs, and are accessed and manipulated by the same methods, with the only differences being as noted in the descriptions of those methods.

The LHS symbol is lhs_id, and there are length symbols on the RHS. The RHS symbols are in an array pointed to by rhs_ids.

Possible hard failures, with their error codes, include:

Return value: On success, the ID of the new external rule. On hard failure, -2.

Function: Marpa_Symbol_ID marpa_g_rule_rhs ( Marpa_Grammar g, Marpa_Rule_ID rule_id, int ix)

[Accessor] When successful, returns the ID of the symbol at index ix in the RHS of the rule with ID rule_id. The indexing of RHS symbols is zero-based.

Soft fails if rule_id is well-formed (a non-negative integer), but a rule with that ID does not exist.

A common hard failure is for ix not to be a valid index of the RHS. This happens if ix is less than zero, or or if ix is greater than or equal to the length of the rule.

Return value: On success, a symbol ID, which is always non-negative. On soft failure, -1. On hard failure, -2.


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