0

I'm trying to parse Android log system file. Everything is going OK but it doesn't work when I try to parse parameters from log content. My grammar which provides parsing log content

logcontent : ( parameter|text|SPECIALCHAR|DIGIT|MINUS|EQUAL|COLON|DOT|APOSTROPHE|LEFTBRACKET|RIGHTBRACKET|SLASH|'_'|WS )+;

parameter : text+ EQUAL (integer|floatnumber|exponentfloat) ;

Parameter has text inside the rule so ANTLR says that grammar is ambiguous. I tried with a different rule definitions but it doesn't work. I'd like to parse this fragment of log

acquireWakeLock flags=0x2000000a tag=KEEP_SCREEN_ON_FLAG uid=1000 pid=373

How can I get whole log in string format and list of pairs 'parameter = value'

flags=0x2000000a

tag=KEEP_SCREEN_ON_FLAG

uid=1000

pid=373

  • 1
    I don't think ANTLR is the right tool for this job, not that it can't do it, I just don't think it is the best tool. –  Jan 02 '13 at 22:19

1 Answers1

1

I highly recommend using regular expressions for this task instead. It's faster and simpler for this type of operation.

Pattern pattern = Pattern.compile("^(.*?)=(.*)$");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
    String key = matcher.group(1);
    String value = matcher.group(2);
    // do whatever here...
}
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280