Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce the --trace=actions option #396

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/lrama/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def run(argv)
puts grammar.rules
end

if options.trace_opts && options.trace_opts[:actions]
puts "Grammar rules with actions:"
grammar.rules.each { |rule| puts rule.with_actions }
end

File.open(options.outfile, "w+") do |f|
Lrama::Output.new(
out: f,
Expand Down
4 changes: 4 additions & 0 deletions lib/lrama/grammar/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def as_comment
"#{l}: #{r}"
end

def with_actions
"#{to_s} {#{token_code&.s_value}}"
end

# opt_nl: ε <-- empty_rule
# | '\n' <-- not empty_rule
def empty_rule?
Expand Down
5 changes: 3 additions & 2 deletions lib/lrama/option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ def validate_report(report)

VALID_TRACES = %w[
none locations scan parse automaton bitsets
closure grammar rules resource sets muscles tools
m4-early m4 skeleton time ielr cex all
closure grammar rules actions resource
sets muscles tools m4-early m4 skeleton time
ielr cex all
]

def validate_trace(trace)
Expand Down
19 changes: 19 additions & 0 deletions spec/lrama/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@
end
end

context "when `--trace=actions` option specified" do
it "print grammar rules with actions" do
command = Lrama::Command.new
expect { command.run(o_option + [fixture_path("command/basic.y"), "--trace=actions"]) }.to output(<<~'OUTPUT').to_stdout
Grammar rules with actions:
$accept -> list, YYEOF {}
list -> ε {}
list -> list, LF {}
list -> list, expr, LF { printf("=> %d\n", $2); }
expr -> NUM {}
expr -> expr, '+', expr { $$ = $1 + $3; }
expr -> expr, '-', expr { $$ = $1 - $3; }
expr -> expr, '*', expr { $$ = $1 * $3; }
expr -> expr, '/', expr { $$ = $1 / $3; }
expr -> '(', expr, ')' { $$ = $2; }
OUTPUT
end
end

context "when `--report-file` option specified" do
it "create report file" do
allow(File).to receive(:open).and_call_original
Expand Down
2 changes: 1 addition & 1 deletion spec/lrama/option_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
states itemsets lookaheads solved counterexamples all verbose

Valid Traces:
none locations scan parse automaton bitsets closure grammar rules resource sets muscles tools m4-early m4 skeleton time ielr cex all
none locations scan parse automaton bitsets closure grammar rules actions resource sets muscles tools m4-early m4 skeleton time ielr cex all

HELP
end
Expand Down