lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


For kicks I tried to translate a Boyer-Moore recognizer into LPEG.
Using an example from Roberto's talk I got 'eartt' to work OK on
small examples, but when I tried the Bible, this happened:

: nr@homedog 7952 ; ./eartt.lua < /home/nr/tmp/downloads/kjv10.txt 
/usr/bin/lua5.1: ./eartt.lua:53: too many pending calls/choices
stack traceback:
        [C]: in function 'match'
        ./eartt.lua:53: in function 'find'
        ./eartt.lua:57: in main chunk
        [C]: ?
: nr@homedog 7953 ; 


Anybody have any ideas how to work around it?


Norman

P.S. For diehards, here's the grammar.  The 'L' function ('look') is
purely for debugging:



local P, S, R, V = lpeg.P, lpeg.S, lpeg.R, lpeg.V

local v = setmetatable ({}, { __index = function(t, k) t[k] = V(k); return t[k] end })

local function M(pat)
  if type(pat) == 'string' then pat = P(pat) end
  return -(-pat)
end -- matches pat

local text = io.stdin:read '*a'

local function L(s)
  return lpeg.Cp() /
  function(pos)
    io.stderr:write('Looking for ', s, ' at position ', pos,
                    ' ("', text:sub(pos, pos+5), '"...)\n')
  end
end

local eartt = P(4) * ( -S'eart' * P(1) * v.eartt
                     + M 'e' * v.eartt) +
              M(P(4) * M 'a') * P(3) * v.eartt +
              M(P(4) * M 'r') * P(2) * v.eartt +
              M(P(4) * M 't') * v.eart * P(1)

local eart = P(3) * (-S'eart' * P(1) * v.eartt
                     + M 'e' * v.eartt) +
              M(P(3) * M 'a') * P(2) * v.eartt +
              M(P(3) * M 'r') * P(1) * v.eartt +
              M(P(3) * M 't') * v.ear * P(1)

local ear  = P(2) * (-S'ear' * P(1) * v.eartt
                     + M 'e' * v.eartt) +
              M(P(2) * M 'a') * P(1) * v.eartt +
              M(P(2) * M 'r') * v.ea * P(1)

local ea = P(1) * (-S'a' * P(1) * v.eartt) + S 'e' * P(1)

local search = P { 'eartt',
                   eartt = L 'eartt' * eartt,
                   eart = L 'eart' * eart,
                   ear = L 'ear' * ear,
                   ea = L 'ea' * ea }