Skip to content

Commit

Permalink
Change yacc.c to automatically add includes and remove --auto-include…
Browse files Browse the repository at this point in the history
… option
  • Loading branch information
ydah committed Oct 17, 2023
1 parent 339c9a2 commit 180dbad
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 74 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,8 @@ $ lrama -d sample/parse.y
```

```shell
# "y.tab.c" and "y.tab.h" are generated and added `#include "y.tab.h"`
$ lrama -d sample/parse.y --auto-include
```

```shell
# "calc", "calc.c", and "calc.h" are generated and added `#include "calc.h"`
$ lrama -d sample/calc.y -o calc.c --auto-include && gcc -Wall calc.c -o calc && ./calc
# "calc", "calc.c", and "calc.h" are generated
$ lrama -d sample/calc.y -o calc.c && gcc -Wall calc.c -o calc && ./calc
Enter the formula:
1
=> 1
Expand Down
2 changes: 1 addition & 1 deletion lib/lrama/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def run(argv)
warning = Lrama::Warning.new
text = options.y.read
options.y.close if options.y != STDIN
grammar = Lrama::NewParser.new(text, options.include_header).parse
grammar = Lrama::NewParser.new(text).parse
states = Lrama::States.new(grammar, warning, trace_state: (options.trace_opts[:automaton] || options.trace_opts[:closure]))
states.compute
context = Lrama::Context.new(states)
Expand Down
13 changes: 2 additions & 11 deletions lib/lrama/new_parser.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions lib/lrama/option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ def parse(argv)
end
end

if @options.auto_include
@options.include_header = @options.header_file.sub("./", "")
end

@options
end

Expand All @@ -66,7 +62,6 @@ def parse_by_option_parser(argv)
o.separator 'Output:'
o.on('-h', '--header=[FILE]', 'also produce a header file named FILE') {|v| @options.header = true; @options.header_file = v }
o.on('-d', 'also produce a header file') { @options.header = true }
o.on('--auto-include', 'also include header files automatically') {|v| @options.auto_include = true }
o.on('-r', '--report=THINGS', Array, 'also produce details on the automaton') {|v| @report = v }
o.on('--report-file=FILE', 'also produce details on the automaton output to a file named FILE') {|v| @options.report_file = v }
o.on('-o', '--output=FILE', 'leave output to FILE') {|v| @options.outfile = v }
Expand Down
6 changes: 2 additions & 4 deletions lib/lrama/options.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
module Lrama
# Command line options.
class Options
attr_accessor :skeleton, :header, :header_file, :auto_include,
:include_header, :report_file, :outfile,
attr_accessor :skeleton, :header, :header_file,
:report_file, :outfile,
:error_recovery, :grammar_file,
:report_file, :trace_opts, :report_opts, :y

def initialize
@skeleton = "bison/yacc.c"
@header = false
@header_file = nil
@auto_include = false
@include_header = nil
@report_file = nil
@outfile = "y.tab.c"
@error_recovery = false
Expand Down
3 changes: 2 additions & 1 deletion lib/lrama/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Output
extend Forwardable
include Report::Duration

attr_reader :grammar_file_path, :context, :grammar, :error_recovery
attr_reader :grammar_file_path, :context, :grammar, :error_recovery, :include_header

def_delegators "@context", :yyfinal, :yylast, :yyntokens, :yynnts, :yynrules, :yynstates,
:yymaxutok, :yypact_ninf, :yytable_ninf
Expand All @@ -28,6 +28,7 @@ def initialize(
@context = context
@grammar = grammar
@error_recovery = error_recovery
@include_header = header_file_path ? header_file_path.sub("./", "") : nil
end

if ERB.instance_method(:initialize).parameters.last.first == :key
Expand Down
12 changes: 2 additions & 10 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ rule
}
"%}"
{
@grammar.prologue = prologue(val[2].s_value)
@grammar.prologue = val[2].s_value
}
| "%require" STRING

Expand Down Expand Up @@ -376,9 +376,8 @@ end

---- inner

def initialize(text, include_header)
def initialize(text)
@text = text
@include_header = include_header
end

def parse
Expand All @@ -397,10 +396,3 @@ def next_token
@lexer.next_token
end

