summaryrefslogtreecommitdiff
path: root/tool
diff options
context:
space:
mode:
authoryui-knk <spiketeika@gmail.com>2023-08-28 22:04:10 +0900
committerYuichiro Kaneko <spiketeika@gmail.com>2023-08-29 08:26:18 +0900
commitc02f978fd5653b5ca1959b6547c26a834ee1f043 (patch)
tree663dd1c3817cfcb9b43cf73034c0c12825d5b742 /tool
parentfd0df1f8c6845671c86eddd85c9bcced30501690 (diff)
Lrama v0.5.5
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/8308
Diffstat (limited to 'tool')
-rwxr-xr-xtool/lrama/exe/lrama1
-rw-r--r--tool/lrama/lib/lrama/command.rb9
-rw-r--r--tool/lrama/lib/lrama/context.rb2
-rw-r--r--tool/lrama/lib/lrama/counterexamples.rb4
-rw-r--r--tool/lrama/lib/lrama/grammar.rb7
-rw-r--r--tool/lrama/lib/lrama/grammar/code.rb1
-rw-r--r--tool/lrama/lib/lrama/lexer.rb7
-rw-r--r--tool/lrama/lib/lrama/lexer/token.rb1
-rw-r--r--tool/lrama/lib/lrama/output.rb2
-rw-r--r--tool/lrama/lib/lrama/parser.rb8
-rw-r--r--tool/lrama/lib/lrama/state.rb1
-rw-r--r--tool/lrama/lib/lrama/states.rb9
-rw-r--r--tool/lrama/lib/lrama/states_reporter.rb20
-rw-r--r--tool/lrama/lib/lrama/version.rb2
14 files changed, 33 insertions, 41 deletions
diff --git a/tool/lrama/exe/lrama b/tool/lrama/exe/lrama
index 7988afe507..5e1ee582cf 100755
--- a/tool/lrama/exe/lrama
+++ b/tool/lrama/exe/lrama
@@ -1,6 +1,5 @@
#!/usr/bin/env ruby
-
$LOAD_PATH << File.join(__dir__, "../lib")
require "lrama"
diff --git a/tool/lrama/lib/lrama/command.rb b/tool/lrama/lib/lrama/command.rb
index 9fceb55fe0..45670ae405 100644
--- a/tool/lrama/lib/lrama/command.rb
+++ b/tool/lrama/lib/lrama/command.rb
@@ -5,7 +5,6 @@ module Lrama
def initialize(argv)
@argv = argv
- @version = nil
@skeleton = "bison/yacc.c"
@header = false
@header_file = nil
@@ -23,15 +22,11 @@ module Lrama
def run
parse_option
- if @version
- puts Lrama::VERSION
- exit 0
- end
-
Report::Duration.enable if @trace_opts[:time]
warning = Lrama::Warning.new
grammar = Lrama::Parser.new(@y.read).parse
+ @y.close if @y != STDIN
states = Lrama::States.new(grammar, warning, trace_state: (@trace_opts[:automaton] || @trace_opts[:closure]))
states.compute
context = Lrama::Context.new(states)
@@ -112,7 +107,7 @@ module Lrama
opt = OptionParser.new
# opt.on('-h') {|v| p v }
- opt.on('-V', '--version') {|v| @version = true }
+ opt.on('-V', '--version') {|v| puts "lrama #{Lrama::VERSION}"; exit 0 }
# Tuning the Parser
opt.on('-S', '--skeleton=FILE') {|v| @skeleton = v }
diff --git a/tool/lrama/lib/lrama/context.rb b/tool/lrama/lib/lrama/context.rb
index 861c4e1137..da70bf1542 100644
--- a/tool/lrama/lib/lrama/context.rb
+++ b/tool/lrama/lib/lrama/context.rb
@@ -401,7 +401,6 @@ module Lrama
end
print sprintf("]\n\n")
-
print sprintf("width [\n")
vectors_count.times do |i|
print sprintf("%d, ", ary[i] ? ary[i][3] : 0)
@@ -409,7 +408,6 @@ module Lrama
end
print sprintf("]\n\n")
-
print sprintf("tally [\n")
vectors_count.times do |i|
print sprintf("%d, ", ary[i] ? ary[i][2] : 0)
diff --git a/tool/lrama/lib/lrama/counterexamples.rb b/tool/lrama/lib/lrama/counterexamples.rb
index a5d62a0c7c..5019257dc3 100644
--- a/tool/lrama/lib/lrama/counterexamples.rb
+++ b/tool/lrama/lib/lrama/counterexamples.rb
@@ -205,7 +205,7 @@ module Lrama
end
def build_paths_from_state_items(state_items)
- paths = state_items.zip([nil] + state_items).map do |si, prev_si|
+ state_items.zip([nil] + state_items).map do |si, prev_si|
case
when prev_si.nil?
StartPath.new(si)
@@ -215,8 +215,6 @@ module Lrama
TransitionPath.new(prev_si, si)
end
end
-
- paths
end
def shortest_path(conflict_state, conflict_reduce_item, conflict_term)
diff --git a/tool/lrama/lib/lrama/grammar.rb b/tool/lrama/lib/lrama/grammar.rb
index 25f1a49170..81df399682 100644
--- a/tool/lrama/lib/lrama/grammar.rb
+++ b/tool/lrama/lib/lrama/grammar.rb
@@ -103,6 +103,10 @@ module Lrama
set_precedence(sym, Precedence.new(type: :right, precedence: precedence))
end
+ def add_precedence(sym, precedence)
+ set_precedence(sym, Precedence.new(type: :precedence, precedence: precedence))
+ end
+
def set_precedence(sym, precedence)
raise "" if sym.nterm?
sym.precedence = precedence
@@ -310,7 +314,6 @@ module Lrama
end || (raise "Nterm not found: #{id}")
end
-
def append_special_symbols
# YYEMPTY (token_id: -2, number: -2) is added when a template is evaluated
# term = add_term(id: Token.new(Token::Ident, "YYEMPTY"), token_id: -2)
@@ -512,7 +515,7 @@ module Lrama
sym.token_id = 11
when "\""
sym.token_id = 34
- when "\'"
+ when "'"
sym.token_id = 39
when "\\\\"
sym.token_id = 92
diff --git a/tool/lrama/lib/lrama/grammar/code.rb b/tool/lrama/lib/lrama/grammar/code.rb
index 0f61ebb58e..712cb1ad5a 100644
--- a/tool/lrama/lib/lrama/grammar/code.rb
+++ b/tool/lrama/lib/lrama/grammar/code.rb
@@ -50,7 +50,6 @@ module Lrama
end
alias :translated_error_token_code :translated_printer_code
-
private
# * ($1) yyvsp[i]
diff --git a/tool/lrama/lib/lrama/lexer.rb b/tool/lrama/lib/lrama/lexer.rb
index c1b5c8fe4e..c591684a05 100644
--- a/tool/lrama/lib/lrama/lexer.rb
+++ b/tool/lrama/lib/lrama/lexer.rb
@@ -30,7 +30,6 @@ module Lrama
@grammar_rules = []
@epilogue = []
- #
@bison_declarations_tokens = []
@grammar_rules_tokens = []
@@ -155,6 +154,8 @@ module Lrama
tokens << create_token(Token::P_left, ss[0], line, ss.pos - column)
when ss.scan(/%right/)
tokens << create_token(Token::P_right, ss[0], line, ss.pos - column)
+ when ss.scan(/%precedence/)
+ tokens << create_token(Token::P_precedence, ss[0], line, ss.pos - column)
when ss.scan(/%prec/)
tokens << create_token(Token::P_prec, ss[0], line, ss.pos - column)
when ss.scan(/{/)
@@ -223,7 +224,7 @@ module Lrama
references << [:dollar, ss[2], tag, str.length, str.length + ss[0].length - 1]
when ss.scan(/@\$/) # @$
references << [:at, "$", nil, str.length, str.length + ss[0].length - 1]
- when ss.scan(/@(\d)+/) # @1
+ when ss.scan(/@(\d+)/) # @1
references << [:at, Integer(ss[1]), nil, str.length, str.length + ss[0].length - 1]
when ss.scan(/{/)
brace_count += 1
@@ -314,8 +315,6 @@ module Lrama
str << ss.getch
next
end
-
- str << ss[0]
end
line # Reach to end of input
diff --git a/tool/lrama/lib/lrama/lexer/token.rb b/tool/lrama/lib/lrama/lexer/token.rb
index aa49bfe47d..4eca6f6007 100644
--- a/tool/lrama/lib/lrama/lexer/token.rb
+++ b/tool/lrama/lib/lrama/lexer/token.rb
@@ -61,6 +61,7 @@ module Lrama
define_type(:P_nonassoc) # %nonassoc
define_type(:P_left) # %left
define_type(:P_right) # %right
+ define_type(:P_precedence) # %precedence
define_type(:P_prec) # %prec
define_type(:User_code) # { ... }
define_type(:Tag) # <int>
diff --git a/tool/lrama/lib/lrama/output.rb b/tool/lrama/lib/lrama/output.rb
index 757d16e25a..2dcdae7b42 100644
--- a/tool/lrama/lib/lrama/output.rb
+++ b/tool/lrama/lib/lrama/output.rb
@@ -252,7 +252,7 @@ module Lrama
end
def extract_param_name(param)
- /\A(\W*)([a-zA-Z0-9_]+)\z/.match(param.split.last)[2]
+ param[/\b([a-zA-Z0-9_]+)(?=\s*\z)/]
end
def parse_param_name
diff --git a/tool/lrama/lib/lrama/parser.rb b/tool/lrama/lib/lrama/parser.rb
index a12c8bbb34..18e2ef033e 100644
--- a/tool/lrama/lib/lrama/parser.rb
+++ b/tool/lrama/lib/lrama/parser.rb
@@ -159,6 +159,14 @@ module Lrama
grammar.add_right(sym, precedence_number)
end
precedence_number += 1
+ when T::P_precedence
+ # %precedence (ident|char|string)+
+ ts.next
+ while (id = ts.consume(T::Ident, T::Char, T::String)) do
+ sym = grammar.add_term(id: id)
+ grammar.add_precedence(sym, precedence_number)
+ end
+ precedence_number += 1
when nil
# end of input
raise "Reach to end of input within declarations"
diff --git a/tool/lrama/lib/lrama/state.rb b/tool/lrama/lib/lrama/state.rb
index 6632bafecc..1b40640215 100644
--- a/tool/lrama/lib/lrama/state.rb
+++ b/tool/lrama/lib/lrama/state.rb
@@ -62,7 +62,6 @@ module Lrama
@items_to_state[items] = next_state
end
- #
def set_look_ahead(rule, look_ahead)
reduce = reduces.find do |r|
r.rule == rule
diff --git a/tool/lrama/lib/lrama/states.rb b/tool/lrama/lib/lrama/states.rb
index d27c5411ea..290e996b82 100644
--- a/tool/lrama/lib/lrama/states.rb
+++ b/tool/lrama/lib/lrama/states.rb
@@ -455,6 +455,11 @@ module Lrama
# shift_prec == reduce_prec, then check associativity
case sym.precedence.type
+ when :precedence
+ # %precedence only specifies precedence and not specify associativity
+ # then a conflict is unresolved if precedence is same.
+ state.conflicts << State::ShiftReduceConflict.new(symbols: [sym], shift: shift, reduce: reduce)
+ next
when :right
# Shift is selected
state.resolved_conflicts << State::ResolvedConflict.new(symbol: sym, reduce: reduce, which: :shift, same_prec: true)
@@ -515,9 +520,9 @@ module Lrama
state.default_reduction_rule = state.reduces.map do |r|
[r.rule, r.rule.id, (r.look_ahead || []).count]
- end.sort_by do |rule, rule_id, count|
+ end.min_by do |rule, rule_id, count|
[-count, rule_id]
- end.first.first
+ end.first
end
end
diff --git a/tool/lrama/lib/lrama/states_reporter.rb b/tool/lrama/lib/lrama/states_reporter.rb
index 0d805829eb..8be0f71e40 100644
--- a/tool/lrama/lib/lrama/states_reporter.rb
+++ b/tool/lrama/lib/lrama/states_reporter.rb
@@ -110,7 +110,6 @@ module Lrama
end
io << "\n"
-
# Report shifts
tmp = state.term_transitions.select do |shift, _|
!shift.not_selected
@@ -123,7 +122,6 @@ module Lrama
end
io << "\n" if !tmp.empty?
-
# Report error caused by %nonassoc
nl = false
tmp = state.resolved_conflicts.select do |resolved|
@@ -138,7 +136,6 @@ module Lrama
end
io << "\n" if !tmp.empty?
-
# Report reduces
nl = false
max_len = state.non_default_reduces.flat_map(&:look_ahead).compact.map(&:display_name).map(&:length).max || 0
@@ -171,7 +168,6 @@ module Lrama
end
io << "\n" if nl
-
# Report nonterminal transitions
tmp = []
max_len = 0
@@ -189,7 +185,6 @@ module Lrama
end
io << "\n" if !tmp.empty?
-
if solved
# Report conflict resolutions
state.resolved_conflicts.each do |resolved|
@@ -202,13 +197,13 @@ module Lrama
# Report counterexamples
examples = cex.compute(state)
examples.each do |example|
- label0 = example.type == :shift_reduce ? "shift/reduce" : "reduce/reduce"
+ label0 = example.type == :shift_reduce ? "shift/reduce" : "reduce/reduce"
label1 = example.type == :shift_reduce ? "Shift derivation" : "First Reduce derivation"
label2 = example.type == :shift_reduce ? "Reduce derivation" : "Second Reduce derivation"
io << " #{label0} conflict on token #{example.conflict_symbol.id.s_value}:\n"
- io << " #{example.path1_item.to_s}\n"
- io << " #{example.path2_item.to_s}\n"
+ io << " #{example.path1_item}\n"
+ io << " #{example.path2_item}\n"
io << " #{label1}\n"
example.derivations1.render_strings_for_report.each do |str|
io << " #{str}\n"
@@ -234,7 +229,6 @@ module Lrama
end
io << "\n"
-
# Report reads_relation
io << " [Reads Relation]\n"
@states.nterms.each do |nterm|
@@ -248,7 +242,6 @@ module Lrama
end
io << "\n"
-
# Report read_sets
io << " [Read sets]\n"
read_sets = @states.read_sets
@@ -263,7 +256,6 @@ module Lrama
end
io << "\n"
-
# Report includes_relation
io << " [Includes Relation]\n"
@states.nterms.each do |nterm|
@@ -277,7 +269,6 @@ module Lrama
end
io << "\n"
-
# Report lookback_relation
io << " [Lookback Relation]\n"
@states.rules.each do |rule|
@@ -286,12 +277,11 @@ module Lrama
a.each do |state_id2, nterm_id2|
n = @states.nterms.find {|n| n.token_id == nterm_id2 }
- io << " (Rule: #{rule.to_s}) -> (State #{state_id2}, #{n.id.s_value})\n"
+ io << " (Rule: #{rule}) -> (State #{state_id2}, #{n.id.s_value})\n"
end
end
io << "\n"
-
# Report follow_sets
io << " [Follow sets]\n"
follow_sets = @states.follow_sets
@@ -306,7 +296,6 @@ module Lrama
end
io << "\n"
-
# Report LA
io << " [Look-Ahead Sets]\n"
tmp = []
@@ -326,7 +315,6 @@ module Lrama
io << "\n" if !tmp.empty?
end
-
# End of Report State
io << "\n"
end
diff --git a/tool/lrama/lib/lrama/version.rb b/tool/lrama/lib/lrama/version.rb
index 3f2eb3b9d4..41215c1aee 100644
--- a/tool/lrama/lib/lrama/version.rb
+++ b/tool/lrama/lib/lrama/version.rb
@@ -1,3 +1,3 @@
module Lrama
- VERSION = "0.5.4".freeze
+ VERSION = "0.5.5".freeze
end