Class: Racc::Grammar

Inherits:
Object show all
Extended by:
Forwardable
Defined in:
lib/racc/grammar.rb

Defined Under Namespace

Classes: DefinitionEnv, PrecedenceDefinitionEnv

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug_flags = DebugFlags.new) ⇒ Grammar

Returns a new instance of Grammar.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/racc/grammar.rb', line 21

def initialize(debug_flags = DebugFlags.new)
  @symboltable = SymbolTable.new
  @debug_symbol = debug_flags.token
  @rules   = []  # :: [Rule]
  @start   = nil
  @n_expected_srconflicts = nil
  @prec_table = []
  @prec_table_closed = false
  @closed = false
  @states = nil
end

Instance Attribute Details

#n_expected_srconflictsObject

Returns the value of attribute n_expected_srconflicts.



35
36
37
# File 'lib/racc/grammar.rb', line 35

def n_expected_srconflicts
  @n_expected_srconflicts
end

#startObject (readonly)

Returns the value of attribute start.



33
34
35
# File 'lib/racc/grammar.rb', line 33

def start
  @start
end

#symboltableObject (readonly)

Returns the value of attribute symboltable.



34
35
36
# File 'lib/racc/grammar.rb', line 34

def symboltable
  @symboltable
end

Class Method Details

.define(&block) ⇒ Object

Dynamic Generation Interface



196
197
198
199
200
# File 'lib/racc/grammar.rb', line 196

def Grammar.define(&block)
  env = DefinitionEnv.new
  env.instance_eval(&block)
  env.grammar
end

Instance Method Details

#[](x) ⇒ Object



37
38
39
# File 'lib/racc/grammar.rb', line 37

def [](x)
  @rules[x]
end

#add(rule) ⇒ Object

Grammar Definition Interface

Raises:

  • (ArgumentError)


161
162
163
164
# File 'lib/racc/grammar.rb', line 161

def add(rule)
  raise ArgumentError, "rule added after the Grammar closed" if @closed
  @rules.push rule
end

#added?(sym) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/racc/grammar.rb', line 166

def added?(sym)
  @rules.detect {|r| r.target == sym }
end

#declare_precedence(assoc, syms) ⇒ Object

Raises:



175
176
177
178
# File 'lib/racc/grammar.rb', line 175

def declare_precedence(assoc, syms)
  raise CompileError, "precedence table defined twice" if @prec_table_closed
  @prec_table.push [assoc, syms]
end

#dfaObject Also known as: states



115
116
117
# File 'lib/racc/grammar.rb', line 115

def dfa
  (@states ||= States.new(self)).dfa
end

#each_index(&block) ⇒ Object



47
48
49
# File 'lib/racc/grammar.rb', line 47

def each_index(&block)
  @rules.each_index(&block)
end

#each_rule(&block) ⇒ Object Also known as: each



41
42
43
# File 'lib/racc/grammar.rb', line 41

def each_rule(&block)
  @rules.each(&block)
end

#each_with_index(&block) ⇒ Object



51
52
53
# File 'lib/racc/grammar.rb', line 51

def each_with_index(&block)
  @rules.each_with_index(&block)
end

#end_precedence_declaration(reverse) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/racc/grammar.rb', line 180

def end_precedence_declaration(reverse)
  @prec_table_closed = true
  return if @prec_table.empty?
  table = reverse ? @prec_table.reverse : @prec_table
  table.each_with_index do |(assoc, syms), idx|
    syms.each do |sym|
      sym.assoc = assoc
      sym.precedence = idx
    end
  end
end

#initObject

Computation

Raises:



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/racc/grammar.rb', line 404

def init
  return if @closed
  @closed = true
  @start ||= @rules.map {|r| r.target }.detect {|sym| not sym.dummy? }
  raise CompileError, 'no rule in input' if @rules.empty?
  add_start_rule
  @rules.freeze
  fix_ident
  compute_hash
  compute_heads
  determine_terminals
  compute_nullable_0
  @symboltable.fix
  compute_locate
  @symboltable.each_nonterminal {|t| compute_expand t }
  compute_nullable
  compute_useless
end

#intern(value, dummy = false) ⇒ Object



69
70
71
# File 'lib/racc/grammar.rb', line 69

def intern(value, dummy = false)
  @symboltable.intern(value, dummy)
end

#n_useless_nonterminalsObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/racc/grammar.rb', line 85

def n_useless_nonterminals
  @n_useless_nonterminals ||=
      begin
        n = 0
        @symboltable.each_nonterminal do |sym|
          n += 1 if sym.useless?
        end
        n
      end
end

#n_useless_rulesObject



100
101
102
103
104
105
106
107
108
109
# File 'lib/racc/grammar.rb', line 100

def n_useless_rules
  @n_useless_rules ||=
      begin
        n = 0
        each do |r|
          n += 1 if r.useless?
        end
        n
      end
end

#nfaObject



111
112
113
# File 'lib/racc/grammar.rb', line 111

def nfa
  (@states ||= States.new(self)).nfa
end

#nonterminal_baseObject



77
78
79
# File 'lib/racc/grammar.rb', line 77

def nonterminal_base
  @symboltable.nt_base
end

#parser_classObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/racc/grammar.rb', line 125

def parser_class
  states = states()   # cache
  if $DEBUG
    srcfilename = caller(1).first.slice(/\A(.*?):/, 1)
    begin
      write_log srcfilename + ".output"
    rescue SystemCallError
    end
    report = lambda {|s| $stderr.puts "racc: #{srcfilename}: #{s}" }
    if states.should_report_srconflict?
      report["#{states.n_srconflicts} shift/reduce conflicts"]
    end
    if states.rrconflict_exist?
      report["#{states.n_rrconflicts} reduce/reduce conflicts"]
    end
    g = states.grammar
    if g.useless_nonterminal_exist?
      report["#{g.n_useless_nonterminals} useless nonterminals"]
    end
    if g.useless_rule_exist?
      report["#{g.n_useless_rules} useless rules"]
    end
  end
  states.state_transition_table.parser_class
end

#sizeObject



55
56
57
# File 'lib/racc/grammar.rb', line 55

def size
  @rules.size
end

#start_symbol=(s) ⇒ Object

Raises:



170
171
172
173
# File 'lib/racc/grammar.rb', line 170

def start_symbol=(s)
  raise CompileError, "start symbol set twice'" if @start
  @start = s
end

#state_transition_tableObject



121
122
123
# File 'lib/racc/grammar.rb', line 121

def state_transition_table
  states().state_transition_table
end

#symbolsObject



73
74
75
# File 'lib/racc/grammar.rb', line 73

def symbols
  @symboltable.symbols
end

#to_sObject



59
60
61
# File 'lib/racc/grammar.rb', line 59

def to_s
  "<Racc::Grammar>"
end

#useless_nonterminal_exist?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/racc/grammar.rb', line 81

def useless_nonterminal_exist?
  n_useless_nonterminals() != 0
end

#useless_rule_exist?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/racc/grammar.rb', line 96

def useless_rule_exist?
  n_useless_rules() != 0
end

#write_log(path) ⇒ Object



151
152
153
154
155
# File 'lib/racc/grammar.rb', line 151

def write_log(path)
  File.open(path, 'w') {|f|
    LogFileGenerator.new(states()).output f
  }
end