Reviews grammar and parsers and discusses my personal path toward writing my own packrat parser-generator for Erlang called neotoma.
Given to "Evil Robot Conference" on September 12, 2009.
58. % Memoization wrapper
p(Inp, StartIndex, Name, ParseFun, TransformFun) ->
% Grab the memo table from ets
Memo = get_memo(StartIndex),
% See if the current reduction is memoized
case dict:find(Name, Memo) of
% If it is, return the result
{ok, Result} -> Result;
% If not, attempt to parse
_ ->
case ParseFun(Inp, StartIndex) of
% If it fails, memoize the failure
{fail,_} = Failure ->
memoize(StartIndex, dict:store(Name, Failure, Memo)),
Failure;
% If it passes, transform and memoize the result.
{Result, InpRem, NewIndex} ->
Transformed = TransformFun(Result, StartIndex),
memoize(StartIndex, dict:store(Name, {Transformed,
InpRem, NewIndex}, Memo)),
{Transformed, InpRem, NewIndex}
end
end.
Saturday, September 12, 2009