Class: Rly::LRTable
- Inherits:
-
Object
- Object
- Rly::LRTable
- Defined in:
- lib/rly/parse/lr_table.rb
Constant Summary collapse
- MAXINT =
(2**(0.size * 8 -2) -1)
Instance Attribute Summary collapse
-
#lr_action ⇒ Object
readonly
Returns the value of attribute lr_action.
-
#lr_goto ⇒ Object
readonly
Returns the value of attribute lr_goto.
-
#lr_productions ⇒ Object
readonly
Returns the value of attribute lr_productions.
Instance Method Summary collapse
-
#initialize(grammar, method = :LALR) ⇒ LRTable
constructor
A new instance of LRTable.
- #parse_table(log = PlyDump.stub) ⇒ Object
Constructor Details
#initialize(grammar, method = :LALR) ⇒ LRTable
Returns a new instance of LRTable.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rly/parse/lr_table.rb', line 10 def initialize(grammar, method=:LALR) raise ArgumentError unless [:LALR, :SLR].include?(method) @grammar = grammar @lr_method = method @lr_action = {} @lr_goto = {} @lr_productions = grammar.productions @lr_goto_cache = {} @lr0_cidhash = {} @add_count = 0 @sr_conflict = 0 @rr_conflict = 0 @conflicts = [] @sr_conflicts = [] @rr_conflicts = [] grammar.build_lritems grammar.compute_first grammar.compute_follow end |
Instance Attribute Details
#lr_action ⇒ Object (readonly)
Returns the value of attribute lr_action.
8 9 10 |
# File 'lib/rly/parse/lr_table.rb', line 8 def lr_action @lr_action end |
#lr_goto ⇒ Object (readonly)
Returns the value of attribute lr_goto.
8 9 10 |
# File 'lib/rly/parse/lr_table.rb', line 8 def lr_goto @lr_goto end |
#lr_productions ⇒ Object (readonly)
Returns the value of attribute lr_productions.
8 9 10 |
# File 'lib/rly/parse/lr_table.rb', line 8 def lr_productions @lr_productions end |
Instance Method Details
#parse_table(log = PlyDump.stub) ⇒ Object
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/rly/parse/lr_table.rb', line 36 def parse_table(log=PlyDump.stub) productions = @grammar.productions precedence = @grammar.precedence actionp = {} log.info("Parsing method: %s", @lr_method) c = lr0_items add_lalr_lookaheads(c) if @lr_method == :LALR # Build the parser table, state by state st = 0 c.each do |i| # Loop over each production in I actlist = [] # List of actions st_action = {} st_actionp = {} st_goto = {} log.info("") log.info("state %d", st) log.info("") i.each { |p| log.info(" (%d) %s", p.index, p.to_s) } log.info("") i.each do |p| if p.length == p.lr_index + 1 if p.name == :"S'" # Start symbol. Accept! st_action[:"$end"] = 0 st_actionp[:"$end"] = p else # We are at the end of a production. Reduce! if @lr_method == :LALR laheads = p.lookaheads[st] else laheads = @grammar.follow[p.name] end laheads.each do |a| actlist << [a, p, sprintf("reduce using rule %d (%s)", p.index, p)] r = st_action[a] if r # Whoa. Have a shift/reduce or reduce/reduce conflict if r > 0 # Need to decide on shift or reduce here # By default we favor shifting. Need to add # some precedence rules here. sprec, slevel = productions[st_actionp[a].index].precedence rprec, rlevel = precedence[a] || [:right, 0] if (slevel < rlevel) || ((slevel == rlevel) && (rprec == :left)) # We really need to reduce here. st_action[a] = -p.index st_actionp[a] = p if ! slevel && ! rlevel log.info(" ! shift/reduce conflict for %s resolved as reduce",a) @sr_conflicts << [st, a, 'reduce'] end productions[p.index].reduced += 1 elsif (slevel == rlevel) && (rprec == :nonassoc) st_action[a] = nil else # Hmmm. Guess we'll keep the shift unless rlevel log.info(" ! shift/reduce conflict for %s resolved as shift",a) @sr_conflicts << [st,a,'shift'] end end elsif r < 0 # Reduce/reduce conflict. In this case, we favor the rule # that was defined first in the grammar file oldp = productions[-r] pp = productions[p.index] if oldp.line > pp.line st_action[a] = -p.index st_actionp[a] = p chosenp = pp rejectp = oldp productions[p.index].reduced += 1 productions[oldp.index].reduced -= 1 else chosenp,rejectp = oldp,pp end @rr_conflicts << [st, chosenp, rejectp] log.info(" ! reduce/reduce conflict for %s resolved using rule %d (%s)", a, st_actionp[a].index, st_actionp[a]) else raise RuntimeError("Unknown conflict in state #{st}") end else st_action[a] = -p.index st_actionp[a] = p productions[p.index].reduced += 1 end end end else a = p.prod[p.lr_index+1] # Get symbol right after the "." if @grammar.terminals.include?(a) g = lr0_goto(i, a) j = @lr0_cidhash[g.hash] || -1 if j >= 0 # We are in a shift state actlist << [a, p, sprintf("shift and go to state %d", j)] r = st_action[a] if r # Whoa have a shift/reduce or shift/shift conflict if r > 0 if r != j raise RuntimeError("Shift/shift conflict in state #{st}") end elsif r < 0 # Do a precedence check. # - if precedence of reduce rule is higher, we reduce. # - if precedence of reduce is same and left assoc, we reduce. # - otherwise we shift rprec, rlevel = productions[st_actionp[a].index].precedence sprec, slevel = precedence[a] || [:right, 0] if (slevel > rlevel) || ((slevel == rlevel) && (rprec == :right)) # We decide to shift here... highest precedence to shift productions[st_actionp[a].index].reduced -= 1 st_action[a] = j st_actionp[a] = p unless rlevel log.info(" ! shift/reduce conflict for %s resolved as shift",a) @sr_conflicts << [st, a, 'shift'] end elsif (slevel == rlevel) && (rprec == :nonassoc) st_action[a] = nil else # Hmmm. Guess we'll keep the reduce if ! slevel && ! rlevel log.info(" ! shift/reduce conflict for %s resolved as reduce",a) @sr_conflicts << [st, a, 'reduce'] end end else raise RuntimeError("Unknown conflict in state #{st}") end else st_action[a] = j st_actionp[a] = p end end end end end # Print the actions associated with each terminal _actprint = {} actlist.each do |a, p, m| if st_action[a] if p == st_actionp[a] log.info(" %-15s %s",a,m) _actprint[[a,m]] = 1 end end end log.info("") # Print the actions that were not used. (debugging) not_used = false actlist.each do |a, p, m| if st_action[a] unless p == st_actionp[a] unless _actprint[[a,m]] log.debug(" ! %-15s [ %s ]", a, m) not_used = true _actprint[[a,m]] = 1 end end end end log.debug("") if not_used # Construct the goto table for this state nkeys = {} i.each do |ii| ii.usyms.each do |s| nkeys[s] = nil if @grammar.nonterminals.include?(s) end end nkeys.each do |n, _| g = lr0_goto(i, n) j = @lr0_cidhash[g.hash] || -1 if j >= 0 st_goto[n] = j log.info(" %-30s shift and go to state %d",n,j) end end @lr_action[st] = st_action actionp[st] = st_actionp @lr_goto[st] = st_goto st += 1 end end |