0
votes

So I'm building a grammar to parse c++ header files.

I have only written grammar for the header files, and I don't intend to write any for the implementation.

My problem is that if a method is implemented in the header rather than just defined.

Foo bar()
{
    //some code
};

I just want to match the implementation of bar to

BLOCK 
    : '{' INTERNAL_BLOCK*? '}'
    ;

fragment INTERNAL_BLOCK
    : BLOCK
    | ~('}')
    ;

but then this interferes for anything other grammar that includes { ... } because this will always match what is in between two braces. Is there anyway to specify which token to use when there is an ambiguity?

p.s. I don't know if the grammar for BLOCK works but you get the gist.

1

1 Answers

0
votes

So, the significant parser rules would be:

method  : mType mTypeName LPAREN RPAREN BLOCK ; // simplified
unknown : . ;

BLOCK tokens produced by the lexer that are not matched as part of the method rule will appear in the parse-tree in unknown context nodes. Analyze the method context nodes and ignore the unknown nodes.