Generating a compiler from lex and yacc grammar
Asked Answered
O

1

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.
Ornamentation answered 17/5, 2014 at 23:17 Comment(2)
How do you define yyerror in your lexer file? See: #15641756Laclos
In my clex.l file I only have two references to yyerror which are : extern void yyerror(const char *); yyerror("unterminated comment");Ornamentation
T
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

Thrippence answered 17/5, 2014 at 23:30 Comment(7)
I should be adding those to the top of my yacc file correct ?Ornamentation
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)"Ornamentation
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).Thrippence
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 ?Ornamentation
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 fileThrippence
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 ?Ornamentation
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 helpThrippence

© 2022 - 2024 — McMap. All rights reserved.