Class: Rley::Parser::GFGEarleyParser

Inherits:
Base::BaseParser show all
Defined in:
lib/rley/parser/gfg_earley_parser.rb

Overview

Implementation of a parser that uses the Earley parsing algorithm.

Instance Attribute Summary collapse

Attributes inherited from Base::BaseParser

#dotted_items, #grammar

Instance Method Summary collapse

Methods included from Base::GrmItemsBuilder

#build_dotted_items

Constructor Details

#initialize(aGrammar) ⇒ GFGEarleyParser

Constructor.

Parameters:



17
18
19
20
# File 'lib/rley/parser/gfg_earley_parser.rb', line 17

def initialize(aGrammar)
  super(aGrammar)
  @gf_graph = GFG::GrmFlowGraph.new(dotted_items)
end

Instance Attribute Details

#gf_graphGFG::GrmFlowGraph (readonly)

The Grammar Flow graph generated from the provided grammar.

Returns:



13
14
15
# File 'lib/rley/parser/gfg_earley_parser.rb', line 13

def gf_graph
  @gf_graph
end

Instance Method Details

#parse(aTokenSequence) ⇒ GFGParsing

Parse a sequence of input tokens. tokenizer/scanner/lexer.

Parameters:

  • aTokenSequence (Array)

    Array of Tokens objects returned by a

Returns:

  • (GFGParsing)

    an object that embeds the parse results.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rley/parser/gfg_earley_parser.rb', line 26

def parse(aTokenSequence)
  result = GFGParsing.new(gf_graph)
  token_count = aTokenSequence.size
  if token_count.zero? && !grammar.start_symbol.nullable?
    return unexpected_empty_input(result)
  end

  aTokenSequence.each_with_index do |token, i|
    parse_for_token(result, i)
    if token.terminal.kind_of?(String)
      symb = grammar.name2symbol[token.terminal]
      token.instance_variable_set(:@terminal, symb)
    end
    scan_success = scan_rule(result, i, token)
    break unless scan_success
  end
  parse_for_token(result, token_count) unless result.failure_reason

  result.done # End of parsing process
  return result
end