LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Im getting following errors while compiling my LEX program (https://www.linuxquestions.org/questions/linux-newbie-8/im-getting-following-errors-while-compiling-my-lex-program-4175427997/)

ashlin 09-19-2012 09:47 AM

Im getting following errors while compiling my LEX program
 
There are the errors I'm getting:
Quote:

editted.l: In function ‘main’:
editted.l:39:4: warning: passing argument 1 of ‘fprintf’ from incompatible pointer type [enabled by default]/usr/include/stdio.h:355:12: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘char *’
editted.l: In function ‘yywrap’:
editted.l:65:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
editted.l:65:12: error: expected expression before ‘:’ token

My code is as follows:

Code:

%{
        unsigned long charCount = 0, wordCount = 0, lineCount = 0;
        #undef yywrap
%}

word [^ \t\n]+
eol \n

%%
{word} { wordCount++; charCount += yyleng; }
{eol} { charCount++; lineCount++; }
.        charCount++;
%%


char **fileList;
unsigned currentFile = 0;
unsigned nFiles;
unsigned long totalCC = 0;
unsigned long totalWC = 0;
unsigned long totalLC = 0;

main(argc, argv)
int argc;
char **argv;
{
        FILE *file;

        fileList= argv+1;
        nFiles=argc-1;

        if (argc == 2)
        {
                currentFile = 1;
                file = fopen(argv[1], "r") ;
                if (!file)
                {
                        fprintf ("stderr,*could not open %s\n",argv[1]);
                        exit(1);
                }
                yyin=file;
        }

        if (argc > 2)
                yywrap(); /* open first file */

        yylex();
       
        if (argc > 2)
        {
                printf("%8lu %8lu %8lu %s\n", lineCount, wordCount, charCount, fileList [currentFile-1]);
                totalCC += charCount;
                totalWC += wordCount;
                totalLC += lineCount;
                printf("%8lu %8lu %8lu total\n",totalLC, totalWC, totalCC);
        }
        else
                printf("%8lu %8lu %8lu\n",lineCount,wordCount, charCount );
        return 0;
}

yywrap()
{
        FILE *file:
        if ( (currentFile ! = 0) && (nFiles > 1) && (currentFile < nFiles) )
        {
                printf("%8lu %8lu %8lu %s\n", lineCount, wordCount, charCount, fileList[CurrentFile-1]);
                totalCC += charCount;
                totalWC += wordCount;
                totalLC += lineCount;
                charCount = wordcount = lineCount = 0;
                fclose (yyin) ; /* done with that file */
        }

        while (fileList[currentFile] != (char *)0)
        {
                file = fopen(fileList[currentFile++], "r");
                if (file != NULL)
                {
                        yyin = file;
                        break;
                }
                fprintf ( stderr ,"could open %s\n", fileList[currentFile-1]);
        }
        return (file ? 0 : 1);
}


Snark1994 09-20-2012 05:54 AM

I don't know lex, but from C++ I'm guessing you need to write
Code:

fprintf (file,"stderr,*could not open %s\n",argv[1]);
and in the first line of yywrap change the ':' at the end of the line to a ';'

Ztcoracat 09-22-2012 01:41 AM

I've never compiled from source before but I did find some good instructions to learn how to.

If you ever need to re-compile a program you should do a <make clean> or (make dist clean)
This will delete files created during the previous compile and give you a clean tree.

The instructions also said:
If you encounter problems from source the error should clearly state what you are missing or why it is failing.

I found the instructions under "Tutorials" at the main page of linuxquestions written by Jeremy titled
Compiling Programs From Source
http://www.linuxquestions.org/linux/...ms_from_Source

Hope this helps

ashlin 09-24-2012 12:48 PM

Thank you! :)
 
Quote:

Originally Posted by Snark1994 (Post 4784965)
I don't know lex, but from C++ I'm guessing you need to write
Code:

fprintf (file,"stderr,*could not open %s\n",argv[1]);
and in the first line of yywrap change the ':' at the end of the line to a ';'


Thank you very much for your help. I have successfully edited my code.

Snark1994 09-25-2012 06:24 AM

Well done :) please mark the thread as 'SOLVED' (there should be a link at the top of the page)

Thanks,


All times are GMT -5. The time now is 08:33 AM.