lua-users home
lua-l archive

Re: how to translate lua pattern "(.*)and(.*)" with lpeg re ?

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


It was thus said that the Great albertmcchan once stated:
> during testing, lpeg backtracking can only handle max string length of 199:
> is this a lpeg bug ? only 199 chars limit ?
 Nope. It's documented. From the LPeg documentation:
	lpeg.setmaxstack (max)
		Sets a limit for the size of the backtrack stack used by
		LPeg to track calls and choices. (The default limit is 400.)
		Most well-written patterns need little backtrack levels and
		therefore you seldom need to change this limit; before
		changing it you should try to rewrite your pattern to avoid
		the need for extra space. Nevertheless, a few useful
		patterns may overflow. Also, with recursive grammars,
		subjects with deep recursion may also need larger limits.
And this is exactly what you are hitting:
	lua: a.lua:18: backtrack stack overflow (current limit is 400)
You need to rethink your solution. Nick Gamon's email [1] has a solution
that can work, albeit with a slight tweak:
c = re.compile [[
 parse <- {noDelim} lastDelim -- look for all up to the last
 -- delimiter followed by the last part
 delim <- 'and' -- our delimiter
 noDelim <- (!lastDelim .)* -- zero or more characters
 -- without the last delimiter
 lastDelim <- delim {(!delim .)*} !. -- the delimiter without any more
 -- delimiters and then end of subject
]]
print(c:match( str:rep(50) ))
 -spc
[1]	http://lua-users.org/lists/lua-l/2018-01/msg00294.html

AltStyle によって変換されたページ (->オリジナル) /