Top Description Fields Constructors Methods
java_cup

public Class production

extends Object
Class Inheritance
Known Direct Subclasses
java_cup.action_production
Imports
java.util.Hashtable, .Enumeration

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()).

Author
Frank Flannery
Version
last updated: 7/3/96
See Also
java_cup.production_part, java_cup.symbol_part, java_cup.action_part

Field Summary

Modifier and TypeField and Description
protected action_part
_action

An action_part containing code for the action to be performed when we reduce with this production.

protected static Hashtable<K, V>
_all

Table of all productions.

protected terminal_set
_first_set

First set of the production.

protected int
_index

Index number of the production.

protected symbol_part
_lhs

The left hand side non-terminal.

protected boolean
_nullable

Nullability of the production (can it derive the empty string).

protected boolean
_nullable_known

Is the nullability of the production known or unknown?

protected int
_num_reductions

Count of number of reductions using this production.

protected production_part[]
_rhs

A collection of parts for the right hand side.

protected int
protected int
_rhs_length

How much of the right hand side array we are presently using.

protected int
_rhs_prec

The precedence of the rule

protected static int
next_index

Static counter for assigning unique index numbers.

Constructor Summary

AccessConstructor and Description
public
production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l, String action_str)

Full constructor.

public
production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l)

Constructor with no action string.

public
production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l, String action_str, int prec_num, int prec_side)

public
production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l, int prec_num, int prec_side)

Method Summary

Modifier and TypeMethod and Description
public action_part
action()

An action_part containing code for the action to be performed when we reduce with this production.

public static Enumeration<E>
all()

Access to all productions.

public terminal_set
check_first_set()

Update (and return) the first set based on current NT firsts.

public boolean
check_nullable()

Check to see if the production (now) appears to be nullable.

protected String
declare_labels(production_part[]
array of RHS parts.
rhs
,
int
how much of rhs to consider valid.
rhs_len
,
String
the final action string of the production.
final_action
)

Declare label names as valid variables within the action string

public boolean
equals(production other)

Equality comparison.

public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Generic equality comparison.
public static production
find(int indx)

Lookup a production by index.

public terminal_set
first_set()

First set of the production.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Produce a hash code.
public int
index()

Index number of the production.

protected static boolean
is_id_char(char
the character in question.
c
)

Determine if a character can be in a label id.

protected static boolean
is_id_start(char
the character in question.
c
)

Determine if a given character can be a label id starter.

public symbol_part
lhs()

The left hand side non-terminal.

protected String
make_declaration(String
the label name
labelname
,
String
the stack type of label?
stack_type
,
int offset)

Return label declaration code

protected int

Returns:

remaining valid length.
merge_adjacent_actions
(production_part[]
array of RHS parts.
rhs_parts
,
int
amount of that array that is valid.
len
)

Helper routine to merge adjacent actions in a set of RHS parts

public void
note_reduction_use()

Increment the count of reductions with this non-terminal

public boolean
nullable()

Nullability of the production (can it derive the empty string).

public boolean
nullable_known()

Is the nullability of the production known or unknown?

public int
num_reductions()

Count of number of reductions using this production.

public static int
number()

Total number of productions.

public int
precedence_num()

Access to the precedence of the rule

public int
protected void
remove_embedded_actions()

Remove all embedded actions from a production by factoring them out into individual action production using new non terminals.

public production_part
rhs(int indx)

Access to the collection of parts for the right hand side.

public int
rhs_length()

How much of the right hand side array we are presently using.

pack-priv boolean
set_nullable(boolean v)

set (and return) nullability

public void
set_precedence_num(int prec_num)

Setting the precedence of a rule

public void
set_precedence_side(int prec_side)

protected action_part

Returns:

the removed action part.
strip_trailing_action
(production_part[]
array of RHS parts.
rhs_parts
,
int
how many of those are valid.
len
)

Helper routine to strip a trailing action off rhs and return it

public String
to_simple_string()

Convert to a simpler string.

public String
toString()

Overrides java.lang.Object.toString.

Convert to a string.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

_actionback to summary
protected action_part _action

An action_part containing code for the action to be performed when we reduce with this production.

_allback to summary
protected static Hashtable<K, V> _all

Table of all productions. Elements are stored using their index as the key.

_first_setback to summary
protected terminal_set _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.

_indexback to summary
protected int _index

Index number of the production.

