Package java_cup
Class production
java.lang.Object
java_cup.production
- Direct Known Subclasses:
action_production
This class represents a production in the grammar. It contains
a LHS non terminal, and an array of RHS symbols. As various
transformations are done on the RHS of the production, it may shrink.
As a result a separate length is always maintained to indicate how much
of the RHS array is still valid.
I addition to construction and manipulation operations, productions provide methods for factoring out actions (see remove_embedded_actions()), for computing the nullability of the production (i.e., can it derive the empty string, see check_nullable()), and operations for computing its first set (i.e., the set of terminals that could appear at the beginning of some string derived from the production, see check_first_set()).
- Version:
- last updated: 7/3/96
- Author:
- Frank Flannery
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected action_partAn action_part containing code for the action to be performed when we reduce with this production.protected static HashtableTable of all productions.protected terminal_setFirst set of the production.protected intIndex number of the production.protected symbol_partThe left hand side non-terminal.protected booleanNullability of the production (can it derive the empty string).protected booleanIs the nullability of the production known or unknown?protected intCount of number of reductions using this production.protected production_part[]A collection of parts for the right hand side.protected intprotected intHow much of the right hand side array we are presently using.protected intThe precedence of the ruleprotected static intStatic counter for assigning unique index numbers. -
Constructor Summary
ConstructorsConstructorDescriptionproduction(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l) Constructor with no action string.production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l, int prec_num, int prec_side) production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l, String action_str) Full constructor.production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l, String action_str, int prec_num, int prec_side) -
Method Summary
Modifier and TypeMethodDescriptionaction()An action_part containing code for the action to be performed when we reduce with this production.static Enumerationall()Access to all productions.Update (and return) the first set based on current NT firsts.booleanCheck to see if the production (now) appears to be nullable.static voidclear()protected Stringdeclare_labels(production_part[] rhs, int rhs_len, String final_action) Declare label names as valid variables within the action stringbooleanequals(production other) Equality comparison.booleanGeneric equality comparison.static productionfind(int indx) Lookup a production by index.First set of the production.inthashCode()Produce a hash code.intindex()Index number of the production.protected static booleanis_id_char(char c) Determine if a character can be in a label id.protected static booleanis_id_start(char c) Determine if a given character can be a label id starter.lhs()The left hand side non-terminal.protected Stringmake_declaration(String labelname, String stack_type, int offset) Return label declaration codeprotected intmerge_adjacent_actions(production_part[] rhs_parts, int len) Helper routine to merge adjacent actions in a set of RHS partsvoidIncrement the count of reductions with this non-terminalbooleannullable()Nullability of the production (can it derive the empty string).booleanIs the nullability of the production known or unknown?intCount of number of reductions using this production.static intnumber()Total number of productions.intAccess to the precedence of the ruleintprotected voidRemove all embedded actions from a production by factoring them out into individual action production using new non terminals.rhs(int indx) Access to the collection of parts for the right hand side.intHow much of the right hand side array we are presently using.voidset_precedence_num(int prec_num) Setting the precedence of a rulevoidset_precedence_side(int prec_side) protected action_partstrip_trailing_action(production_part[] rhs_parts, int len) Helper routine to strip a trailing action off rhs and return itConvert to a simpler string.toString()Convert to a string.
-
Field Details
-
_all
Table of all productions. Elements are stored using their index as the key. -
next_index
protected static int next_indexStatic counter for assigning unique index numbers. -
_lhs
The left hand side non-terminal. -
_rhs_prec
protected int _rhs_precThe precedence of the rule -
_rhs_assoc
protected int _rhs_assoc -
_rhs
A collection of parts for the right hand side. -
_rhs_length
protected int _rhs_lengthHow much of the right hand side array we are presently using. -
_action
An action_part containing code for the action to be performed when we reduce with this production. -
_index
protected int _indexIndex number of the production. -
_num_reductions
protected int _num_reductionsCount of number of reductions using this production. -
_nullable_known
protected boolean _nullable_knownIs the nullability of the production known or unknown? -
_nullable
protected boolean _nullableNullability of the production (can it derive the empty string). -
_first_set
First set of the production. This is the set of terminals that could appear at the front of some string derived from this production.
-
-
Constructor Details
-
production
public production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l, String action_str) throws internal_error Full constructor. This constructor accepts a LHS non terminal, an array of RHS parts (including terminals, non terminals, and actions), and a string for a final reduce action. It does several manipulations in the process of creating a production object. After some validity checking it translates labels that appear in actions into code for accessing objects on the runtime parse stack. It them merges adjacent actions if they appear and moves any trailing action into the final reduce actions string. Next it removes any embedded actions by factoring them out with new action productions. Finally it assigns a unique index to the production.Factoring out of actions is accomplished by creating new "hidden" non terminals. For example if the production was originally:
A ::= B {action} C Dthen it is factored into two productions:A ::= B X C D X ::= {action}(where X is a unique new non terminal). This has the effect of placing all actions at the end where they can be handled as part of a reduce by the parser.- Throws:
internal_error
-
production
public production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l) throws internal_error Constructor with no action string.- Throws:
internal_error
-
production
public production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l, String action_str, int prec_num, int prec_side) throws internal_error - Throws:
internal_error
-
production
public production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l, int prec_num, int prec_side) throws internal_error - Throws:
internal_error
-
-
Method Details
-
all
Access to all productions. -
find
Lookup a production by index. -
clear
public static void clear() -
number
public static int number()Total number of productions. -
lhs
The left hand side non-terminal. -
precedence_num
public int precedence_num()Access to the precedence of the rule -
precedence_side
public int precedence_side() -
set_precedence_num
public void set_precedence_num(int prec_num) Setting the precedence of a rule -
set_precedence_side
public void set_precedence_side(int prec_side) -
rhs
Access to the collection of parts for the right hand side.- Throws:
internal_error
-
rhs_length
public int rhs_length()How much of the right hand side array we are presently using. -
action
An action_part containing code for the action to be performed when we reduce with this production. -
index
public int index()Index number of the production. -
num_reductions
public int num_reductions()Count of number of reductions using this production. -
note_reduction_use
public void note_reduction_use()Increment the count of reductions with this non-terminal -
nullable_known
public boolean nullable_known()Is the nullability of the production known or unknown? -
nullable
public boolean nullable()Nullability of the production (can it derive the empty string). -
first_set
First set of the production. This is the set of terminals that could appear at the front of some string derived from this production. -
is_id_start
protected static boolean is_id_start(char c) Determine if a given character can be a label id starter.- Parameters:
c- the character in question.
-
is_id_char
protected static boolean is_id_char(char c) Determine if a character can be in a label id.- Parameters:
c- the character in question.
-
make_declaration
Return label declaration code- Parameters:
labelname- the label namestack_type- the stack type of label?
-
declare_labels
Declare label names as valid variables within the action string- Parameters:
rhs- array of RHS parts.rhs_len- how much of rhs to consider valid.final_action- the final action string of the production.lhs_type- the object type associated with the LHS symbol.
-
merge_adjacent_actions
Helper routine to merge adjacent actions in a set of RHS parts- Parameters:
rhs_parts- array of RHS parts.len- amount of that array that is valid.- Returns:
- remaining valid length.
-
strip_trailing_action
Helper routine to strip a trailing action off rhs and return it- Parameters:
rhs_parts- array of RHS parts.len- how many of those are valid.- Returns:
- the removed action part.
-
remove_embedded_actions
Remove all embedded actions from a production by factoring them out into individual action production using new non terminals. if the original production was:A ::= B {action1} C {action2} Dthen it will be factored into:A ::= B NT$1 C NT$2 D NT$1 ::= {action1} NT$2 ::= {action2}where NT$1 and NT$2 are new system created non terminals.- Throws:
internal_error
-
check_nullable
Check to see if the production (now) appears to be nullable. A production is nullable if its RHS could derive the empty string. This results when the RHS is empty or contains only non terminals which themselves are nullable.- Throws:
internal_error
-
check_first_set
Update (and return) the first set based on current NT firsts. This assumes that nullability has already been computed for all non terminals and productions.- Throws:
internal_error
-
equals
Equality comparison. -
equals
Generic equality comparison. -
hashCode
public int hashCode()Produce a hash code. -
toString
Convert to a string. -
to_simple_string
Convert to a simpler string.- Throws:
internal_error
-