Ruby词法分析器和新闻1则

团购巨头LivingSocial目前已经从风投融资2.32亿美元,最近和Groupon一样都展开了大规模的收购行动。该公司称,InfoEther的灵活性和专业知识能更增强该公司更高效地创新的能力。收购协议条款尚未透露。

InfoEther是专业从事开源Ruby软件开发语言及其相关Web开发框架 Ruby on Rails的领先技术咨询公司。该开发框架正是LivingSocial所采用的技术的基础。Hadoop的期货价格结构的大数据:DataStax轻灵,EMC和MapR

InfoEther成立于 2001年,据称是美国第一家依靠Ruby(创造于日本)语言创收的公司。2007年后,该公司专门为全球40多家客户进行Rails框架的开发。

对于这次收购,InfoEther首席技术官Chad Fowler评价称:“我们非常高兴能将我们咨询工作中的灵活性和精力带到才华横溢的LivingSocial团队中来,帮助改变地方性商业的面貌。”

LivingSocial最近收购了Let’s Bonus的大部分股权,2010年收购了Urban Escapes。该公司称期望今年能扩张到300个市场。
class WordAnalysis 
  def analysis(filename, outfilename)
    @outfile= File.new(outfilename, "w")
    File.open(filename) do |file|
      while line = file.gets
        analysisHandler(line)
      end
    end
    @outfile.close
  end

  def analysisHandler(line)
    @line = line
    @max = line.length
    @ptr = 0
    @word = ""

    while @ptr<@max
      getChar()
      if isLetter()
        if @ch==105
          getChar()
          if @ch==102
            handlerResult("(1, if )")
          elsif @ch==110
            getChar()
            if @ch==116
              handlerResult("(1, int )")
            else
              idhandler()
            end
          else
            idHandler()
          end
        elsif @ch==109
          getChar()
          if @ch==97
            getChar()
            if @ch==105
              getChar()
              if @ch==110
                handlerResult("(1, main )")
              else
                idHandler()
              end
            else
              idHandler()
            end
          else
            idHandler()
          end
        elsif @ch==99
          getChar()
          if @ch==111
            getChar()
            if @ch==110
              getChar()
              if @ch==115
                getChar()
                if @ch==116
                  handlerResult("(1, const )")
                else
                  idHandler()
                end
              else
                idHandler()
              end
            else
              idHandler()
            end

          else
            idHandler()
          end
        elsif @ch==102
          getChar()
          if @ch==111
            getChar()
            if @ch==114
              handlerResult("(1, for )")
            else
              idHandler()
            end
          else
            idHandler()
          end
        elsif @ch==119
          getChar()
          if @ch==104
            getChar()
            if @ch==105
              getChar()
              if @ch==108
                getChar()
                if @ch==101
                  handlerResult("(1 while )")
                else
                  idHandler()
                end
              else
                idHandler()
              end
            else
              idHandler()
            end
          else
            idHandler()
          end

        elsif @ch==101
          getChar()
          if @ch==108
            getChar()
            if @ch==115
              getChar()
              if @ch==101
                handlerResult("(1, else )")
              else
                idHandler()
              end
              idHandler()
            end
          else
            idHandler()
          end
        elsif @ch==114
          getChar()
          if @ch==101
            getChar()
            if @ch==116
              getChar()
              if @ch==117
                getChar()
                if @ch==114
                  getChar()
                  if @ch==110
                    handlerResult("(1, return )")
                  else
                    idHandler()
                  end
                else
                  idHandler()
                end
              else
                idHandler()
              end
            else
              idHandler()
            end
          else
            idHandler()
          end
        else
          idHandler()

        end


      elsif isDigit()
        intHandler()

      elsif @ch==61 #=
        handlerResult("(4, = )")
      elsif @ch==45 #=
        handlerResult("(4, - )")
      elsif @ch==43 #+
        handlerResult("(4, + )")
      elsif @ch==42 #*
        handlerResult("(5, * )")
      elsif @ch==47 #/
        handlerResult("(5, / )")
      elsif @ch==44 #,
        handlerResult("(5, , )")
      elsif @ch==59 #;
        @word[@word.length-1]=""
        handlerResult("(5, ; )")
      elsif @ch==40 #(
        handlerResult("(5, ( )")
      elsif @ch==41 #)
        handlerResult("(5, ) )")
      elsif @ch==123 #{
        handlerResult("(5, { )")
      elsif @ch==125 #}
        handlerResult("(5, } )")
      elsif isWhitespace()
        @word[@word.length-1]=""
      else
        procError()


      end


    end

  end

  def idHandler()
    while isLetter()||isDigit()
      getChar()
    end
    if !isLetter()||!isDigit()
      retract()
    end

    handlerResult("(2, #{@word} )")


  end

  def intHandler()
    while isDigit()
      getChar()
    end
    if !isDigit()
      if (@ch==61||@ch==43||@ch==42||@ch==44||isWhitespace()||@ch==59)
        retract()
      else
        while !(@ch==61||@ch==43||@ch==42||@ch==44||isWhitespace()||@ch==59)
          getChar()
        end
        if (@ch==61||@ch==43||@ch==42||@ch==44||isWhitespace()||@ch==59)

          retract()
        end
        procWordError()
        return
      end
    end
    handlerResult("(3, #{@word} )")
  end


  def isLetter()
    if (@ch>=65&&@ch<=90)||(@ch>=97&&@ch<=122)
      return true
    else
      return false
    end
  end

