11

I'm trying to generate a compiler so I can pass him a .c file after.

I've downloaded both YACC and LEX grammars from http://www.quut.com/c/ANSI-C-grammar-y.html and named them clexyacc.l and clexyacc.y

When generating it on terminal I did :

yacc -d clexyacc.y
lex clexyacc.l

All went fine. When I move on to the last part I get a few errors.

The last part is : cc lex.yy.c y.tab.c -oclexyacc.exe

But I get these errors :

y.tab.c:2261:16: warning: implicit declaration of function 'yylex' is invalid in
      C99 [-Wimplicit-function-declaration]
      yychar = YYLEX;
               ^
y.tab.c:1617:16: note: expanded from macro 'YYLEX'
# define YYLEX yylex ()
               ^
y.tab.c:2379:7: warning: implicit declaration of function 'yyerror' is invalid
      in C99 [-Wimplicit-function-declaration]
      yyerror (YY_("syntax error"));
      ^
clexyacc.y:530:6: error: conflicting types for 'yyerror'
void yyerror(const char *s)
     ^
y.tab.c:2379:7: note: previous implicit declaration is here
      yyerror (YY_("syntax error"));
      ^
2 warnings and 1 error generated.
user207421
  • 305,947
  • 44
  • 307
  • 483
ChasingCars
  • 125
  • 1
  • 1
  • 6
  • How do you define `yyerror` in your lexer file? See: http://stackoverflow.com/questions/15641256/bison-conflicting-type-for-yyerror – Joe May 17 '14 at 23:27
  • In my clex.l file I only have two references to yyerror which are : extern void yyerror(const char *); yyerror("unterminated comment"); – ChasingCars May 17 '14 at 23:31

1 Answers1

21

The version of yacc you are using is producing C code which is invalid for C99.

The code it produces does not include declarations for the functions yylex or yyerror prior to calling them. This is producing the warnings. In the case of yyerror, it is also resulting in an implicit declaration which does not match the later actual definition.

You can get around it by including the following at the top of the .y file:

%{
int yylex();
void yyerror(const char *s);
%}

Or, you can switch to a more modern yacc compiler.

See also this: Simple yacc grammars give an error

Community
  • 1
  • 1
harmic
  • 28,606
  • 5
  • 67
  • 91
  • I should be adding those to the top of my yacc file correct ? – ChasingCars May 17 '14 at 23:37
  • 1
    I've added "%{ int yylex(); void yyerror(const char *s); %}" at the top of my yacc file. When compiling the last part I now get : "Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)" – ChasingCars May 17 '14 at 23:40
  • You are trying to compile it to an executable, and there is no main function in your program. A parser/lexer by itself doesn't do anything, you need to write a main function that will pass it a file and do something with the parsing result. Or just compile it to an object file without linking and link it to an object containing main later (-c option for this). – harmic May 17 '14 at 23:42
  • Ohhh, alright. So you're telling me I should just stick to the part : "yacc -d clexyacc.y lex clexyacc.l" and afterwards compile it to a .c file ? – ChasingCars May 17 '14 at 23:44
  • I would still compile it to a .o file, otherwhise you do not know if it contains valid syntax. do that with: `cc -c lex.yy.c y.tab.c`. The -c option tells it to produce .o (object) files which you can later link to a .c file – harmic May 17 '14 at 23:48
  • Alright. Thanks so much, I've already done that and it has produced both the .o files (lex.yy.o and y.tab.o). Just one last question since I'm a bit nooby at all this right now. How can I use all this to parse a .c file now ? – ChasingCars May 17 '14 at 23:51
  • No. As per my earlier comment you need a main function. Sorry explaining about that is beyond the scope of a comment, but professor Google should be able to help – harmic May 17 '14 at 23:55