Class: RLTK::Parser::ParseStack

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(id, ostack = [], sstack = [0], nstack = [], connections = [], labels = [], positions = []) ⇒ ParseStack

Instantiate a new ParserStack object.

Parameters:

  • id (Integer)

    ID for this parse stack. Used by GLR algorithm.

  • ostack (Array<Object>) (defaults to: [])

    Output stack. Holds results of Reduce and Shift actions.

  • sstack (Array<Integer>) (defaults to: [0])

    State stack. Holds states that have been shifted due to Shift actions.

  • nstack (Array<Integer>) (defaults to: [])

    Node stack. Holds dot language IDs for nodes in the parse tree.

  • connections (Array<Array<Integer>>) (defaults to: [])

    Integer pairs representing edges in the parse tree.

  • labels (Array<Symbol>) (defaults to: [])

    Labels for nodes in the parse tree.

  • positions (Array<StreamPosition>) (defaults to: [])

    Position data for symbols that have been shifted.



1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
# File 'lib/rltk/parser.rb', line 1272

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

#idInteger (readonly)

Returns ID of this parse stack.

Returns:

  • (Integer)

    ID of this parse stack.



1255
1256
1257
# File 'lib/rltk/parser.rb', line 1255

def id
  @id
end

#output_stackArray<Object> (readonly)

Returns Array of objects produced by Reduce actions.

Returns:

  • (Array<Object>)

    Array of objects produced by Reduce actions.



1258
1259
1260
# File 'lib/rltk/parser.rb', line 1258

def output_stack
  @output_stack
end

#state_stackArray<Integer> (readonly)

Returns Array of states used when performing Reduce actions.

Returns:

  • (Array<Integer>)

    Array of states used when performing Reduce actions.



1261
1262
1263
# File 'lib/rltk/parser.rb', line 1261

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.

Parameters:

  • new_id (Integer)

    ID for the new ParseStack.

Returns:



1290
1291
1292
1293
# File 'lib/rltk/parser.rb', line 1290

def branch(new_id)
	ParseStack.new(new_id, @output_stack.clone, @state_stack.clone, @node_stack.clone,
		@connections.clone, @labels.clone, @positions.clone)
end

#pop(n = 1) ⇒ Array<Array<Object, StreamPosition>>

Pop some number of objects off of the inside stacks.

Parameters:

  • n (Integer) (defaults to: 1)

    Number of object to pop off the stack.

Returns:



1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
# File 'lib/rltk/parser.rb', line 1331

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

#positionStreamPosition

Returns Position data for the last symbol on the stack.

Returns:

  • (StreamPosition)

    Position data for the last symbol on the stack.



1296
1297
1298
1299
1300
1301
1302
# File 'lib/rltk/parser.rb', line 1296

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.

Parameters:

  • state (Integer)

    ID of the shifted state.

  • o (Object)

    Value of Token that caused the shift.

  • node0 (Symbol)

    Label for node in parse tree.

  • position (StreamPosition)

    Position token that got shifted.



1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
# File 'lib/rltk/parser.rb', line 1312

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

#resultObject

Fetch the result stored in this ParseStack. If there is more than one object left on the output stack there is an error.

Returns:

  • (Object)

    The end result of this parse stack.



1346
1347
1348
1349
1350
1351
1352
# File 'lib/rltk/parser.rb', line 1346

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

#stateInteger

Returns Current state of this ParseStack.

Returns:

  • (Integer)

    Current state of this ParseStack.



1355
1356
1357
# File 'lib/rltk/parser.rb', line 1355

def state
	@state_stack.last
end

#treeString

Returns Representation of the parse tree in the DOT langauge.

Returns:

  • (String)

    Representation of the parse tree in the DOT langauge.



1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
# File 'lib/rltk/parser.rb', line 1360

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