Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ker2x
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ker2x/a86b1f6fc0de6a9c1f2e to your computer and use it in GitHub Desktop.
Save ker2x/a86b1f6fc0de6a9c1f2e to your computer and use it in GitHub Desktop.
ragel spec problem
# Lexer.rl
@stack = [];
%%{
machine plexer;
number = ('+'|'-')?digit+('.'digit)*;
main := |*
space => { emit(:SPACE, data, token_array, ts, te) };
number => {
emit(:NUMBER, data, token_array, ts, te)
};
"print" => {
puts @stack.inspect
};
*|;
}%%
%% write data;
def emit(token_type, name, target_array, ts, te)
@stack << {:type => token_type.to_sym, :name => name[ts..te].pack("c*") }
end
def run_lexer(data)
data = data.unpack("c*") if(data.is_a?(String))
eof = data.length
token_array = []
%% write init;
%% write exec;
end
run_lexer('+1 2 -3 4 print 0')
output :
[{:type=>:NUMBER, :name=>"+1 "}, {:type=>:SPACE, :name=>" 2"}, {:type=>:NUMBER, :name=>"2 "}, {:type=>:SPACE, :name=>" -"}, {:type=>:NUMBER, :name=>"-3 "}, {:type=>:SPACE, :name=>" 4"}, {:type=>:NUMBER, :name=>"4 "}, {:type=>:SPACE, :name=>" p"}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment