Creating a compiler from lex and yacc grammar

I am trying to generate a compiler so I can pass the .c file to it later.

I downloaded the 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 on the terminal, I did:

yacc -d clexyacc.y
lex clexyacc.l

Everything went fine. When I go to the last part, I get a few errors.

Last part: 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.
+4
source share
1 answer

In the yacc version, you create C code that is not valid for C99.

, , yylex yyerror . . yyerror , .

, .y:

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

yacc.

: yacc

+8

Source: https://habr.com/ru/post/1540994/


All Articles