racc icon indicating copy to clipboard operation
racc copied to clipboard

Terminal can only be in first term

Open arkansas-coder opened this issue 3 years ago • 0 comments

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

arkansas-coder avatar Feb 06 '22 23:02 arkansas-coder