jrester / EBNF.cr

Work with (E)BNF and bison/YACC Grammar: Parsing, FIRST/FOLLOW set, CNF, Conversions, LR and LL parsing tables
MIT License
26 stars 3 forks source link

Bison to EBNF conversion doesn't work #1

Closed lvh closed 5 years ago

lvh commented 5 years ago

Minimal reproducer with Dockerfile in: https://github.com/lvh/yacc2bnf

Code:

require "ebnf"

grammar = <<-Grammar
root:
    foo             { puts "foo" }
    | bar           { puts "bar" }

foo:
    A B
    | B B

bar:
    B A
    | A B
Grammar

bison = EBNF::Bison.from grammar
bison.to_bnf
puts bison

Error:

Error in yacc2bnf.cr:18: instantiating 'EBNF::Grammar#to_bnf()'

bison.to_bnf
      ^~~~~~

in lib/ebnf/src/ebnf/to_bnf.cr:78: instantiating 'EBNF::BNF:Module#from_bison(EBNF::Grammar)'

      when Type::Bison then BNF.from_bison dup
                                ^~~~~~~~~~

in lib/ebnf/src/ebnf/to_bnf.cr:6: instantiating 'EBNF::Grammar#each_production()'

      grammar.each_production do |production|
              ^~~~~~~~~~~~~~~

in lib/ebnf/src/ebnf/grammar/grammar.cr:169: instantiating 'Hash(String, EBNF::Production)#each_value()'

      @productions.each_value { |production| yield production }
                   ^~~~~~~~~~

in usr/lib/crystal/core/hash.cr:369: instantiating 'each()'

    each do |key, value|
    ^~~~

in usr/lib/crystal/core/hash.cr:369: instantiating 'each()'

    each do |key, value|
    ^~~~

in lib/ebnf/src/ebnf/grammar/grammar.cr:169: instantiating 'Hash(String, EBNF::Production)#each_value()'

      @productions.each_value { |production| yield production }
                   ^~~~~~~~~~

in lib/ebnf/src/ebnf/to_bnf.cr:6: instantiating 'EBNF::Grammar#each_production()'

      grammar.each_production do |production|
              ^~~~~~~~~~~~~~~

in lib/ebnf/src/ebnf/to_bnf.cr:7: instantiating 'Array(EBNF::Rule)#each_with_index()'

        production.rules.each_with_index do |rule, i|
                         ^~~~~~~~~~~~~~~

in usr/lib/crystal/core/enumerable.cr:377: instantiating 'each()'

    each do |elem|
    ^~~~

in usr/lib/crystal/core/indexable.cr:148: instantiating 'each_index()'

    each_index do |i|
    ^~~~~~~~~~

in usr/lib/crystal/core/indexable.cr:148: instantiating 'each_index()'

    each_index do |i|
    ^~~~~~~~~~

in usr/lib/crystal/core/enumerable.cr:377: instantiating 'each()'

    each do |elem|
    ^~~~

in lib/ebnf/src/ebnf/to_bnf.cr:7: instantiating 'Array(EBNF::Rule)#each_with_index()'

        production.rules.each_with_index do |rule, i|
                         ^~~~~~~~~~~~~~~

in lib/ebnf/src/ebnf/to_bnf.cr:9: wrong number of arguments for 'EBNF::Production#[]=' (given 2, expected 1)
Overloads are:
 - EBNF::Production#[]=(arg)

            production[i] = Rule.new bison_rule.atoms
jrester commented 5 years ago

Sorry, could you provide more information? I am not able to reproduce the issue.

lvh commented 5 years ago

Looks like I neglected to push to the repo. I fixed that. There's a Dockerfile, you should be able to reproduce by attempting to build it.

jrester commented 5 years ago

Okay, thanks. Alpine still uses crystal 0.25.0 which doesn't support the delegation of the #[]= method.

lvh commented 5 years ago

Thanks! That fixed it :)

I'm new to Crystal but I noticed the to_bnf! method is now returning a copy and to_bnf is destructive. Is that intentional? That seems like the opposite of what I'd expect from Ruby.

(Closing this since the issue is fixed.)