#48-57(0-9)
  def isDigit()
    if (@ch>=48&&@ch<=57)
      return true
    else
      return false
    end
  end

  def isWhitespace()
    tempSpace = ""
    tempSpace<<@ch
    delim = /\s+/
    if tempSpace=~ delim
      return true
    else
      return false
    end
  end

  def getChar()
    if @ptr < @max
      @ch = @line[@ptr]
      @word<<@ch
      @ptr = @ptr+1
    else
      @ch = 32
    end
  end

  def retract()
    @word[@word.length-1]=''
    @ptr = @ptr-1
    @ch = @line[@ptr]
  end

  def handlerResult(val)
    puts val
    @outfile.syswrite(val+"\n")
    @word=""
  end

  def procError()
    val=""
    val<<@ch
    puts "return(error,#{val})"
    @outfile.syswrite("return(error,#{val}) \n")
    @word=""
  end

  def procWordError()
    puts "return(error,#{@word})"
    @outfile.syswrite("return(error,#{@word}) \n")
    @word=""
  end

end


word = WordAnalysis.new
word.analysis("/input.txt", "/output.txt")

转载于:https://my.oschina.net/linuxred/blog/14771

1. C++和Java编译器词法分析技术的比较 C++和Java编译器都采用了词法分析技术来将源代码转化为词法单元序列,但它们的实现方式和特点是不同的。 C++编译器的词法分析器主要特点有: (1)采用手写方式实现,使用了有限状态自动机(Finite State Automata)算法,具有高效性和灵活性。 (2)支持宏定义和条件编译,在处理源代码时需要考虑这些预处理指令的影响,增加了复杂度。 (3)支持直接访问内存和指针操作,需要对代码进行更加细致的词法分析,增加了难度。 Java编译器的词法分析器主要特点有: (1)采用工具自动生成,使用了正则表达式和有限状态自动机算法,具有高度规范化和易维护性。 (2)不支持宏定义和条件编译,因此相对简单。 (3)不支持直接访问内存和指针操作,因此避免了一些安全问题。 总体来说,C++编译器的词法分析器具有更高的灵活性和可扩展性,但也更加复杂和容易出错。Java编译器的词法分析器则更加规范化和易于维护,但也可能受到一些限制。 2. PythonRuby编译器词法分析技术的比较 PythonRuby编译器都采用了词法分析技术来将源代码转化为词法单元序列,但它们的实现方式和特点是不同的。 Python编译器的词法分析器主要特点有: (1)采用手写方式实现,使用了有限状态自动机算法,具有高效性和灵活性。 (2)支持动态类型和垃圾回收,需要对类型进行动态推断和处理,增加了复杂度。 (3)使用缩进来表示代码块,需要对缩进进行特殊处理,增加了难度。 Ruby编译器的词法分析器主要特点有: (1)采用工具自动生成,使用了正则表达式和有限状态自动机算法,具有高度规范化和易维护性。 (2)支持动态类型和垃圾回收,需要对类型进行动态推断和处理,增加了复杂度。 (3)使用关键字和标点符号来表示代码块,与传统的C语言类似,因此比较容易理解和使用。 总体来说,Python编译器的词法分析器具有更高的灵活性和可扩展性,但也更加复杂和容易出错。Ruby编译器的词法分析器则更加规范化和易于维护,但也可能受到一些限制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值