1

Ruby で文法用の再帰降下パーサーを作成しようとしていますが、これは次の規則で定義されています。

  1. 入力は、 Stop-wordで始まる空白で区切られたカードで構成されます。ここで、空白は正規表現です/[ \n\t]+/
  2. カードは、カード固有の順序/パターンを持つ、空白で区切られたキーワードまたは/および値で構成されている場合があります
  3. すべてのストップ ワードとキーワードは、大文字と小文字を区別しません。つまり、次のようになります。/^[a-z]+[a-z0-9]*$/i
  4. 値は、二重引用符で囲まれた文字列にすることができます。これは、空白で他の単語と区切られていない場合があります。例:

    word"quoted string"word
    
  5. 値はword /^[a-z]+[a-z0-9]*$/integerfloatのいずれでもかまいません(例: -1.15、 または1.0e+2)

  6. 単一行のコメントは で示され#、他の単語と区切られていない場合があります。例:

    word#single-line comment\n
    
  7. 複数行のコメント/*はandで示され*/、他の単語と区切られていない場合があります。例:

    word/*multi-line 
    comment*/word
    

# Input example. Stop-words are chosen just to highlight them: set, object
set title"Input example"set objects 2#not-separated by white-space. test: "/*
set test "#/*"
object 1 shape box/* shape is a Keyword, 
box is a Value. test: "#*/object 2 shape sphere
set data # message and complete are Values
0 0 0 0 1 18 18 18 1 35 35 35 72 35 35 # all numbers are Values of the Card "set"

ほとんどの単語は空白で区切られているため、しばらくの間、入力全体を分割して単語ごとに解析することを考えていました。コメントと引用に対処するために、私はやろうとしていました

words = input_text.gsub( /([\"\#\n]|\/\*|\*\/)/, ' \1 ' ).split( /[ \t]+/ )

ただし、この方法では、文字列 (および保持したい場合はコメント) の内容が変更されます。これらの厄介なコメントや引用にどのように対処しますか?

4

1 に答える 1

0

わかりました、私はそれを自分で作りました。可読性が必要ない場合は、次のコードを最小限に抑えることができます

class WordParser
  attr_reader :words

  def initialize text
    @text = text
  end

  def parse
    reset_parser
    until eof?
      case curr_char
        when '"' then
          start_word and add_chars_until? '"'
          close_word
        when '#','%' then
          start_word and add_chars_until? "\n"
          close_word
        when '/' then
          if next_is? '*' then
            start_word and 2.times { add_char }
            add_char until curr_is? '*' and next_is? '/' or eof?
            2.times { add_char } unless eof?
            close_word
          else
            # parser_error "unexpected symbol '/'" # if not allowed in the grammar
            start_word unless word_already_started?
            add_char
          end
        when /[^\s]/ then
          start_word unless word_already_started?
          add_char
      else # skip whitespaces etc. between words
        move and close_word
      end
    end
    return @words
  end

private

  def reset_parser
    @position = 0
    @line, @column = 1, 1
    @words = []
    @word_started = false
  end

  def parser_error s
    Kernel.puts 'Parser error on line %d, col %d: ' + s
    raise 'Parser error'
  end

  def word_already_started?
    @word_started
  end

  def close_word
    @word_started = false
  end

  def add_chars_until? ch
    add_char until next_is? ch or eof?
    2.times { add_char } unless eof?
  end

  def add_char
    @words.last[:to] = @position
    # @words.last[:length] += 1
    # @word.last += curr_char # if one just collects words
    move
  end

  def start_word
    @words.push from: @position, to: @position, line: @line, column: @column
    # @words.push '' unless @words.last.empty? # if one just collects words
    @word_started = true
  end

  def move
    increase :@position
    return if eof?
    if prev_is? "\n"
      increase :@line
      reset :@column
    else
      increase :@column
    end
  end

  def reset var; instance_variable_set(var, 1) end
  def increase var; instance_variable_set(var, instance_variable_get(var)+1) end

  def eof?; @position >= @text.length end

  def prev_is? ch; prev_char == ch end
  def curr_is? ch; curr_char == ch end
  def next_is? ch; next_char == ch end

  def prev_char; @text[ @position-1 ] end
  def curr_char; @text[ @position   ] end
  def next_char; @text[ @position+1 ] end
end

質問にある例を使用してテストします

words = WordParser.new(text).parse
p words.collect { |w| text[ w[:from]..w[:to] ] } .to_a

# >> ["# Input example. Stop-words are chosen just to highlight them: set, object\n", 
# >>  "set", "title", "\"Input example\"", "set", "objects", "2", 
# >>  "#not-separated by white-space. test: \"/*\n", "set", "test", "\"#/*\"", 
# >>  "object", "1", "shape", "box", "/* shape is a Keyword, \nbox is a Value. test: \"#*/", 
# >>  "object", "2", "shape", "sphere", "set", "data", "# message and complete are Values\n", 
# >>  "0", "0", "0", "0", "1", "18", "18", "18", "1", "35", "35", "35", "72", 
# >>  "35", "35", "# all numbers are Values of the Card \"set\"\n"]

これで、このようなものを使用して単語をさらに解析できます。

于 2010-08-05T16:12:16.140 に答える