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

Terminal can only be in first term #182

Open
arkansas-coder opened this issue Feb 6, 2022 · 0 comments
Open

Terminal can only be in first term #182

arkansas-coder opened this issue Feb 6, 2022 · 0 comments

Comments

@arkansas-coder
Copy link

If a terminal is specified in other than the first term, racc generates an error. For example, this will not generate a Ruby file

class Parse
rule
  target    : a
                { p 'target:a' }
  a         : :TERM1 # first term _can_ be a terminal
                { p 'a:TERM1' }
            | :TERM2 # other terms _cannot_ begin with terminals
                { p 'a:TERM2'}
end

If the term does not begin with a terminal but contains a terminal, the Ruby file will be generated but will generate an error when executed. For example, this will generate a Ruby file

class Parse
rule
  target      : a
                  { p 'target:a' }
  a           : :TERM1 # first term _can_ be terminal
                  { p 'a:TERM1' }
              | term3_sym
                  { p 'a:term3_sym' }
              | term3_sym term1_sym
                  { p 'a:term3_sym, a:term1_sym'}
              | term3_sym :TERM2 # other terms _cannot_ contain terminals
                  { p 'a:term3_sym, a:TERM2'}
  term1_sym   : :TERM1
                  { p 'term1_sym:TERM1' }
  term3_sym   : :TERM3
                  { p 'term3_sym:TERM3' }
end

but this test will fail

require_relative '../lib/racc_fail'

p 'Test Fail'
parser = Parse.new
tests = [
  [[:TERM1, 'term1']],
  [[:TERM3, 'term3']],
  [[:TERM3, 'term3'], [:TERM1, 'term1']],
  [[:TERM3, 'term3'], [:TERM2, 'term2']] # fails
]
tests.each do |test|
  puts
  p "test input = #{test}"
  p "result = #{parser.parse(test)}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant