0
votes

I am currently getting the error message:

In file included from /usr/include/errno.h:35:0,
                 from lex.yy.c:21:
/usr/include/x86_64-linux-gnu/bits/errno.h:50:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘extern’
 extern int *__errno_location (void) __THROW __attribute__ ((__const__));
 ^

(along with many others) when attempting a to compile a file generated by lex using the command:

gcc lex.yy.c

lex.l (file passed to lex to generate lex.yy.c) source:

%%
"hello world"   printf("goodbye");
.               ;
%%

This problem occurs when I try to compile any file which has been generated by lex or flex (I've tried both)

As I mentioned there are also many more errors but maybe fixing this one will solve some of the others. After looking for some common errors with errno.h and finding nothing of any use, I'm asking here.

I am using Ubuntu 16.04 LTS. Let me know if you would like/need more information regarding the problem and I'll do my best to help.

Thanks for any advice :)

Edit for 'rici':

The first 21 lines of my lex.yy.c file are as follows:

    #line 3 "lex.yy.c"

    #define  YY_INT_ALIGNED short int

    /* A lexical scanner generated by flex */

    #define FLEX_SCANNER
    #define YY_FLEX_MAJOR_VERSION 2
    #define YY_FLEX_MINOR_VERSION 6
    #define YY_FLEX_SUBMINOR_VERSION 0
    #if YY_FLEX_SUBMINOR_VERSION > 0
    #define FLEX_BETA
    #endif

    /* First, we deal with  platform-specific or compiler-specific issues. */

    /* begin standard C headers. */
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #include <stdlib.h>
    /* end standard C headers. */

Edit for @sepp2k:

I used vimdiff to compare the 2 files.

Things that are in my file but aren't in yours:

#ifdef __cplusplus

/* The "const" storage-class-modifier is valid. */
#define YY_USE_CONST

#else   /* ! __cplusplus */

/* C99 requires __STDC__ to be defined as 1. */
#if defined (__STDC__)

#define YY_USE_CONST

#endif  /* defined (__STDC__) */
#endif  /* ! __cplusplus */

#ifdef YY_USE_CONST

===============================================================

#include <string.h>
#include <errno.h>
#include <stdlib.h>
#line 476 "lex.yy.c"

There is nothing really which is in your file that is not in mine

Any other differences seem to just be differences of formatting.

I also tested the four header files in a standard C program (see what you mean now) and I can confirm it is errno.h which is causing the error.

A hello world in C with errno.h included threw up the following error:

In file included from /usr/include/errno.h:35:0,
                 from test.c:2:
/usr/include/x86_64-linux-gnu/bits/errno.h:50:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘extern’
 extern int *__errno_location (void) __THROW __attribute__ ((__const__));
 ^
In file included from test.c:4:0:
/usr/include/stdio.h:13:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
 int printf(const char* __restrict, ...);
 ^

edit for @rici:

Here is the full dump of errors thrown when i run "gcc lex.yy.c": https://gist.github.com/raygarner/0601e57f5be21e16e0ae4ee34643b121

edit for @sepp2k:

earlier on, i tested this exact same compilation process on a fresh install of debian 9 in a VM and I got the exact same error I am doing here, on Ubuntu after fixing errno.h

Here is what it looks like:

/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
1
I can't reproduce this on my system - your code compiles fine for me when I run it through flex and then gcc -c. Are those the exact contents of your lex file? Can you show us what the generated lex.yy.c looks like? Can you compile a normal C program like hello world? - sepp2k
Hello. That is the complete code for the file I pass to (f)lex. Normal C programs compile with no issues. The -c flag make no difference for me. I will post the full source for lex.yy.c in my original question. - RAY
Hello. The source for lex.yy.c is actually too long for me to post here (this file is probably the issue here). Could you post the contents of your lex.yy.c file? - RAY
What are the first 21 lines of lex.yy.c? - rici
I've posted the first 21 lines of the lex.yy.c source to my original question - RAY

1 Answers

0
votes

As we've found out in the comments, the original error was due to a messed up errno.h on your system and you fixed that by reinstalling the file.

With that solved, your code will compile fine, but won't link as a standalone application because of missing yywrap and main functions. You can fix the former by defining yywrap or using %option noyywrap. The latter can be fixed by defining a main function or linking against an object file that defines main (if the lexer is part of a larger project that already defines its own main function).

You can also fix both problems by linking -lfl, which defines the yywrap and main functions to read either from stdin or the file names from argv and then prints the resulting tokens to stdout. Of course that's only useful for testing purposes as you'll want to do more than that in your main in real projects.