Search code examples
lexflex-lexer

.... undeclared (first use in this function)?


I have a simple code in lex language and i generate lex.yy.c with Flex. when i want to compile lex.yy.c to .exe file i get some error like "undeclared (first use in this function) " ! when i search in web i understand i need a Const.h file, so i want to generate that file. how i can do this ?

Some Errors:

35 C:\Users\Majid\Desktop\win\lex.l STRING' undeclared (first use in this function) 38 C:\Users\Majid\Desktop\win\lex.lLC' undeclared (first use in this function) 39 C:\Users\Majid\Desktop\win\lex.l `LP' undeclared (first use in this function) ....

Beginnig of The Code is :

%{int stk[20],stk1[20];
int lbl=0,wlbl=0;
int lineno=0;
int pcount=1;
int lcount=0,wlcount=0;
int token=100;
int dtype=0;
int count=0;
int fexe=0;
char c,str[20],str1[10],idename[10];
char a[100];
void eatcom();
void eatWS();
int set(int);
void check(char);
void checkop();
int chfunction(char *);%}

 Digit  [0-9]
 Letter [a-zA-Z]
ID     {letter}({letter}|{digit})*
NUM    {digit}+
 Delim  [ \t]
 A     [A-Za-z]]
%%



 "/*"               eatcom();
 "//"(.)*
 \"(\\.|[^\"])*\"       return (STRING);
 \"(\\.|[^\"])*\n       printf("Adding missing \" to sting constant");

 "{"                {a[count++]='{';fexe=0;eatWS();return LC;}
 "("                {a[count++]='(';eatWS();return LP;}
 "["                {a[count++]='[';eatWS();return LB;}
 "}"                {check('{');eatWS();
                if(cflag)
                {
                    //stk[lbl++]=lcount++;
                    fprintf(fc,"lbl%d:\n",stk[--lbl]);
                    //stk[lbl++]=lcount++;
                    printf("%d\n",stk[lbl]);
                    cflag=0;
                }
                return RC;
            }
    "]"             {check('[');eatWS();return RB;}
    ")"             {check('(');eatWS();return RP;}
    "++" | "--"         return INCOP;
    [~!]                return UNOP;
     "*"                {eatWS();return STAR;}
    [/%]            {eatWS();return DIVOP;}
    "+"             {eatWS();return PLUS;}
     "-"                {eatWS();return MINUS;}

Solution

  • You need a .h file with the definitions. You can write it by hand, but typically this file is generated by Bison. The two tools Flex and Bison are very often used together.

    Bison is a parser-generator. Its input is a file where you have written a grammar that describes the syntax of a language, and Bison generates a parser. The parser (or "syntactical analyzer") is the part of a compiler (or similar tool) that analyzes input according to the syntax of the language. For example, it is the parser that knows that an if statement can, but doesn't have to, have an else part.

    Flex is a scanner-generator, and from a file with regular expressions it creates a scanner. The scanner (or "lexical analyzer") is the part of a compiler (or similar tool) that analyzes input and divides it up into "tokens". A token can be a keyword such as if, an operator such as +, an integer constant, etcetera. It is the scanner that for example knows that an integer constant is written as a sequence of one or more digits.

    The scanner reports to the parser when it has found a token. For example, if the input starts with 123, the scanner might recognize that this is an integer constant, and report this to the parser. In the case of Flex and Bison, it does this by returning the token code for integer constant, which might (just as an example) be 17. But since the scanner and parser must agree on these token codes, they need common definitions. Bison will generate token codes, and if given the flag -d it will output them in a .h file.

    Thomas Niemann's A Compact Guide to Lex & Yacc gives a good introduction to how to use Flex and Bison. (Lex and Yacc are the old, original tools, and Flex and Bison are new, free versions of the same tools.)