Class: Depager::LALR::Table
- Inherits:
-
Object
- Object
- Depager::LALR::Table
- Defined in:
- lib/depager/lr.rb,
lib/depager/lr.rb
Instance Attribute Summary collapse
-
#action_table ⇒ Object
Returns the value of attribute action_table.
-
#defred_after_shift_table ⇒ Object
Returns the value of attribute defred_after_shift_table.
-
#defred_table ⇒ Object
Returns the value of attribute defred_table.
-
#goto_table ⇒ Object
Returns the value of attribute goto_table.
-
#grammar ⇒ Object
Returns the value of attribute grammar.
-
#state_info ⇒ Object
readonly
Returns the value of attribute state_info.
-
#states ⇒ Object
Returns the value of attribute states.
Instance Method Summary collapse
- #check_table(dp) ⇒ Object
- #checkconf(newv, oldv, key, g = nil) ⇒ Object
- #gen_state_info ⇒ Object
-
#initialize(grammar) ⇒ Table
constructor
A new instance of Table.
- #items ⇒ Object
- #mkset ⇒ Object
- #mktable ⇒ Object
-
#resolveconf(sh, rd) ⇒ Object
-1:reduce sh; 1:shift rd(=key); 0:can’t resolve.
- #verbose(dp) ⇒ Object
Constructor Details
#initialize(grammar) ⇒ Table
Returns a new instance of Table.
256 257 258 259 260 261 262 263 264 |
# File 'lib/depager/lr.rb', line 256 def initialize(grammar) @grammar = grammar @states = nil @warning_list = [] items mkset mktable end |
Instance Attribute Details
#action_table ⇒ Object
Returns the value of attribute action_table.
254 255 256 |
# File 'lib/depager/lr.rb', line 254 def action_table @action_table end |
#defred_after_shift_table ⇒ Object
Returns the value of attribute defred_after_shift_table.
254 255 256 |
# File 'lib/depager/lr.rb', line 254 def defred_after_shift_table @defred_after_shift_table end |
#defred_table ⇒ Object
Returns the value of attribute defred_table.
254 255 256 |
# File 'lib/depager/lr.rb', line 254 def defred_table @defred_table end |
#goto_table ⇒ Object
Returns the value of attribute goto_table.
254 255 256 |
# File 'lib/depager/lr.rb', line 254 def goto_table @goto_table end |
#grammar ⇒ Object
Returns the value of attribute grammar.
254 255 256 |
# File 'lib/depager/lr.rb', line 254 def grammar @grammar end |
#state_info ⇒ Object (readonly)
Returns the value of attribute state_info.
475 476 477 |
# File 'lib/depager/lr.rb', line 475 def state_info @state_info end |
#states ⇒ Object
Returns the value of attribute states.
254 255 256 |
# File 'lib/depager/lr.rb', line 254 def states @states end |
Instance Method Details
#check_table(dp) ⇒ Object
434 435 436 437 438 439 440 441 |
# File 'lib/depager/lr.rb', line 434 def check_table(dp) @warning_list.each { |i| dp.warning i } return unless Depager.debug_mode? gen_state_info verbose dp if Depager.verbose_mode? end |
#checkconf(newv, oldv, key, g = nil) ⇒ Object
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/depager/lr.rb', line 314 def checkconf(newv, oldv, key, g = nil) return newv unless oldv rl = grammar.rulelist syms = grammar.syms if oldv == "ACC" @warning_list << "'ACC' conflict #{g}." return oldv end # warn "\n-- N:#{newv} O:#{oldv} K:#{grammar.syms[key]} " if (newv < 0) && (oldv > 0) # warn "-:shift #{grammar.syms[key]} go to #{oldv}" r = resolveconf(key, -newv) if r > 0 # warn "+:shift #{grammar.syms[key]} go to #{oldv}" oldv elsif r < 0 # warn "+:reduce #{grammar[-newv]}" newv else @warning_list << ("shift/reduce conflict #{syms[key]}.\n #{g}\n " \ "shift : #{syms[key]}\n " \ "reduce: #{rl[-newv]}") oldv end elsif (newv > 0) && (oldv < 0) r = resolveconf(key, -oldv) if r > 0 # warn "shift" newv elsif r < 0 # warn "reduce" oldv else @warning_list << ("shift/reduce conflict #{syms[key]}.\n #{g}\n " \ "shift : #{syms[key]}\n " \ "reduce: #{rl[-oldv]}") newv end elsif (newv < 0) && (oldv < 0) unless newv == oldv @warning_list << ("reduce/reduce conflict #{syms[key]}.\n #{g}\n " \ "reduce: #{rl[-newv]}\n " \ "reduce: #{rl[-oldv]}") newv end else warn "must not happen" newv end end |
#gen_state_info ⇒ Object
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'lib/depager/lr.rb', line 477 def gen_state_info g = grammar @state_info = [] @action_table.each_with_index do |s, x| shi = "" red = "" acc = "" s.each_with_index do |t, y| next unless t hd = format(" %-15s", g.symname(y + g.nonterms.size)) if t == "ACC" acc = "#{hd} accept" elsif t < 0 red << "#{hd} reduce using rule #{-t} (#{g.symname g[-t].lhs})\n" else shi << "#{hd} shift, and goto to state #{t}\n" end end if (t = @defred_table[x]) as = @defred_after_shift_table[x] ? " [after shift]" : "" red << format(" %-15s", "$default") << "reduce using rule #{-t} (#{g.symname g[-t].lhs}) #{as}" end @state_info << (@states[x].to_s << "\n\n#{shi}\n#{red}\n#{acc}").strip end end |
#items ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/depager/lr.rb', line 266 def items warn "** LR(0) **" if Depager.debug_mode? n = 0 m = 0 states = [State.new([LRItem[grammar[0], 0]], 0)] memo = { states[0] => m } while n < states.size states[n].mkgoto.each do |x, a| unless a.empty? if (memo_a = memo[a]) states[n].gg[x] = states[memo_a] else m += 1 a.n = m states[m] = a memo[a] = m end end end n += 1 end @states = states end |
#mkset ⇒ Object
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'lib/depager/lr.rb', line 443 def mkset warn "** LA **" if Depager.debug_mode? trn = [] @states.each do |state| state.items.each do |sitem| sitem.closure1([TLA]).each do |citem| next if citem.n == citem.rule.rhs.size gi = state.goto(citem.dotsym).items i = gi.find { |j| j.rule == citem.rule && j.n == citem.n + 1 } trn << [sitem, i] if citem.la.include? TLA i.la.merge!(citem.la).delete(TLA) end end end warn "** LALR(1) **" if Depager.debug_mode? @states[0].items[0].la = @grammar.symset([grammar.nonterms.size]) # '$' begin changed = false trn.each do |k, v| unless k.la.subset_of?(v.la) v.la.merge!(k.la) changed end end end while changed end |
#mktable ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/depager/lr.rb', line 369 def mktable warn "** TABLE **" if Depager.debug_mode? taction = (0...@states.size).map { Array.new(grammar.syms.size - grammar.nonterms.size) } tgoto = (0...@states.size).map { Array.new(grammar.nonterms.size - 1) } @states.each_with_index do |c, i| c.gg.each do |k, j| next unless k if grammar.term? k key = k - grammar.nonterms.size taction[i][key] = checkconf(j.n, taction[i][key], k, c) else tgoto[i][k - 1] = j.n end end c.items.each do |j| if (j.n == j.rule.rhs.size) && (j.rule.lhs != 0) # $start j.la.each do |u| key = u - grammar.nonterms.size taction[i][key] = checkconf(-j.rule.n, taction[i][key], u, c) end elsif (efs = grammar.f0e[j.dotsym]) efs.each do |rn, ef| ef.each do |es| fst = grammar.symset j.la.each do |la| fst |= grammar.first [es, *j.dotrest].push(la) end fst.each do |u| key = u - grammar.nonterms.size taction[i][key] = checkconf(-rn, taction[i][key], u, c) end end end end taction[i][0] = "ACC" if j.rule == grammar[0] && j.n == 1 # $start : ... end end @defred_table = [] taction.each_with_index do |l, x| rs = l.select { |i| i.is_a? Integer }.uniq.compact rs = rs.select { |i| i < 0 } @defred_table[x] = (rs[0] if rs.size == 1) end @defred_after_shift_table = [] taction.each_with_index do |l, x| rs = l.select { |i| i.is_a? Integer }.uniq.compact @defred_after_shift_table[x] = (rs[0] if (rs.size == 1) && (rs[0] < 0)) end taction.each_with_index do |l, x| # rubocop:disable Style/CombinableLoops next unless @defred_table[x] l.size.times do |i| l[i] = nil if l[i] && l[i] < 0 end end @action_table = taction @goto_table = tgoto end |
#resolveconf(sh, rd) ⇒ Object
-1:reduce sh; 1:shift rd(=key); 0:can’t resolve
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/depager/lr.rb', line 292 def resolveconf(sh, rd) precs = grammar.precs psh = precs[sh] mrt = grammar[rd].rhs.reverse.find { |i| grammar.term?(i) } prd = grammar[rd].prec || precs[mrt] if psh && prd if psh[1] == prd[1] # warn :LEFT ? 'reduce' : 'shift' psh[0] == :LEFT ? -1 : 1 elsif psh[1] > prd[1] # warn 'reduce' -1 else # warn 'shift' 1 end else 0 end end |
#verbose(dp) ⇒ Object
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 |
# File 'lib/depager/lr.rb', line 506 def verbose(dp) g = grammar output = [] if Depager.debug_mode?(:f) output << "** FIRST1 **" str = g.first1.map do |k, v| "#{g.symname k} => #{v.map { |i| g.symname i }.join(' ')}" end.join("\n") output << "#{str}\n\n" end if Depager.debug_mode?(:e) output << "** Empty Reduction **" str = g.f0e.map do |k, v| "#{g.symname k} =>\n" << v.map do |n, f| " #{rulelist[n]} ? #{f.map { |i| g.symname i }.join(' ')}" end.join("\n") end.join("\n") output << "#{str}\n\n" end if Depager.debug_mode?(:s) output << "** SYMBOLS **" str = g.syms.map { |k, _| "#{format('%03i', k)} #{g.symname k}" }.sort.join("\n") output << "#{str}\n\n" end if Depager.debug_mode?(:g) output << "*** Grammar ***" str = g.rulelist.join("\n") output << "#{str}\n\n" end if Depager.debug_mode?(:c) output << "*** States ***" str = @state_info.join("\n\n").strip output << "#{str}\n\n" end nssize = g.nonterms.size if Depager.debug_mode?(:t) output << "*** Action Table ***" ws = (nssize...g.syms.size).map do |i| j = g.symname(i).size [j, 6].max end str = " |" (nssize...g.syms.size).each_with_index do |i, x| str << format("%0#{ws[x]}s|", g.symname(i)) end; str << " $default|\n" @action_table.each_with_index do |i, x| str << format("%03i|", x) i.each_with_index do |j, y| str << format("%0#{ws[y]}s|", j) end str << (format("%04s,%04s|\n", @defred_table[x], @defred_after_shift_table[x])) end output << "#{str}\n\n" output << "*** Goto Table ***" ws = (1...nssize).map do |i| j = g.symname(i).size [j, 6].max end str = " |" (1...nssize).each_with_index do |i, x| str << format("%0#{ws[x]}s|", g.symname(i)) end; str << "\n" @goto_table.each_with_index do |i, x| str << format("%03i|", x) i.each_with_index do |j, y| str << format("%0#{ws[y]}s|", j) end str << "\n" end output << "#{str}\n\n" end File.write("#{File.basename(dp.file.path, '.dr')}.output", output.join("\n")) end |