8cc icon indicating copy to clipboard operation
8cc copied to clipboard

failed to parse function return function pointers

Open minux opened this issue 10 years ago • 4 comments

void (*f(int *p))(void) {
    return (void *)0;
}
$ 8cc -c t2.c 
[ERROR] 8cc.h:426: t.c:1:25: ';' or ',' are expected, but got {

minux avatar May 08 '15 02:05 minux

Looks like is_funcdef() in parse.c makes some incorrect assumptions so it tries to parse this as a decl.

andrewchambers avatar May 08 '15 02:05 andrewchambers

That's right. is_funcdef returns a wrong result for the input. Maybe we should stop making a guess before calling the parser but instead call the parser directly, because making a good guess is as hard as parsing input.

rui314 avatar May 08 '15 04:05 rui314

The look ahead is good for readability. If we don't have the ability to save and reset the parser at choice points on an error, decl and function parsing may need to be combined into an ugly mess.

andrewchambers avatar May 08 '15 04:05 andrewchambers

It actually might not be too bad, I think it comes down to a check in read_decl like:

decl = read_decl()
if ( peek == '{' && ! first decl in list  ) 
  error("function cannot appear in decl list");
if (peek == '{') { 
  if (sclass == typdef)
    error()
  parse_body() 
} 

Edit: Yeah, this is what I am currently doing (my function is too big), but I don't handle old style arguments. https://github.com/andrewchambers/cc/blob/master/parse/parse.go#L547

andrewchambers avatar May 08 '15 05:05 andrewchambers