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


11.5 Rules

Function: int marpa_g_highest_rule_id (Marpa_Grammar g)

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

Function: int marpa_g_rule_is_accessible (Marpa_Grammar g, Marpa_Rule_ID rule_id)

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.

Return value: On success, 1 if rule rule_id is accessible, 0 if not. If rule_id is well-formed, but there is no such rule, -1. If the grammar is not precomputed, or on other failure, -2.

Function: int marpa_g_rule_is_nullable ( Marpa_Grammar g, Marpa_Rule_ID ruleid)

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.

Return value: On success, 1 if rule ruleid is nullable, 0 if not. If rule_id is well-formed, but there is no such rule, -1. If the grammar is not precomputed, or on other failure, -2.

Function: int marpa_g_rule_is_nulling (Marpa_Grammar g, Marpa_Rule_ID ruleid)

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

Return value: On success, 1 if rule ruleid is nulling, 0 if not. If rule_id is well-formed, but there is no such rule, -1. If the grammar is not precomputed, or on other failure, -2.

Function: int marpa_g_rule_is_loop (Marpa_Grammar g, Marpa_Rule_ID rule_id)

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.

Return value: On success, 1 if rule rule_id is a loop rule, 0 if not. If rule_id is well-formed, but there is no such rule, -1. If the grammar is not precomputed, or on other failure, -2.

Function: int marpa_g_rule_is_productive (Marpa_Grammar g, Marpa_Rule_ID rule_id)

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.

Return value: On success, 1 if rule rule_id is productive, 0 if not. If rule_id is well-formed, but there is no such rule, -1. If the grammar is not precomputed, or on other failure, -2.

Function: int marpa_g_rule_length ( Marpa_Grammar g, Marpa_Rule_ID rule_id)

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

Return value: On success, the length of rule rule_id. On failure, -2.

Function: Marpa_Symbol_ID marpa_g_rule_lhs ( Marpa_Grammar g, Marpa_Rule_ID rule_id)

Return value: On success, the LHS symbol of rule rule_id. If rule_id is well-formed, but there is no such rule, -1. On other 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)

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 another method: marpa_g_sequence_new(). Sequence rules and BNF rules are numbered in the same series, so that a BNF rule will never have the same rule ID as a sequence rule, and vice versa.

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 failures, with their error codes, include:

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

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

Returns the ID of the symbol in position ix in the RHS of rule rule_id. The RHS position, ix, is zero-based.

Return value: On success, the ID of the symbol in position ix on the rules RHS. If rule_id is well-formed, but there is no such rule, -1. If ix is greater than or equal to the length of the rule, or on other failure, -2.


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