首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby中的解析器:处理粘性注释和引号

Ruby中的解析器:处理粘性注释和引号
EN

Stack Overflow用户
提问于 2010-07-29 13:56:57
回答 1查看 484关注 0票数 1

我试图在Ruby中为语法创建一个递归下降解析器,语法由以下规则定义

  1. Input由以停止字开头的空格分隔的组成,其中空格regex /^[a-z]+[a-z0-9]*$/i
  2. Value可以由关键字或/和组成,后者也由空格分隔,它们具有特定于卡片的order/pattern
  3. All停止词和关键字是不区分大小写的,也就是说:/^[a-z]+[a-z0-9]*$/i
  4. Value可以是一个双引号字符串,可以用空格与其他单词分隔,例如:

引号string"word

  • Value也可以是单词/^[a-z]+[a-z0-9]*$/、整数或浮点数(例如,-1.151.0e+2)

  • Single-line注释由#表示,可以与其他单词分隔,例如:

word#单行comment\n

  • Multi-line注释由/**/表示,不能与其他单词分隔,例如:

word/*多行comment*/word

代码语言:javascript
复制
# 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"

因为大部分单词都是用空格隔开的,所以有一段时间,我一直在考虑把整个输入分开,然后逐字分析。为了处理评论和引语,我打算

代码语言:javascript
复制
words = input_text.gsub( /([\"\#\n]|\/\*|\*\/)/, ' \1 ' ).split( /[ \t]+/ )

但是,以这种方式修改字符串的内容(以及注释,如果我想保持它们的话)。你会如何处理这些棘手的评论和引语?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-08-05 16:12:16

好吧,我自己做的。如果不需要以下代码的可读性,可以将其最小化

代码语言:javascript
复制
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

使用问题中的示例进行测试

代码语言:javascript
复制
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"]

因此,现在我可以使用something like this进一步解析这些单词。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3363227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档