Class: RLTK::Parser::State
- Inherits:
-
Object
- Object
- RLTK::Parser::State
- Defined in:
- lib/rltk/parser.rb
Overview
The State class is used to represent sets of items and actions to be used during parsing.
Instance Attribute Summary collapse
-
#actions ⇒ Hash{Symbol => Array<Action>}
readonly
Maps lookahead symbols to actions.
-
#id ⇒ Integer
State’s ID.
-
#items ⇒ Array<CFG::Item>
readonly
Item objects that comprise this state.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compare one State to another.
-
#add_reduction(production) ⇒ void
Add a Reduce action to the state.
- #append(item) ⇒ Object (also: #<<)
-
#clean ⇒ void
Clean this State by removing the list of CFG::Item objects.
-
#close(productions) ⇒ vod
Close this state using productions.
-
#conflict_on?(sym) ⇒ :SR, ...
Checks to see if there is a conflict in this state, given a input of sym.
-
#each ⇒ void
Iterate over the state’s items.
-
#initialize(tokens, items = []) ⇒ State
constructor
Instantiate a new State object.
-
#on(symbol, action) ⇒ void
Specify an Action to perform when the input token is symbol.
-
#on?(symbol) ⇒ Array<Action>
Returns that actions that should be taken when the input token is symbol.
Constructor Details
#initialize(tokens, items = []) ⇒ State
Instantiate a new State object.
1487 1488 1489 1490 1491 |
# File 'lib/rltk/parser.rb', line 1487 def initialize(tokens, items = []) @id = nil @items = items @actions = tokens.inject(Hash.new) { |h, t| h[t] = Array.new; h } end |
Instance Attribute Details
#actions ⇒ Hash{Symbol => Array<Action>} (readonly)
Returns Maps lookahead symbols to actions.
1481 1482 1483 |
# File 'lib/rltk/parser.rb', line 1481 def actions @actions end |
#id ⇒ Integer
Returns State’s ID.
1475 1476 1477 |
# File 'lib/rltk/parser.rb', line 1475 def id @id end |
#items ⇒ Array<CFG::Item> (readonly)
Returns Item objects that comprise this state.
1478 1479 1480 |
# File 'lib/rltk/parser.rb', line 1478 def items @items end |
Instance Method Details
#==(other) ⇒ Boolean
Compare one State to another. Two States are equal if they have the same items or, if the items have been cleaned, if the States have the same ID.
1500 1501 1502 |
# File 'lib/rltk/parser.rb', line 1500 def ==(other) if self.items and other.items then self.items == other.items else self.id == other.id end end |
#add_reduction(production) ⇒ void
This method returns an undefined value.
Add a Reduce action to the state.
1509 1510 1511 1512 1513 1514 |
# File 'lib/rltk/parser.rb', line 1509 def add_reduction(production) action = Reduce.new(production) # Reduce actions are not allowed for the ERROR terminal. @actions.each { |k, v| if CFG::is_terminal?(k) and k != :ERROR then v << action end } end |
#append(item) ⇒ Object Also known as: <<
1517 1518 1519 |
# File 'lib/rltk/parser.rb', line 1517 def append(item) if item.is_a?(CFG::Item) and not @items.include?(item) then @items << item end end |
#clean ⇒ void
This method returns an undefined value.
Clean this State by removing the list of CFG::Item objects.
1525 1526 1527 |
# File 'lib/rltk/parser.rb', line 1525 def clean @items = nil end |
#close(productions) ⇒ vod
Close this state using productions.
1534 1535 1536 1537 1538 1539 1540 |
# File 'lib/rltk/parser.rb', line 1534 def close(productions) self.each do |item| if (next_symbol = item.next_symbol) and CFG::is_nonterminal?(next_symbol) productions[next_symbol].each { |p| self << p.to_item } end end end |
#conflict_on?(sym) ⇒ :SR, ...
Checks to see if there is a conflict in this state, given a input of sym. Returns :SR if a shift/reduce conflict is detected and :RR if a reduce/reduce conflict is detected. If no conflict is detected nil is returned.
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 |
# File 'lib/rltk/parser.rb', line 1550 def conflict_on?(sym) reductions = 0 shifts = 0 @actions[sym].each do |action| if action.is_a?(Reduce) reductions += 1 elsif action.is_a?(Shift) shifts += 1 end end if shifts == 1 and reductions > 0 :SR elsif reductions > 1 :RR else nil end end |
#each ⇒ void
This method returns an undefined value.
Iterate over the state’s items.
1577 1578 1579 1580 1581 1582 1583 |
# File 'lib/rltk/parser.rb', line 1577 def each current_item = 0 while current_item < @items.count yield @items.at(current_item) current_item += 1 end end |
#on(symbol, action) ⇒ void
This method returns an undefined value.
Specify an Action to perform when the input token is symbol.
1591 1592 1593 1594 1595 1596 1597 |
# File 'lib/rltk/parser.rb', line 1591 def on(symbol, action) if @actions.key?(symbol) @actions[symbol] << action else raise ParserConstructionException, "Attempting to set action for token (#{symbol}) not seen in grammar definition." end end |
#on?(symbol) ⇒ Array<Action>
Returns that actions that should be taken when the input token is symbol.
1605 1606 1607 |
# File 'lib/rltk/parser.rb', line 1605 def on?(symbol) @actions[symbol].clone end |