_lhsback to summary
protected symbol_part _lhs

The left hand side non-terminal.

_nullableback to summary
protected boolean _nullable

Nullability of the production (can it derive the empty string).

_nullable_knownback to summary
protected boolean _nullable_known

Is the nullability of the production known or unknown?

_num_reductionsback to summary
protected int _num_reductions

Count of number of reductions using this production.

_rhsback to summary
protected production_part[] _rhs

A collection of parts for the right hand side.

_rhs_assocback to summary
protected int _rhs_assoc
_rhs_lengthback to summary
protected int _rhs_length

How much of the right hand side array we are presently using.

_rhs_precback to summary
protected int _rhs_prec

The precedence of the rule

next_indexback to summary
protected static int next_index

Static counter for assigning unique index numbers.

Constructor Detail

productionback to summary
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 D
then 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.
productionback to summary
public production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l) throws internal_error

Constructor with no action string.

productionback to summary
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
productionback to summary
public production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l, int prec_num, int prec_side) throws internal_error

Method Detail

actionback to summary
public action_part action()

An action_part containing code for the action to be performed when we reduce with this production.

allback to summary
public static Enumeration<E> all()

Access to all productions.

check_first_setback to summary
public terminal_set check_first_set() throws internal_error

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.

check_nullableback to summary
public boolean check_nullable() throws internal_error

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.

declare_labelsback to summary
protected String declare_labels(production_part[] rhs, int rhs_len, String final_action)

Declare label names as valid variables within the action string

Parameters
rhs:production_part[]

array of RHS parts.

rhs_len:int

how much of rhs to consider valid.

final_action:String

the final action string of the production.

equalsback to summary
public boolean equals(production other)

Equality comparison.

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Generic equality comparison.

Parameters
other:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

findback to summary
public static production find(int indx)

Lookup a production by index.

first_setback to summary
public terminal_set 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.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Produce a hash code.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object.

indexback to summary
public int index()

Index number of the production.

is_id_charback to summary
protected static boolean is_id_char(char c)

Determine if a character can be in a label id.

Parameters
c:char

the character in question.

is_id_startback to summary
protected static boolean is_id_start(char c)

Determine if a given character can be a label id starter.

Parameters
c:char

the character in question.

lhsback to summary
public symbol_part lhs()

The left hand side non-terminal.

make_declarationback to summary
protected String make_declaration(String labelname, String stack_type, int offset)

Return label declaration code

Parameters
labelname:String

the label name

stack_type:String

the stack type of label?

Author
frankf
merge_adjacent_actionsback to summary
protected int merge_adjacent_actions(production_part[] rhs_parts, int len)

Helper routine to merge adjacent actions in a set of RHS parts

Parameters
rhs_parts:production_part[]

array of RHS parts.

len:int

amount of that array that is valid.

Returns:int

remaining valid length.

note_reduction_useback to summary
public void note_reduction_use()

Increment the count of reductions with this non-terminal

nullableback to summary
public boolean nullable()

Nullability of the production (can it derive the empty string).

nullable_knownback to summary
public boolean nullable_known()

Is the nullability of the production known or unknown?

num_reductionsback to summary
public int num_reductions()

Count of number of reductions using this production.

numberback to summary
public static int number()

Total number of productions.

precedence_numback to summary
public int precedence_num()

Access to the precedence of the rule

precedence_sideback to summary
public int precedence_side()
remove_embedded_actionsback to summary
protected void remove_embedded_actions() throws internal_error

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} D 
then 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.
rhsback to summary
public production_part rhs(int indx) throws internal_error

Access to the collection of parts for the right hand side.

rhs_lengthback to summary
public int rhs_length()

How much of the right hand side array we are presently using.

set_nullableback to summary
pack-priv boolean set_nullable(boolean v)

set (and return) nullability

set_precedence_numback to summary
public void set_precedence_num(int prec_num)

Setting the precedence of a rule

set_precedence_sideback to summary
public void set_precedence_side(int prec_side)
strip_trailing_actionback to summary
protected action_part strip_trailing_action(production_part[] rhs_parts, int len)

Helper routine to strip a trailing action off rhs and return it

Parameters
rhs_parts:production_part[]

array of RHS parts.

len:int

how many of those are valid.

Returns:action_part

the removed action part.

to_simple_stringback to summary
public String to_simple_string() throws internal_error

Convert to a simpler string.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Convert to a string.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object.