}
 
+<config>{
+config                 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); }
+config1                        { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG1); }
+config2                        { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); }
+name                   { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); }
+period                 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); }
+branch_type            { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
+,                      { return ','; }
+"/"                    { BEGIN(INITIAL); return '/'; }
+{name_minus}           { return str(yyscanner, PE_NAME); }
+}
+
+<mem>{
+{modifier_bp}          { return str(yyscanner, PE_MODIFIER_BP); }
+:                      { return ':'; }
+{num_dec}              { return value(yyscanner, 10); }
+{num_hex}              { return value(yyscanner, 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(*yytext); BEGIN(INITIAL); }
+       /*
+        * We destroy the scanner after reaching EOF,
+        * but anyway just to be sure get back to INIT state.
+        */
+<<EOF>>                        { BEGIN(INITIAL); }
+}
+
 cpu-cycles|cycles                              { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); }
 stalled-cycles-frontend|idle-cycles-frontend   { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
 stalled-cycles-backend|idle-cycles-backend     { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
 refs|Reference|ops|access              |
 misses|miss                            { return str(yyscanner, PE_NAME_CACHE_OP_RESULT); }
 
-<config>{
-config                 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); }
-config1                        { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG1); }
-config2                        { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); }
-name                   { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); }
-period                 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); }
-branch_type            { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
-,                      { return ','; }
-"/"                    { BEGIN(INITIAL); return '/'; }
-{name_minus}           { return str(yyscanner, PE_NAME); }
-}
-
 mem:                   { BEGIN(mem); return PE_PREFIX_MEM; }
 r{num_raw_hex}         { return raw(yyscanner); }
 {num_dec}              { return value(yyscanner, 10); }
 "}"                    { return '}'; }
 =                      { return '='; }
 \n                     { }
-
-<mem>{
-{modifier_bp}          { return str(yyscanner, PE_MODIFIER_BP); }
-:                      { return ':'; }
-{num_dec}              { return value(yyscanner, 10); }
-{num_hex}              { return value(yyscanner, 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(*yytext); BEGIN(INITIAL); }
-       /*
-        * We destroy the scanner after reaching EOF,
-        * but anyway just to be sure get back to INIT state.
-        */
-<<EOF>>                        { BEGIN(INITIAL); }
-}
+.                      { }
 
 %%