词法分析_2

Lexer = {p=1,src="",line=0,code=true,list={},tmp="",err=false,errors="",keyword={i_null="null",i_true="true",i_false="false",i_s="s",i_ss="ss",i_sss="sss",i_f="f",i_else="else",i_w="w",i_t="t",i_for="for",i_fn="fn",i_end="end",i_goto="goto",i_endcode="endcode",i_return="return"}}

function Lexer:new(str)

  self.src = str
  return self

end

先创个lua文件,p是字符串指针、src是字符串、line表示当前行、list用来保存每行代码(出错时用来提示)、err是错误判断、errors用来保存错误信息、keyword是关键字列表。其它的信息暂时不讲。用处不大

然后function Lexer:new用来初始化。

再写个run方法函数用于开始词法解析

下面是具体代码,有一定lua基础的人应该能看懂

function Lexer:run()

  local result,code = {},true

  while code do
    
    local token = Lexer:next()
    
    if Lexer.errors ~= "" then return Lexer.errors,Lexer.list,true end
   
    if token == nil then return result,Lexer.list,false end
    
    result[#result+1] = token
    
    end
  
  return result,Lexer.list,false

end

上面代码具体意思是说,用while循环获取整个token(上偏文章说的{"KEYWORD","s",1}数据),而Lexer:next()用来获取下个token table(表)

现在看看Lexer:next()整个代码(由于是直接发全部代码,可能比较长。可以从后面向上看。)

function Lexer:next()

   local ch = string.char(string.byte(Lexer.src,Lexer.p))
   
   if ch == nil or ch == "" then return nil end
   
   local t,v,l,lines
   
   --换行解析
   if ch == "\x0a" then
     
     t = "LINE" v = ch l = Lexer.line
     
     Lexer.line = Lexer.line + 1
     
     --空格解析
     elseif ch == "\x20" then
       
     t = "SPACE" v = ch l = Lexer.line
     
     --标识符解析
     elseif "a" <= ch and ch <= "z" or "A" <= ch and ch <= "Z" then
     
     local str = Lexer:Identifier()
     
     t = "IDENTIFIER"
     
     --判断关键字
     local keyword = Lexer:Keyword(str)
     
     if keyword then t = keyword end
    
     v = str l = Lexer.line
     
     --解析数字
     elseif "0" <= ch and ch <= "9" then
     
     t = "NUMBER" v = Lexer:Number() l = Lexer.line
     
     --解析字符串
     elseif ch == "\"" or ch == "'" then
     
     local vv = Lexer.String()
     
     t = "STRING" v = vv l = Lexer.line
     
     lines = "\"" .. vv .. "\""
     
     elseif ch == '+' then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析+=
     if chr == "=" then t = "ADD_ASSIGN" v = ch .. chr l = Lexer.line
       
       --解析++
       elseif chr == "+" then t = "INCREMENT" v = ch .. chr l = Lexer.line
       
       --解析+
       else Lexer.p = Lexer.p - 1 t = "PLUS" v = ch l = Lexer.line end
     
     elseif ch == "-" then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析-=
     if chr == "=" then t = "SUBTRACT_ASSIGN" v = ch .. chr l = Lexer.line
       
       --解析--
       elseif chr == "-" then t = "DECREMENT" v = ch .. chr l = Lexer.line
       
       --解析-
       else Lexer.p = Lexer.p - 1 t = "MINUS" v = ch l = Lexer.line end
     
     elseif ch == "*" then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析*指针
     if "a" <= chr and chr <= "z" or "A" <= chr and chr <= "Z" then t = "POINTER" v = Lexer:Identifier() l = Lexer.line
     
       --解析*=
       elseif chr == "=" then t = "MULTIPLY_ASSIGN" v = ch .. chr l = Lexer.line
       
       --解析*
       else Lexer.p = Lexer.p - 1 t = "MULTIPLY" v = ch l = Lexer.line end
     
     elseif ch == "/" then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析/=
     if chr == "=" then t = "QUOTIENT_ASSIGN" v = ch .. chr l = Lexer.line
       
       --解析//
       elseif chr == "/" then
       
       
       
       local line,c_p,c_d,cc = Lexer.line,true,"",""

       while c_p do

       Lexer.p = Lexer.p + 1

       cc = string.char(string.byte(Lexer.src,Lexer.p))

       if cc == "\x0a" or cc == "" then Lexer.line = Lexer.line + 1 break end

       c_d = c_d .. cc

       end

       t = "COMMENT" v = c_d l = line
       
       
       
       --解析/* ... */
       elseif chr == "*" then
       
       
       
       local line,c_p,c_d,a,b = Lexer.line,true,"","",""

       while c_p do

       Lexer.p = Lexer.p + 1

       a = string.char(string.byte(Lexer.src,Lexer.p))

       b = string.char(string.byte(Lexer.src,Lexer.p+1))

       if a == "" then Lexer.p = Lexer.p + 1 break end

       if a == '*' and b == '/' then break end

       if a == "\x0a" then Lexer.line = Lexer.line + 1 end

       c_d = c_d .. a

       end

       Lexer.p = Lexer.p + 1
       
       t = "COMMENT" v = c_d l = line
       
       
       
       --解析/
       else Lexer.p = Lexer.p - 1 t = "SLASH" v = ch l = Lexer.line end
     
     elseif ch == "%" then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析%=
     if chr == "=" then t = "REMAINDER_ASSIGN" v = ch .. chr l = Lexer.line
       
       --解析%
       else Lexer.p = Lexer.p - 1 t = "REMAINDER" v = ch l = Lexer.line end
     
     elseif ch == "&" then
     
      Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析&指针
     if "a" <= chr and chr <= "z" or "A" <= chr and chr <= "Z" then t = "QUOTE" v = Lexer:Identifier() l = Lexer.line
     
       --解析&^
       elseif chr == "^" then t = "AND_NOT" v = ch .. chr l = Lexer.line
       
       --解析&=
       elseif chr == "=" then t = "AND_ASSIGN" v = ch .. chr l = Lexer.line
       
       --解析&&
       elseif chr == "&" then t = "LOGICAL_AND" v = ch .. chr l = Lexer.line
       
       --解析&
       else Lexer.p = Lexer.p - 1 t = "AND" v = ch l = Lexer.line end
     
     elseif ch == "|" then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析|=
     if chr == "=" then t = "OR_ASSIGN" v = ch .. chr l = Lexer.line
       
       --解析||
       elseif chr == "|" then t = "LOGICAL_OR" v = ch .. chr l = Lexer.line
       
       --解析|
       else Lexer.p = Lexer.p - 1 t = "OR" v = ch l = Lexer.line end
     
     elseif ch == "^" then t = "EXCLUSIVE_OR" v = ch l = Lexer.line
     
     elseif ch == "=" then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析==
     if chr == "=" then
       
       Lexer.p = Lexer.p + 1
       
       local chrs = string.char(string.byte(Lexer.src,Lexer.p))
       
       --解析===
       if chrs == "=" then t = "STRICT_EQUAL" v = ch .. chr .. chrs l = Lexer.line
         
         --解析==
         else Lexer.p = Lexer.p - 1 t = "EQUAL" v = ch .. chr l = Lexer.line end
       
       --解析=
       else Lexer.p = Lexer.p - 1 t = "ASSIGN" v = ch l = Lexer.line end
     
     elseif ch == "<" then
       
       Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析<=
     if chr == "=" then t = "LESS_OR_EQUAL" v = ch .. chr l = Lexer.line
       
       --解析<<
       elseif chr == "<" then t = "SHIFT_LEFT" v = ch .. chr l = Lexer.line
       
       --解析<
       else Lexer.p = Lexer.p - 1 t = "LESS" v = ch l = Lexer.line end
     
     elseif ch == ">" then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析>=
     if chr == "=" then t = "GREATER_OR_EQUAL" v = ch .. chr l = Lexer.line
       
       --解析>>
       elseif chr == ">" then t = "SHIFT_RIGHT" v = ch .. chr l = Lexer.line
       
       --解析>
       else Lexer.p = Lexer.p - 1 t = "GREATER" v = ch l = Lexer.line end
     
     elseif ch == "!" then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析!=
     if chr == "=" then
       
       Lexer.p = Lexer.p + 1
       
       local chrs = string.char(string.byte(Lexer.src,Lexer.p))
       
       --解析!==
       if chrs == "=" then t = "STRICT_NOT_EQUAL" v = ch .. chr .. chrs l = Lexer.line
         
         --解析!=
         else Lexer.p = Lexer.p - 1 t = "NOT_EQUAL" v = ch .. chr l = Lexer.line end
       
       --解析!
       else Lexer.p = Lexer.p - 1 t = "NOT" v = ch l = Lexer.line end
     
     --解析~
     elseif ch == "~" then t = "BITWISE_NOT" v = ch l = Lexer.line
     
     --解析(
     elseif ch == "(" then t = "LEFT_PARENTHESIS" v = ch l = Lexer.line
     
     --解析[
     elseif ch == "[" then t = "LEFT_BRACKET" v = ch l = Lexer.line
     
     --解析{
     elseif ch == "{" then t = "LEFT_BRACE" v = ch l = Lexer.line
     
     --解析)
     elseif ch == ")" then t = "RIGHT_PARENTHESIS" v = ch l = Lexer.line
     
     --解析]
     elseif ch == "]" then t = "RIGHT_BRACKET" v = ch l = Lexer.line
     
     --解析}
     elseif ch == "}" then t = "RIGHT_BRACE" v = ch l = Lexer.line
     
     --解析,
     elseif ch == "," then t = "COMMA" v = ch l = Lexer.line
     
     elseif ch == "." then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析..
     if chr == "." then t = "CONCAT" v = ch .. chr l = Lexer.line
       
       --解析.
       else Lexer.p = Lexer.p - 1 t = "PERIOD" v = ch l = Lexer.line end
     
     --解析;
     elseif ch == ";" then t = "SEMICOLON" v = ch l = Lexer.line
     
     --解析:
     elseif ch == ":" then t = "COLON" v = ch l = Lexer.line
     
     --解析?
     elseif ch == "?" then t = "QUESTION_MARK" v = ch l = Lexer.line
     
     else
     
     if Lexer.err == true and ch ~= "" then Lexer.errors = "词法分析错误:在第" .. Lexer.line .. "行,error数据[" .. ch .. "],具体代码--->" .. Lexer.list[Lexer.line] .. Lexer:Line() return nil end
     
     end
   
   Lexer.err = true
   
   Lexer.p = Lexer.p + 1
   
   --存储每行代码
     if lines then str = lines else str = v end
     
     if Lexer.list[l] == nil then Lexer.list[l] = "" end
     
     Lexer.list[l] = Lexer.list[l] .. str
   
   return {token=t,value=v,line=l}
   
end

附辅助函数:

--解析一行
function Lexer:Line()
  
  local result = ""
  
  while true do
    
    local chr = string.char(string.byte(Lexer.src,Lexer.p))
    
    if chr == "\x0a" or chr == "" then return result
      
      else result = result .. chr Lexer.p = Lexer.p + 1 end
    
    end
  
  end

--整体标识符解析
function Lexer:Identifier()

   local result = ""

   local code = true

   while code do

   local chr = string.char(string.byte(Lexer.src,Lexer.p))

   if chr == nil or chr == "" then code = false break end

   if ("a" <= chr and chr <= "z") or ("A" <= chr and chr <= "Z") or ("0" <= chr and chr <= "9") then

   result = result .. chr

   Lexer.p = Lexer.p + 1

   else

   code = false

   end

   end

   Lexer.p = Lexer.p - 1

   return result

end

--判断关键字
function Lexer:Keyword(str)
  
  local keyword = Lexer.keyword
  
  for key,value in pairs(keyword) do
    
    if value == str then
        
        return key
        
        end
  
  end

  return false
  
  end


--解析字符串
function Lexer.String()

  local p = string.char(string.byte(Lexer.src,Lexer.p))

  Lexer.p = Lexer.p + 1

  local result = ""

  local code = true

  while code do

  local chr = string.char(string.byte(Lexer.src,Lexer.p))

  if chr == "\x0a" then Lexer.line = Lexer.line + 1 end

  if chr == "" then break end

  if chr ~= p then

  result = result .. chr

  Lexer.p = Lexer.p + 1

  else

  code = false;

  end

end

return result

end

--整体数字解析
function Lexer:Number()

   local result = ""

   local code = true

   while code do

   local chr = string.char(string.byte(Lexer.src,Lexer.p))

   if chr == nil or chr == "" then code = false break end

   if "0" <= chr and chr <= "9" then

   result = result .. chr

   Lexer.p = Lexer.p + 1

   else

   code = false

   end

   end

   Lexer.p = Lexer.p - 1

   return result

end

lexer.lua文件全部代码:

Lexer = {p=1,src="",line=0,code=true,list={},tmp="",err=false,errors="",keyword={i_null="null",i_true="true",i_false="false",i_s="s",i_ss="ss",i_sss="sss",i_f="f",i_else="else",i_w="w",i_t="t",i_for="for",i_fn="fn",i_end="end",i_goto="goto",i_endcode="endcode",i_return="return"}}

function Lexer:new(str)

  self.src = str
  return self

end

function Lexer:run()

  local result,code = {},true

  while code do
    
    local token = Lexer:next()
    
    if Lexer.errors ~= "" then return Lexer.errors,Lexer.list,true end
   
    if token == nil then return result,Lexer.list,false end
    
    result[#result+1] = token
    
    end
  
  return result,Lexer.list,false

end

function Lexer:next()

   local ch = string.char(string.byte(Lexer.src,Lexer.p))
   
   if ch == nil or ch == "" then return nil end
   
   local t,v,l,lines
   
   --换行解析
   if ch == "\x0a" then
     
     t = "LINE" v = ch l = Lexer.line
     
     Lexer.line = Lexer.line + 1
     
     --空格解析
     elseif ch == "\x20" then
       
     t = "SPACE" v = ch l = Lexer.line
     
     --标识符解析
     elseif "a" <= ch and ch <= "z" or "A" <= ch and ch <= "Z" then
     
     local str = Lexer:Identifier()
     
     t = "IDENTIFIER"
     
     --判断关键字
     local keyword = Lexer:Keyword(str)
     
     if keyword then t = keyword end
    
     v = str l = Lexer.line
     
     --解析数字
     elseif "0" <= ch and ch <= "9" then
     
     t = "NUMBER" v = Lexer:Number() l = Lexer.line
     
     --解析字符串
     elseif ch == "\"" or ch == "'" then
     
     local vv = Lexer.String()
     
     t = "STRING" v = vv l = Lexer.line
     
     lines = "\"" .. vv .. "\""
     
     elseif ch == '+' then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析+=
     if chr == "=" then t = "ADD_ASSIGN" v = ch .. chr l = Lexer.line
       
       --解析++
       elseif chr == "+" then t = "INCREMENT" v = ch .. chr l = Lexer.line
       
       --解析+
       else Lexer.p = Lexer.p - 1 t = "PLUS" v = ch l = Lexer.line end
     
     elseif ch == "-" then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析-=
     if chr == "=" then t = "SUBTRACT_ASSIGN" v = ch .. chr l = Lexer.line
       
       --解析--
       elseif chr == "-" then t = "DECREMENT" v = ch .. chr l = Lexer.line
       
       --解析-
       else Lexer.p = Lexer.p - 1 t = "MINUS" v = ch l = Lexer.line end
     
     elseif ch == "*" then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析*指针
     if "a" <= chr and chr <= "z" or "A" <= chr and chr <= "Z" then t = "POINTER" v = Lexer:Identifier() l = Lexer.line
     
       --解析*=
       elseif chr == "=" then t = "MULTIPLY_ASSIGN" v = ch .. chr l = Lexer.line
       
       --解析*
       else Lexer.p = Lexer.p - 1 t = "MULTIPLY" v = ch l = Lexer.line end
     
     elseif ch == "/" then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析/=
     if chr == "=" then t = "QUOTIENT_ASSIGN" v = ch .. chr l = Lexer.line
       
       --解析//
       elseif chr == "/" then
       
       
       
       local line,c_p,c_d,cc = Lexer.line,true,"",""

       while c_p do

       Lexer.p = Lexer.p + 1

       cc = string.char(string.byte(Lexer.src,Lexer.p))

       if cc == "\x0a" or cc == "" then Lexer.line = Lexer.line + 1 break end

       c_d = c_d .. cc

       end

       t = "COMMENT" v = c_d l = line
       
       
       
       --解析/* ... */
       elseif chr == "*" then
       
       
       
       local line,c_p,c_d,a,b = Lexer.line,true,"","",""

       while c_p do

       Lexer.p = Lexer.p + 1

       a = string.char(string.byte(Lexer.src,Lexer.p))

       b = string.char(string.byte(Lexer.src,Lexer.p+1))

       if a == "" then Lexer.p = Lexer.p + 1 break end

       if a == '*' and b == '/' then break end

       if a == "\x0a" then Lexer.line = Lexer.line + 1 end

       c_d = c_d .. a

       end

       Lexer.p = Lexer.p + 1
       
       t = "COMMENT" v = c_d l = line
       
       
       
       --解析/
       else Lexer.p = Lexer.p - 1 t = "SLASH" v = ch l = Lexer.line end
     
     elseif ch == "%" then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析%=
     if chr == "=" then t = "REMAINDER_ASSIGN" v = ch .. chr l = Lexer.line
       
       --解析%
       else Lexer.p = Lexer.p - 1 t = "REMAINDER" v = ch l = Lexer.line end
     
     elseif ch == "&" then
     
      Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析&指针
     if "a" <= chr and chr <= "z" or "A" <= chr and chr <= "Z" then t = "QUOTE" v = Lexer:Identifier() l = Lexer.line
     
       --解析&^
       elseif chr == "^" then t = "AND_NOT" v = ch .. chr l = Lexer.line
       
       --解析&=
       elseif chr == "=" then t = "AND_ASSIGN" v = ch .. chr l = Lexer.line
       
       --解析&&
       elseif chr == "&" then t = "LOGICAL_AND" v = ch .. chr l = Lexer.line
       
       --解析&
       else Lexer.p = Lexer.p - 1 t = "AND" v = ch l = Lexer.line end
     
     elseif ch == "|" then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析|=
     if chr == "=" then t = "OR_ASSIGN" v = ch .. chr l = Lexer.line
       
       --解析||
       elseif chr == "|" then t = "LOGICAL_OR" v = ch .. chr l = Lexer.line
       
       --解析|
       else Lexer.p = Lexer.p - 1 t = "OR" v = ch l = Lexer.line end
     
     elseif ch == "^" then t = "EXCLUSIVE_OR" v = ch l = Lexer.line
     
     elseif ch == "=" then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析==
     if chr == "=" then
       
       Lexer.p = Lexer.p + 1
       
       local chrs = string.char(string.byte(Lexer.src,Lexer.p))
       
       --解析===
       if chrs == "=" then t = "STRICT_EQUAL" v = ch .. chr .. chrs l = Lexer.line
         
         --解析==
         else Lexer.p = Lexer.p - 1 t = "EQUAL" v = ch .. chr l = Lexer.line end
       
       --解析=
       else Lexer.p = Lexer.p - 1 t = "ASSIGN" v = ch l = Lexer.line end
     
     elseif ch == "<" then
       
       Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析<=
     if chr == "=" then t = "LESS_OR_EQUAL" v = ch .. chr l = Lexer.line
       
       --解析<<
       elseif chr == "<" then t = "SHIFT_LEFT" v = ch .. chr l = Lexer.line
       
       --解析<
       else Lexer.p = Lexer.p - 1 t = "LESS" v = ch l = Lexer.line end
     
     elseif ch == ">" then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析>=
     if chr == "=" then t = "GREATER_OR_EQUAL" v = ch .. chr l = Lexer.line
       
       --解析>>
       elseif chr == ">" then t = "SHIFT_RIGHT" v = ch .. chr l = Lexer.line
       
       --解析>
       else Lexer.p = Lexer.p - 1 t = "GREATER" v = ch l = Lexer.line end
     
     elseif ch == "!" then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析!=
     if chr == "=" then
       
       Lexer.p = Lexer.p + 1
       
       local chrs = string.char(string.byte(Lexer.src,Lexer.p))
       
       --解析!==
       if chrs == "=" then t = "STRICT_NOT_EQUAL" v = ch .. chr .. chrs l = Lexer.line
         
         --解析!=
         else Lexer.p = Lexer.p - 1 t = "NOT_EQUAL" v = ch .. chr l = Lexer.line end
       
       --解析!
       else Lexer.p = Lexer.p - 1 t = "NOT" v = ch l = Lexer.line end
     
     --解析~
     elseif ch == "~" then t = "BITWISE_NOT" v = ch l = Lexer.line
     
     --解析(
     elseif ch == "(" then t = "LEFT_PARENTHESIS" v = ch l = Lexer.line
     
     --解析[
     elseif ch == "[" then t = "LEFT_BRACKET" v = ch l = Lexer.line
     
     --解析{
     elseif ch == "{" then t = "LEFT_BRACE" v = ch l = Lexer.line
     
     --解析)
     elseif ch == ")" then t = "RIGHT_PARENTHESIS" v = ch l = Lexer.line
     
     --解析]
     elseif ch == "]" then t = "RIGHT_BRACKET" v = ch l = Lexer.line
     
     --解析}
     elseif ch == "}" then t = "RIGHT_BRACE" v = ch l = Lexer.line
     
     --解析,
     elseif ch == "," then t = "COMMA" v = ch l = Lexer.line
     
     elseif ch == "." then
     
     Lexer.p = Lexer.p + 1
     
     local chr = string.char(string.byte(Lexer.src,Lexer.p))
     
     --解析..
     if chr == "." then t = "CONCAT" v = ch .. chr l = Lexer.line
       
       --解析.
       else Lexer.p = Lexer.p - 1 t = "PERIOD" v = ch l = Lexer.line end
     
     --解析;
     elseif ch == ";" then t = "SEMICOLON" v = ch l = Lexer.line
     
     --解析:
     elseif ch == ":" then t = "COLON" v = ch l = Lexer.line
     
     --解析?
     elseif ch == "?" then t = "QUESTION_MARK" v = ch l = Lexer.line
     
     else
     
     if Lexer.err == true and ch ~= "" then Lexer.errors = "词法分析错误:在第" .. Lexer.line .. "行,error数据[" .. ch .. "],具体代码--->" .. Lexer.list[Lexer.line] .. Lexer:Line() return nil end
     
     end
   
   Lexer.err = true
   
   Lexer.p = Lexer.p + 1
   
   --存储每行代码
     if lines then str = lines else str = v end
     
     if Lexer.list[l] == nil then Lexer.list[l] = "" end
     
     Lexer.list[l] = Lexer.list[l] .. str
   
   return {token=t,value=v,line=l}
   
end

--解析一行
function Lexer:Line()
  
  local result = ""
  
  while true do
    
    local chr = string.char(string.byte(Lexer.src,Lexer.p))
    
    if chr == "\x0a" or chr == "" then return result
      
      else result = result .. chr Lexer.p = Lexer.p + 1 end
    
    end
  
  end

--整体标识符解析
function Lexer:Identifier()

   local result = ""

   local code = true

   while code do

   local chr = string.char(string.byte(Lexer.src,Lexer.p))

   if chr == nil or chr == "" then code = false break end

   if ("a" <= chr and chr <= "z") or ("A" <= chr and chr <= "Z") or ("0" <= chr and chr <= "9") then

   result = result .. chr

   Lexer.p = Lexer.p + 1

   else

   code = false

   end

   end

   Lexer.p = Lexer.p - 1

   return result

end

--判断关键字
function Lexer:Keyword(str)
  
  local keyword = Lexer.keyword
  
  for key,value in pairs(keyword) do
    
    if value == str then
        
        return key
        
        end
  
  end

  return false
  
  end


--解析字符串
function Lexer.String()

  local p = string.char(string.byte(Lexer.src,Lexer.p))

  Lexer.p = Lexer.p + 1

  local result = ""

  local code = true

  while code do

  local chr = string.char(string.byte(Lexer.src,Lexer.p))

  if chr == "\x0a" then Lexer.line = Lexer.line + 1 end

  if chr == "" then break end

  if chr ~= p then

  result = result .. chr

  Lexer.p = Lexer.p + 1

  else

  code = false;

  end

end

return result

end

--整体数字解析
function Lexer:Number()

   local result = ""

   local code = true

   while code do

   local chr = string.char(string.byte(Lexer.src,Lexer.p))

   if chr == nil or chr == "" then code = false break end

   if "0" <= chr and chr <= "9" then

   result = result .. chr

   Lexer.p = Lexer.p + 1

   else

   code = false

   end

   end

   Lexer.p = Lexer.p - 1

   return result

end

return Lexer

(文章暂时停更,作者有空再继续讲解上面代码具体有什么用)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,012评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,628评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,653评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,485评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,574评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,590评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,596评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,340评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,794评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,102评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,276评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,940评论 5 339
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,583评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,201评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,441评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,173评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,136评论 2 352

推荐阅读更多精彩内容

  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,781评论 0 38
  • 第一篇 语言 第0章 序言 Lua仅让你用少量的代码解决关键问题。 Lua所提供的机制是C不擅长的:高级语言,动态...
    testfor阅读 2,664评论 1 7
  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,380评论 0 5
  • 标题: Rakudo and NQP Internals子标题: The guts tormented imple...
    焉知非鱼阅读 1,367评论 1 3
  • 20170429 亲爱的子宜,今天五一小长假,一家人美美的睡了一个懒觉,然后一起吃早餐聊天。谈了两件事。一是昨晚你...
    Grit888阅读 242评论 0 2