def prologue(s_value)
if @include_header && !s_value.match?(/^\s*#include\s+"#{@include_header}"/)
"\n#include \"#{@include_header}\"\n" + s_value
else
s_value
end
end
6 changes: 3 additions & 3 deletions spec/lrama/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe "basic" do
it do
y = File.read(fixture_path("context/basic.y"))
grammar = Lrama::NewParser.new(y, nil).parse
grammar = Lrama::NewParser.new(y).parse
states = Lrama::States.new(grammar, warning)
states.compute
context = Lrama::Context.new(states)
Expand Down Expand Up @@ -181,7 +181,7 @@
%%
INPUT

grammar = Lrama::NewParser.new(y, nil).parse
grammar = Lrama::NewParser.new(y).parse
states = Lrama::States.new(grammar, warning)
states.compute
context = Lrama::Context.new(states)
Expand Down Expand Up @@ -230,7 +230,7 @@
%%
INPUT

grammar = Lrama::NewParser.new(y, nil).parse
grammar = Lrama::NewParser.new(y).parse
states = Lrama::States.new(grammar, warning)
states.compute
context = Lrama::Context.new(states)
Expand Down
38 changes: 19 additions & 19 deletions spec/lrama/new_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
describe '#parse' do
it "basic" do
y = File.read(fixture_path("common/basic.y"))
grammar = Lrama::NewParser.new(y, nil).parse
grammar = Lrama::NewParser.new(y).parse

expect(grammar.union.code.s_value).to eq(<<-CODE.chomp)
Expand Down Expand Up @@ -408,7 +408,7 @@

it "nullable" do
y = File.read(fixture_path("common/nullable.y"))
grammar = Lrama::NewParser.new(y, nil).parse
grammar = Lrama::NewParser.new(y).parse

expect(grammar.nterms.sort_by(&:number)).to eq([
Sym.new(id: T.new(type: T::Ident, s_value: "$accept"), alias_name: nil, number: 6, tag: nil, term: false, token_id: 0, nullable: false),
Expand Down Expand Up @@ -562,7 +562,7 @@ class : keyword_class tSTRING keyword_end { code 1 }
%%
INPUT

grammar = Lrama::NewParser.new(y, nil).parse
grammar = Lrama::NewParser.new(y).parse

expect(grammar._rules).to eq([
[
Expand Down Expand Up @@ -605,7 +605,7 @@ class : keyword_class tSTRING keyword_end { code 1 }
%%
INPUT
grammar = Lrama::NewParser.new(y, nil).parse
grammar = Lrama::NewParser.new(y).parse

expect(grammar.terms.sort_by(&:number)).to eq([
Sym.new(id: T.new(type: T::Ident, s_value: "EOI"), alias_name: "\"EOI\"", number: 0, tag: nil, term: true, token_id: 0, nullable: false, precedence: nil),
Expand Down Expand Up @@ -661,7 +661,7 @@ class : keyword_class { code 1 } tSTRING { code 2 } keyword_end { code 3 }
%%
INPUT
grammar = Lrama::NewParser.new(y, nil).parse
grammar = Lrama::NewParser.new(y).parse

expect(grammar.nterms.sort_by(&:number)).to eq([
Sym.new(id: T.new(type: T::Ident, s_value: "$accept"), alias_name: nil, number: 11, tag: nil, term: false, token_id: 0, nullable: false),
Expand Down Expand Up @@ -756,7 +756,7 @@ class : keyword_class tSTRING %prec tPLUS keyword_end { code 1 }
%%
INPUT
parser = Lrama::NewParser.new(y, nil)
parser = Lrama::NewParser.new(y)

expect { parser.parse }.to raise_error("Ident after %prec")
end
Expand All @@ -773,7 +773,7 @@ class : keyword_class { code 2 } tSTRING %prec "=" '!' keyword_end { code 3 }
%%
INPUT
parser = Lrama::NewParser.new(y, nil)
parser = Lrama::NewParser.new(y)

expect { parser.parse }.to raise_error("Char after %prec")
end
Expand All @@ -790,7 +790,7 @@ class : keyword_class { code 4 } tSTRING '?' keyword_end %prec tEQ { code 5 } {
%%
INPUT
parser = Lrama::NewParser.new(y, nil)
parser = Lrama::NewParser.new(y)

expect { parser.parse }.to raise_error("Multiple User_code after %prec")
end
Expand All @@ -811,7 +811,7 @@ class : keyword_class
%%
INPUT
grammar = Lrama::NewParser.new(y, nil).parse
grammar = Lrama::NewParser.new(y).parse
codes = grammar.rules.map(&:code).compact

expect(codes.count).to eq(1)
Expand All @@ -838,7 +838,7 @@ class : keyword_class
%%
INPUT
grammar = Lrama::NewParser.new(y, nil).parse
grammar = Lrama::NewParser.new(y).parse
codes = grammar.rules.map(&:code).compact

expect(codes.count).to eq(1)
Expand Down Expand Up @@ -883,7 +883,7 @@ class : keyword_class tSTRING keyword_end { code 1 }
%%
INPUT
grammar = Lrama::NewParser.new(y, nil).parse
grammar = Lrama::NewParser.new(y).parse

expect(grammar.terms.sort_by(&:number)).to eq([
Sym.new(id: T.new(type: T::Ident, s_value: "EOI"), alias_name: "\"EOI\"", number: 0, tag: nil, term: true, token_id: 0, nullable: false),
Expand Down Expand Up @@ -932,7 +932,7 @@ class : keyword_class tSTRING keyword_end { code 1 }
%%
INPUT
grammar = Lrama::NewParser.new(y, nil).parse
grammar = Lrama::NewParser.new(y).parse

expect(grammar.terms.sort_by(&:number)).to eq([
Sym.new(id: T.new(type: T::Ident, s_value: "EOI"), alias_name: "\"EOI\"", number: 0, tag: nil, term: true, token_id: 0, nullable: false, precedence: nil),
Expand Down Expand Up @@ -978,7 +978,7 @@ class : keyword_class tSTRING keyword_end { code 1 }
;
%%
INPUT
grammar = Lrama::NewParser.new(y, nil).parse
grammar = Lrama::NewParser.new(y).parse

expect(grammar.rules).to eq([
Rule.new(
Expand Down Expand Up @@ -1086,7 +1086,7 @@ class : keyword_class tSTRING keyword_end { code 1 }
;
%%
INPUT
grammar = Lrama::NewParser.new(y, nil).parse
grammar = Lrama::NewParser.new(y).parse

expect(grammar.rules).to eq([
Rule.new(
Expand Down Expand Up @@ -1177,7 +1177,7 @@ class : keyword_class tSTRING keyword_end { code 1 }
{ $$ = $1 - $2; }
;
INPUT
grammar = Lrama::NewParser.new(y, nil).parse
grammar = Lrama::NewParser.new(y).parse

expect(grammar.rules).to eq([
Rule.new(
Expand Down Expand Up @@ -1311,7 +1311,7 @@ class : keyword_class tSTRING keyword_end { code 1 }
;
INPUT

expect { Lrama::NewParser.new(y, nil).parse }.to raise_error("'results' is invalid name.")
expect { Lrama::NewParser.new(y).parse }.to raise_error("'results' is invalid name.")
end
end
end
Expand Down Expand Up @@ -1348,7 +1348,7 @@ class : keyword_class tSTRING keyword_end ;
%%
INPUT
grammar = Lrama::NewParser.new(y, nil).parse
grammar = Lrama::NewParser.new(y).parse
terms = grammar.terms.sort_by(&:number).map do |term|
[term.id.s_value, term.token_id]
end
Expand Down Expand Up @@ -1397,7 +1397,7 @@ class : keyword_class tSTRING keyword_end
%%
INPUT
grammar = Lrama::NewParser.new(y, nil).parse
grammar = Lrama::NewParser.new(y).parse
codes = grammar.rules.map(&:code)

expect(codes.count).to eq(3)
Expand Down Expand Up @@ -1444,7 +1444,7 @@ class : keyword_class tSTRING keyword_end
%%
INPUT

expect { Lrama::NewParser.new(y, nil).parse }.to raise_error(RuntimeError) do |e|
expect { Lrama::NewParser.new(y).parse }.to raise_error(RuntimeError) do |e|
expect(e.message).to eq(<<~MSG.chomp)
$$ of 'stmt' has no declared type
$1 of 'stmt' has no declared type
Expand Down
Loading

0 comments on commit 180dbad

Please sign in to comment.