Class: Rly::RuleParser

Inherits:
Yacc
  • Object
show all
Defined in:
lib/rly/parse/rule_parser.rb

Instance Attribute Summary

Attributes inherited from Yacc

#lex, #lr_table

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Yacc

assign_rhs, collect_to_a, error_count, #initialize, #inspect, lexer, on_error, #parse, parsed_rules, precedence, rule, store_grammar, with_values

Constructor Details

This class inherits a constructor from Rly::Yacc

Class Method Details

.lexer_classObject


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rly/parse/rule_parser.rb', line 7

def self.lexer_class
  return @lexer_class if @lexer_class

  @lexer_class = Class.new(Lex) do
    token :PREC, /\%prec/
    token :ID, /[a-zA-Z_][a-zA-Z_0-9]*/
    token :LITERAL, /"."|'.'/ do |t|
      t.value = t.value[1]
      t
    end
    literals ":|"
    ignore " \t\n"
  end

  @lexer_class
end

Instance Method Details

#grammarObject


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rly/parse/rule_parser.rb', line 24

def grammar
  return @grammar if @grammar

  @grammar = Grammar.new(self.class.lexer_class.terminals)

  @grammar.add_production(:grammar_def, [:grammar]) do |d, g|
    d.value = g.value
  end
  @grammar.add_production(:grammar, [:ID, ':', :rules, :PREC, :ID]) do |g, pname, _, r, _, prec|
    productions = []
    r.value.each do |p|
      productions << [pname.value.to_sym, p, prec.value.to_sym]
    end
    g.value = productions
  end
  @grammar.add_production(:grammar, [:ID, ':', :rules]) do |g, pname, _, r|
    productions = []
    r.value.each do |p|
      productions << [pname.value.to_sym, p, nil]
    end
    g.value = productions
  end
  @grammar.add_production(:rules, [:rule, '|', :rules]) do |rls, r, _, rl|
    rls.value = [r.value] + rl.value
  end
  @grammar.add_production(:rules, [:rule]) do |rl, r|
    rl.value = [r.value]
  end
  @grammar.add_production(:rules, []) do |rl|
    rl.value = [[]]
  end
  @grammar.add_production(:rule, [:tokens]) do |r, tok|
    r.value = tok.value
  end
  @grammar.add_production(:tokens, [:ID, :tokens]) do |t, i, toks|
    t.value = [i.value.to_sym] + toks.value
  end
  @grammar.add_production(:tokens, [:LITERAL, :tokens]) do |t, l, toks|
    t.value = [l.value] + toks.value
  end
  @grammar.add_production(:tokens, [:ID]) do |t, i|
    t.value = [i.value.to_sym]
  end
  @grammar.add_production(:tokens, [:LITERAL]) do |t, l|
    t.value = [l.value]
  end

  @grammar.set_start

  @grammar.build_lritems

  @lr_table = LRTable.new(@grammar)

  @lr_table.parse_table

  @grammar
end