views:

50

answers:

1

I'm trying to do some very basic C++ function declaration parsing. Here is my rule for parsing an input parameter:

arg : 'const'? 'unsigned'? t=STRING m=TYPEMOD? n=STRING 
 -> ^(ARG $n $t $m?) ;

STRING  : ('a'..'z'|'A'..'Z'|'0'..'9'|'_'|'::')+ ;

TYPEMOD 
 : ('*' | '&')+ ;

The problem is I'm trying to pass it something like:

int *param

It matches the "int" with t, but then skips the TYPEMOD, throws an exception saying the * is unwanted when trying to match the n, and then matches "param" with n. Why would it skip the TYPEMOD token and not match it?

Thanks in advance!

EDIT:

Here are my whitespace rules (I included my comment rules as well), everything seems to work fine when parsing other stuff like class declarations, properties, structs, it's just these TYPEMOD things...

COMMENT_LINE 
    :   '//' (~'\n')* '\n' { $channel = HIDDEN; } ;

COMMENT_BLOCK 
    :   '/*' .* '*/' { $channel = HIDDEN; } ;

NL      :   ('\r' | '\n')+ { $channel = HIDDEN; } ;

WS      :   (' '|'\t')+ { $channel = HIDDEN; } ;
A: 

With only minor changes I think this is what you want the rules to look like to continue with your work:

 arg : 'const'? 'unsigned'? t=STRING m=typemod? n=STRING 
 -> ^(ARG $n $t $m?) ;

typemod : TYPEMOD+;

STRING  : ('a'..'z'|'A'..'Z'|'0'..'9'|'_'|'::')+ ;

TYPEMOD 
 : '*'|'&' ;

The STRING lexer rule I did not change, but I did modify your arg and TYPEMOD rules and created a new parser rule typemod.

Oh yeah, I used the java target, so hopefully this will work for you.

Hope this helps, good luck.

WayneH
Thanks for the response, this did work. I just don't understand how come... isn't it the same thing pretty much as what I had before?
Steve
I would agree with you, the two are the same. The only difference is that with the working example a parser rule is optional while with the original code the lexer rule is optional. And really strange, for me AntlrWorks would not even let me debug your code.
WayneH