-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Install lrama v0.2.1 and use it for parser generation
- Loading branch information
Showing
20 changed files
with
5,711 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env ruby | ||
|
||
|
||
$LOAD_PATH << File.join(__dir__, "../lib") | ||
require "lrama" | ||
|
||
Lrama::Command.new.run(ARGV.dup) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require "lrama/bitmap" | ||
require "lrama/command" | ||
require "lrama/context" | ||
require "lrama/digraph" | ||
require "lrama/grammar" | ||
require "lrama/lexer" | ||
require "lrama/output" | ||
require "lrama/parser" | ||
require "lrama/report" | ||
require "lrama/states" | ||
require "lrama/states_reporter" | ||
require "lrama/version" | ||
require "lrama/warning" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Lrama | ||
module Bitmap | ||
def self.from_array(ary) | ||
bit = 0 | ||
|
||
ary.each do |int| | ||
bit |= (1 << int) | ||
end | ||
|
||
bit | ||
end | ||
|
||
def self.to_array(int) | ||
a = [] | ||
i = 0 | ||
|
||
while int > 0 do | ||
if int & 1 == 1 | ||
a << i | ||
end | ||
|
||
i += 1 | ||
int >>= 1 | ||
end | ||
|
||
a | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
require 'optparse' | ||
|
||
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 | ||
|
||
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 | ||
|
||
if !grammar_file | ||
puts "File should be specified\n" | ||
exit 1 | ||
end | ||
|
||
Report::Duration.enable if trace_opts[:time] | ||
|
||
warning = Lrama::Warning.new | ||
y = File.read(grammar_file) | ||
grammar = Lrama::Parser.new(y).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 | ||
reporter = Lrama::StatesReporter.new(states) | ||
File.open(report_file, "w+") do |f| | ||
reporter.report(f, **report_opts) | ||
end | ||
end | ||
|
||
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, | ||
context: context, | ||
grammar: grammar, | ||
).render | ||
end | ||
|
||
if warning.has_error? | ||
exit 1 | ||
end | ||
end | ||
|
||
private | ||
|
||
def validate_report(report) | ||
bison_list = %w[states itemsets lookaheads solved counterexamples cex all none] | ||
others = %w[verbose] | ||
list = bison_list + others | ||
not_supported = %w[counterexamples cex none] | ||
h = { grammar: true } | ||
|
||
report.each do |r| | ||
if list.include?(r) && !not_supported.include?(r) | ||
h[r.to_sym] = true | ||
else | ||
raise "Invalid report option \"#{r}\"." | ||
end | ||
end | ||
|
||
if h[:all] | ||
(bison_list - not_supported).each do |r| | ||
h[r.to_sym] = true | ||
end | ||
|
||
h.delete(:all) | ||
end | ||
|
||
return h | ||
end | ||
|
||
def validate_trace(trace) | ||
list = %w[ | ||
none locations scan parse automaton bitsets | ||
closure grammar resource sets muscles tools | ||
m4-early m4 skeleton time ielr cex all | ||
] | ||
h = {} | ||
|
||
trace.each do |t| | ||
if list.include?(t) | ||
h[t.to_sym] = true | ||
else | ||
raise "Invalid trace option \"#{t}\"." | ||
end | ||
end | ||
|
||
return h | ||
end | ||
end | ||
end |
Oops, something went wrong.