rui314 / 8cc

A Small C Compiler
MIT License
6.12k stars 742 forks source link

failed to parse function return function pointers #64

Open minux opened 9 years ago

minux commented 9 years ago
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 {
andrewchambers commented 9 years ago

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

rui314 commented 9 years ago

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.

andrewchambers commented 9 years ago

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 commented 9 years ago

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