Lrama v0.5.5

This commit is contained in:
yui-knk 2023-08-28 22:04:10 +09:00 committed by Yuichiro Kaneko
parent fd0df1f8c6
commit c02f978fd5
Notes: git 2023-08-28 23:26:40 +00:00
14 changed files with 33 additions and 41 deletions

View File

@ -1,6 +1,5 @@
#!/usr/bin/env ruby
$LOAD_PATH << File.join(__dir__, "../lib")
require "lrama"

View File

@ -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 }

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -50,7 +50,6 @@ module Lrama
end
alias :translated_error_token_code :translated_printer_code
private
# * ($1) yyvsp[i]

View File

@ -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

View File

@ -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>

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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|
@ -207,8 +202,8 @@ module Lrama
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

View File

@ -1,3 +1,3 @@
module Lrama
VERSION = "0.5.4".freeze
VERSION = "0.5.5".freeze
end