%option prefix="parse_events_"
+%option stack
 
 %{
 #include <errno.h>
 
 %}
 
+%x mem
+
 num_dec                [0-9]+
 num_hex                0x[a-fA-F0-9]+
 num_raw_hex    [a-fA-F0-9]+
 period                 { return term(PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); }
 branch_type            { return term(PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
 
-mem:                   { return PE_PREFIX_MEM; }
+mem:                   { BEGIN(mem); return PE_PREFIX_MEM; }
 r{num_raw_hex}         { return raw(); }
 {num_dec}              { return value(10); }
 {num_hex}              { return value(16); }
 
 {modifier_event}       { return str(PE_MODIFIER_EVENT); }
-{modifier_bp}          { return str(PE_MODIFIER_BP); }
 {name}                 { return str(PE_NAME); }
 "/"                    { return '/'; }
 -                      { return '-'; }
 :                      { return ':'; }
 =                      { return '='; }
 
+<mem>{
+{modifier_bp}          { return str(PE_MODIFIER_BP); }
+:                      { return ':'; }
+{num_dec}              { return value(10); }
+{num_hex}              { return value(16); }
+       /*
+        * We need to separate 'mem:' scanner part, in order to get specific
+        * modifier bits parsed out. Otherwise we would need to handle PE_NAME
+        * and we'd need to parse it manually. During the escape from <mem>
+        * state we need to put the escaping char back, so we dont miss it.
+        */
+.                      { unput(*parse_events_text); BEGIN(INITIAL); }
+       /*
+        * We destroy the scanner after reaching EOF,
+        * but anyway just to be sure get back to INIT state.
+        */
+<<EOF>>                        { BEGIN(INITIAL); }
+}
+
 %%
 
 int parse_events_wrap(void)