Question

Sorry for my bad English.

I wrote ANTLR4-grammar for GDB/MI output commands from this manual:

grammar GdbOutput;

output : out_of_band_record | result_record | terminator_record;
result_record : TOKEN? '^' RESULT_CLASS (',' result)*;
out_of_band_record : async_record
    | stream_record;
async_record : exec_async_output
    | status_async_output
    | notify_async_output;
exec_async_output : TOKEN? '*' async_output;
status_async_output : TOKEN? '+' async_output;
notify_async_output : TOKEN? '=' async_output;
async_output : async_class (',' result)*;
RESULT_CLASS : 'done'
    | 'running'
    | 'connected'
    | 'error'
    | 'exit';
async_class : 'stopped'; //TODO
result : VARIABLE '=' value;
value : const
    | tuple
    | list;
const : c_string;
c_string : '"' STRING_LITERAL '"';
tuple : '{}'
    | '{' result (',' result)* '}';
list : '[]'
    | '[' value (',' value)* ']'
    | '[' result (',' result)* ']';
stream_record : console_stream_output
    | target_stream_output
    | log_stream_output;
console_stream_output : '~' c_string;
target_stream_output : '@' c_string;
log_stream_output : '&' c_string;
terminator_record : '(gdb)';

VARIABLE : [a-z-]*;
STRING_LITERAL : (~('"' | '\\' | '\r' | '\n') | '\\' ('"' | '\\'))*;
TOKEN : [0-9]+;

I tried some output strings from GDB:

  1. "~\"Reading symbols from C:\\src.exe...\"" - OK (out_of_band_record -> stream_record -> console_stream_output)
  2. "(gdb)" -> OK (terminator_record)
  3. "^done,bkpt={number=\"1\",type=\"breakpoint\",disp=\"keep\",enabled=\"y\",addr=\"0x00000000004014e4\",file=\"src.s\",fullname=\"C:\\src.s\",line=\"17\",thread-groups=[\"i1\"],times=\"0\",original-location=\"main\"}" - FAIL (with exception: line 1:0 no viable alternative at input '^done,bkpt={number=')
  4. "^done,bkpt=\"1\"" - FAIL (line 1:0 no viable alternative at input '^done,bkpt=)
  5. "^done,bkpt={}" - FAIL (line 1:0 no viable alternative at input '^done,bkpt={}')

Why my parser didn't recognize strings #3-5?

P.S.: C# target for ANTLR v.4.2.0 prerelease from Nuget

Was it helpful?

Solution

For starters, let your STRING_LITERAL match the quotes too: don't match them in a parser rule. And let your VARIABLE rule match at least one character (change the * into a +).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top