Skip to content

Commit

Permalink
Lrama v0.5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
yui-knk committed Aug 5, 2023
1 parent e80752f commit f07ef1d
Show file tree
Hide file tree
Showing 29 changed files with 774 additions and 561 deletions.
17 changes: 1 addition & 16 deletions tool/lrama/LEGAL.md
Expand Up @@ -5,22 +5,7 @@ mentioned below.

## GNU General Public License version 3

These files are licensed under the GNU General Public License version 3. See these files for more information.
These files are licensed under the GNU General Public License version 3 or later. See these files for more information.

* template/bison/yacc.c
* template/bison/yacc.h

## Same with Ruby

These files are licensed same with Ruby. See https://github.com/ruby/ruby/blob/master/COPYING for more information.

* spec/fixtures/integration/ruby_3_0_5/parse.tmp.y
* spec/fixtures/integration/ruby_3_0_5/y.tab.c
* spec/fixtures/integration/ruby_3_0_5/y.tab.h
* spec/fixtures/integration/ruby_3_1_0/parse.tmp.y
* spec/fixtures/integration/ruby_3_1_0/y.tab.c
* spec/fixtures/integration/ruby_3_1_0/y.tab.h
* spec/fixtures/integration/ruby_3_2_0/parse.tmp.y
* spec/fixtures/integration/ruby_3_2_0/y.tab.c
* spec/fixtures/integration/ruby_3_2_0/y.tab.h

14 changes: 14 additions & 0 deletions tool/lrama/exe/lex_state
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby


$LOAD_PATH << File.join(__dir__, "../lib")
require "lrama"

grammar_file = ARGV.shift
y = File.read(grammar_file)
warning = Lrama::Warning.new
grammar = Lrama::Parser.new(y).parse
states = Lrama::States.new(grammar, warning)
states.compute

Lrama::LexState.new(states).compute
2 changes: 1 addition & 1 deletion tool/lrama/exe/lrama
Expand Up @@ -4,4 +4,4 @@
$LOAD_PATH << File.join(__dir__, "../lib")
require "lrama"

Lrama::Command.new.run(ARGV.dup)
Lrama::Command.new(ARGV.dup).run
161 changes: 90 additions & 71 deletions tool/lrama/lib/lrama/command.rb
Expand Up @@ -2,94 +2,57 @@

module Lrama
class Command
def run(argv)
opt = OptionParser.new

# opt.on('-h') {|v| p v }
opt.on('-V', '--version') {|v| puts Lrama::VERSION ; exit 0 }

# Tuning the Parser
skeleton = "bison/yacc.c"

opt.on('-S', '--skeleton=FILE') {|v| skeleton = v }
opt.on('-t') { } # Do nothing

# Output Files:
header = false
header_file = nil
report = []
report_file = nil
outfile = "y.tab.c"

opt.on('-h', '--header=[FILE]') {|v| header = true; header_file = v }
opt.on('-d') { header = true }
opt.on('-r', '--report=THINGS') {|v| report = v.split(',') }
opt.on('--report-file=FILE') {|v| report_file = v }
opt.on('-v') { } # Do nothing
opt.on('-o', '--output=FILE') {|v| outfile = v }

# Hidden
trace = []
opt.on('--trace=THINGS') {|v| trace = v.split(',') }

# Error Recovery
error_recovery = false
opt.on('-e') {|v| error_recovery = true }

opt.parse!(argv)

trace_opts = validate_trace(trace)
report_opts = validate_report(report)

grammar_file = argv.shift

if !report.empty? && report_file.nil? && grammar_file
report_file = File.dirname(grammar_file) + "/" + File.basename(grammar_file, ".*") + ".output"
end
def initialize(argv)
@argv = argv

@version = nil
@skeleton = "bison/yacc.c"
@header = false
@header_file = nil
@report = []
@report_file = nil
@outfile = "y.tab.c"
@trace = []
@error_recovery = false
@grammar_file = nil
@report_file = nil
@trace_opts = nil
@report_opts = nil
end

if !header_file && header
case
when outfile
header_file = File.dirname(outfile) + "/" + File.basename(outfile, ".*") + ".h"
when grammar_file
header_file = File.dirname(grammar_file) + "/" + File.basename(grammar_file, ".*") + ".h"
end
end
def run
parse_option

