Skip to content

Conversation

Vagabond
Copy link

 ./build/bin/re -k str -pl erl '^ab+c?.?[a-z]{2,3}$'
-module(fsm).
-export([main/1]).
main([String]) ->
	try fsm(start, list_to_binary(String))
	catch throw:fail ->
		io:format("failed~n"),
		halt(1);
	throw:{matched, N} ->
		io:format("matched ~b~n", [N]),
		halt(0)
	end.

fsm(start, <<_A,_B,_C, _Rest/binary>>) ->
	if
		
		_A /= 97 -> throw(fail);
		true -> ok
	end,
	if

		_B /= 98 -> throw(fail);
		true -> ok
	end,
	if

		_C =< 96 -> fsm(0, _Rest);
		_C == 97 -> fsm(4, _Rest);
		_C == 98 -> fsm(6, _Rest);
		_C == 99 -> fsm(8, _Rest);
		_C =< 122 -> fsm(4, _Rest)
	end;
fsm(0, <<>>) -> % e.g. "abA"
	throw({matched,3});
fsm(0=_State, <<_A,_Rest/binary>>) -> % e.g. "abA"
	 if		
		_A =< 96 -> throw(fail);
		_A > 122 -> throw(fail);
		true -> fsm(_State, _Rest)
	end;
fsm(1, <<>>) -> % e.g. "abAa"
	throw({matched,13});
fsm(1=_State, <<_A,_Rest/binary>>) -> % e.g. "abAa"
	 if		
		_A =< 96 -> throw(fail);
		_A > 122 -> throw(fail);
		true -> fsm(_State, _Rest)
	end;
fsm(2, <<>>) -> % e.g. "abaaa"
	throw({matched,9});
fsm(2=_State, <<_A,_Rest/binary>>) -> % e.g. "abaaa"
	 if		
		_A =< 96 -> throw(fail);
		_A > 122 -> throw(fail);
		true -> fsm(_State, _Rest)
	end;
fsm(3, <<>>) -> % e.g. "abaaaa"
	throw({matched,10});
fsm(3=_State, <<_A,_Rest/binary>>) -> % e.g. "abaaaa"
	 if		
		true -> throw(fail)
	end;
fsm(4, <<>>) -> % e.g. "aba"
	throw({matched,4});
fsm(4=_State, <<_A,_Rest/binary>>) -> % e.g. "aba"
	 if		
		_A =< 96 -> throw(fail);
		_A > 122 -> throw(fail);
		true -> fsm(_State, _Rest)
	end;
fsm(5, <<>>) -> % e.g. "abaa"
	throw({matched,8});
fsm(5=_State, <<_A,_Rest/binary>>) -> % e.g. "abaa"
	 if		
		_A =< 96 -> throw(fail);
		_A =< 122 -> fsm(2, _Rest);
		true -> throw(fail)
	end;
fsm(6, <<>>) -> % e.g. "abb"
	throw({matched,5});
fsm(6=_State, <<_A,_Rest/binary>>) -> % e.g. "abb"
	 if		
		_A =< 96 -> fsm(0, _Rest);
		_A == 97 -> fsm(7, _Rest);
		_A == 98 -> fsm(9, _Rest);
		_A == 99 -> fsm(10, _Rest);
		_A > 122 -> fsm(0, _Rest);
		true -> fsm(_State, _Rest)
	end;
fsm(7, <<>>) -> % e.g. "abca"
	throw({matched,7});
fsm(7=_State, <<_A,_Rest/binary>>) -> % e.g. "abca"
	 if		
		_A =< 96 -> throw(fail);
		_A =< 122 -> fsm(5, _Rest);
		true -> throw(fail)
	end;
fsm(8, <<>>) -> % e.g. "abc"
	throw({matched,6});
fsm(8=_State, <<_A,_Rest/binary>>) -> % e.g. "abc"
	 if		
		_A =< 96 -> fsm(0, _Rest);
		_A =< 122 -> fsm(7, _Rest);
		true -> fsm(0, _Rest)
	end;
fsm(9, <<>>) -> % e.g. "abbb"
	throw({matched,11});
fsm(9=_State, <<_A,_Rest/binary>>) -> % e.g. "abbb"
	 if		
		_A =< 96 -> fsm(0, _Rest);
		_A == 97 -> fsm(7, _Rest);
		_A == 98 -> fsm(9, _Rest);
		_A == 99 -> fsm(10, _Rest);
		_A =< 122 -> fsm(7, _Rest);
		true -> fsm(0, _Rest)
	end;
fsm(10, <<>>) -> % e.g. "abbc"
	throw({matched,12});
fsm(10=_State, <<_A,_Rest/binary>>) -> % e.g. "abbc"
	 if		
		_A =< 96 -> fsm(0, _Rest);
		_A =< 122 -> fsm(7, _Rest);
		true -> fsm(0, _Rest)
	end;
fsm(_, _) -> throw(fail).
; escript fsm.erl hello
failed
; escript fsm.erl abcdf
matched 8
; escript fsm.erl abd
matched 4
; escript fsm.erl ac
failed
; escript fsm.erl abcdefgh
matched 9

The code generator is really kind of terrible, could use some help dealing with the edge conditions better...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant