Class: RLTK::Parser::ParseStack
- Inherits:
-
Object
- Object
- RLTK::Parser::ParseStack
- Defined in:
- lib/rltk/parser.rb
Overview
The ParseStack class is used by a Parser to keep track of state during parsing.
Instance Attribute Summary collapse
-
#id ⇒ Integer
readonly
ID of this parse stack.
-
#output_stack ⇒ Array<Object>
readonly
Array of objects produced by Reduce actions.
-
#state_stack ⇒ Array<Integer>
readonly
Array of states used when performing Reduce actions.
Instance Method Summary collapse
-
#branch(new_id) ⇒ ParseStack
Branch this stack, effectively creating a new copy of its internal state.
-
#initialize(id, ostack = [], sstack = [0], nstack = [], connections = [], labels = [], positions = []) ⇒ ParseStack
constructor
Instantiate a new ParserStack object.
-
#pop(n = 1) ⇒ Array(Object, StreamPosition)
Pop some number of objects off of the inside stacks.
-
#position ⇒ StreamPosition
Position data for the last symbol on the stack.
-
#push(state, o, node0, position) ⇒ void
Push new state and other information onto the stack.
-
#result ⇒ Object
Fetch the result stored in this ParseStack.
-
#state ⇒ Integer
Current state of this ParseStack.
-
#tree ⇒ String
Representation of the parse tree in the DOT langauge.
Constructor Details
#initialize(id, ostack = [], sstack = [0], nstack = [], connections = [], labels = [], positions = []) ⇒ ParseStack
Instantiate a new ParserStack object.
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 |
# File 'lib/rltk/parser.rb', line 1364 def initialize(id, ostack = [], sstack = [0], nstack = [], connections = [], labels = [], positions = []) @id = id @node_stack = nstack @output_stack = ostack @state_stack = sstack @connections = connections @labels = labels @positions = positions end |
Instance Attribute Details
#id ⇒ Integer (readonly)
Returns ID of this parse stack.
1347 1348 1349 |
# File 'lib/rltk/parser.rb', line 1347 def id @id end |
#output_stack ⇒ Array<Object> (readonly)
Returns Array of objects produced by Reduce actions.
1350 1351 1352 |
# File 'lib/rltk/parser.rb', line 1350 def output_stack @output_stack end |
#state_stack ⇒ Array<Integer> (readonly)
Returns Array of states used when performing Reduce actions.
1353 1354 1355 |
# File 'lib/rltk/parser.rb', line 1353 def state_stack @state_stack end |
Instance Method Details
#branch(new_id) ⇒ ParseStack
Branch this stack, effectively creating a new copy of its internal state.
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 |
# File 'lib/rltk/parser.rb', line 1382 def branch(new_id) # We have to do a deeper copy of the output stack to avoid # interactions between the Proc objects for the different # parsing paths. # # The being/rescue block is needed because some classes # respond to `clone` but always raise an error. new_output_stack = @output_stack.map do |o| # Check to see if we can obtain a deep copy. if 0.respond_to?(:copy) o.copy else begin o.clone rescue o end end end ParseStack.new(new_id, new_output_stack, @state_stack.clone, @node_stack.clone, @connections.clone, @labels.clone, @positions.clone) end |
#pop(n = 1) ⇒ Array(Object, StreamPosition)
Pop some number of objects off of the inside stacks.
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 |
# File 'lib/rltk/parser.rb', line 1439 def pop(n = 1) @state_stack.pop(n) # Pop the node stack so that the proper edges can be added # when the production's left-hand side non-terminal is # pushed onto the stack. @cbuffer = @node_stack.pop(n) [@output_stack.pop(n), @positions.pop(n)] end |
#position ⇒ StreamPosition
Returns Position data for the last symbol on the stack.
1404 1405 1406 1407 1408 1409 1410 |
# File 'lib/rltk/parser.rb', line 1404 def position if @positions.empty? StreamPosition.new else @positions.last.clone end end |
#push(state, o, node0, position) ⇒ void
This method returns an undefined value.
Push new state and other information onto the stack.
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 |
# File 'lib/rltk/parser.rb', line 1420 def push(state, o, node0, position) @state_stack << state @output_stack << o @node_stack << @labels.length @labels << if CFG::is_terminal?(node0) and o then node0.to_s + "(#{o})" else node0 end @positions << position if CFG::is_nonterminal?(node0) @cbuffer.each do |node1| @connections << [@labels.length - 1, node1] end end end |
#result ⇒ Object
Fetch the result stored in this ParseStack. If there is more than one object left on the output stack there is an error.
1454 1455 1456 1457 1458 1459 1460 |
# File 'lib/rltk/parser.rb', line 1454 def result if @output_stack.length == 1 return @output_stack.last else raise InternalParserException, "The parsing stack should have 1 element on the output stack, not #{@output_stack.length}." end end |
#state ⇒ Integer
Returns Current state of this ParseStack.
1463 1464 1465 |
# File 'lib/rltk/parser.rb', line 1463 def state @state_stack.last end |
#tree ⇒ String
Returns Representation of the parse tree in the DOT langauge.
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 |
# File 'lib/rltk/parser.rb', line 1468 def tree tree = "digraph tree#{@id} {\n" @labels.each_with_index do |label, i| tree += "\tnode#{i} [label=\"#{label}\"" if CFG::is_terminal?(label) tree += " shape=box" end tree += "];\n" end tree += "\n" @connections.each do |from, to| tree += "\tnode#{from} -> node#{to};\n" end tree += "}" end |