if !grammar_file
abort "File should be specified\n"
if @version
puts Lrama::VERSION
exit 0
end

Report::Duration.enable if trace_opts[:time]
Report::Duration.enable if @trace_opts[:time]

warning = Lrama::Warning.new
if grammar_file == '-'
grammar_file = argv.shift or abort "File name for STDIN should be specified\n"
y = STDIN.read
else
y = File.read(grammar_file)
end
grammar = Lrama::Parser.new(y).parse
states = Lrama::States.new(grammar, warning, trace_state: (trace_opts[:automaton] || trace_opts[:closure]))
grammar = Lrama::Parser.new(@y.read).parse
states = Lrama::States.new(grammar, warning, trace_state: (@trace_opts[:automaton] || @trace_opts[:closure]))
states.compute
context = Lrama::Context.new(states)

if report_file
if @report_file
reporter = Lrama::StatesReporter.new(states)
File.open(report_file, "w+") do |f|
reporter.report(f, **report_opts)
File.open(@report_file, "w+") do |f|
reporter.report(f, **@report_opts)
end
end

File.open(outfile, "w+") do |f|
File.open(@outfile, "w+") do |f|
Lrama::Output.new(
out: f,
output_file_path: outfile,
template_name: skeleton,
grammar_file_path: grammar_file,
header_file_path: header_file,
output_file_path: @outfile,
template_name: @skeleton,
grammar_file_path: @grammar_file,
header_file_path: @header_file,
context: context,
grammar: grammar,
error_recovery: @error_recovery,
).render
end

Expand Down Expand Up @@ -144,5 +107,61 @@ def validate_trace(trace)

return h
end

def parse_option
opt = OptionParser.new

# opt.on('-h') {|v| p v }
opt.on('-V', '--version') {|v| @version = true }

# Tuning the Parser
opt.on('-S', '--skeleton=FILE') {|v| @skeleton = v }
opt.on('-t') { } # Do nothing

# Output Files:
opt.on('-h', '--header=[FILE]') {|v| @header = true; @header_file = v }
opt.on('-d') { @header = true }
opt.on('-r', '--report=THINGS') {|v| @report = v.split(',') }
opt.on('--report-file=FILE') {|v| @report_file = v }
opt.on('-v') { } # Do nothing
opt.on('-o', '--output=FILE') {|v| @outfile = v }

# Hidden
opt.on('--trace=THINGS') {|v| @trace = v.split(',') }

# Error Recovery
opt.on('-e') {|v| @error_recovery = true }

opt.parse!(@argv)

@trace_opts = validate_trace(@trace)
@report_opts = validate_report(@report)

@grammar_file = @argv.shift

if !@grammar_file
abort "File should be specified\n"
end

if @grammar_file == '-'
@grammar_file = @argv.shift or abort "File name for STDIN should be specified\n"
@y = STDIN
else
@y = File.open(@grammar_file, 'r')
end

if !@report.empty? && @report_file.nil? && @grammar_file
@report_file = File.dirname(@grammar_file) + "/" + File.basename(@grammar_file, ".*") + ".output"
end

if !@header_file && @header
case
when @outfile
@header_file = File.dirname(@outfile) + "/" + File.basename(@outfile, ".*") + ".h"
when @grammar_file
@header_file = File.dirname(@grammar_file) + "/" + File.basename(@grammar_file, ".*") + ".h"
end
end
end
end
end
12 changes: 11 additions & 1 deletion tool/lrama/lib/lrama/context.rb
@@ -1,4 +1,4 @@
require "lrama/report"
require "lrama/report/duration"

module Lrama
# This is passed to a template
Expand Down Expand Up @@ -89,6 +89,16 @@ def yytranslate
return a
end

def yytranslate_inverted
a = Array.new(@states.symbols.count, @states.undef_symbol.token_id)

@states.terms.each do |term|
a[term.number] = term.token_id
end

return a
end

# Mapping from rule number to line number of the rule is defined.
# Dummy rule is appended as the first element whose value is 0
# because 0 means error in yydefact.
Expand Down

0 comments on commit f07ef1d

Please sign in to comment.