Lex Yacc parser stuck at EOF
<p>I have been stuck with this problem for a couple of days now and so is this parser. Point of this parser is to parse http request and that it does ok but when the end of request is reached parser enters into infinite loop. I have located the point in the <strong>C</strong> file that is generated from lex file but have no idea how to solve this problem.</p>

<p>I have tried the following approaches suggested in other similar questions without success.</p>

<p><a href="https://stackoverflow.com/questions/1756275/bison-end-of-file">bison-end-of-file</a></p>

<p><a href="https://stackoverflow.com/questions/6614834/lex-flex-scanning-for-the-eof-character">lex-flex-scanning-for-the-eof-character</a></p>

<p>This is my lex file:</p>

<pre><code>#undef YYLMAX
#define YYLMAX  4096

#include "ssoyacc.h"

#define yylval  ssolval
extern YYSTYPE  yylval;

#ifdef FLEX_SCANNER
#define YY_INPUT(buf, result, max_size) { int cc = sso_read(); result = (cc == -1) ? YY_NULL : (buf[0] = cc, 1);}
#else /* NO FLEX */
#undef input
#define input()         sso_read()
#define unput(cc)         sso_unput(cc)
#define yyless(cc)         sso_yyless(cc)
#endif /* FLEX */

%}

%p 30000
%n 4000
%e 2000
%a 30000
%k 2500
%o 50000

nondigit                [_a-zA-Z]
alfanum                 [_a-zA-Z0-9]
digit                   [0-9]
nonzero_digit           [1-9]
octal_digit             [0-7]
hexadecimal_digit       [0-9a-fA-F]

%start HTTP QUERY ARG XML TAG CDAT FORM_PARAM FORM_VALUE

%%

&lt;INITIAL,HTTP&gt;[ ]   {
            return SP;
        }
&lt;INITIAL,HTTP&gt;\r\n      {
            return CRLF;
        }
&lt;HTTP&gt;HTTP\/{digit}\.{digit}    {
            return HTTP_VERSION;
        }
&lt;HTTP&gt;OPTIONS       {
            return OPTIONS;
        }
&lt;HTTP&gt;GET   {
            return GET;
        }
.
.
.
other tags
.
.
.
&lt;FORM_PARAM&gt;\=  {
            BEGIN(FORM_VALUE);
            return IS;
        }
&lt;FORM_VALUE&gt;\&amp;  {
            BEGIN(FORM_PARAM);
            return AMPERSAND;
        }
&lt;FORM_VALUE&gt;[0-9a-zA-Z\%\+\.\/]*    {
            if (yyleng &lt; MAX_ARG_LEN)
            {
                char cc[3];
                int ii;
                int jj = 0;
                for (ii=0;ii&lt;yyleng;ii++)
                {
                    if (yytext[ii] != '%')
                    {
                        if (yytext[ii] == '+')
                        {
                            yylval.sval[jj++] = ' ';
                        }
                        else
                        {
                            yylval.sval[jj++] = yytext[ii];
                        }
                    }
                    else
                    {
                        strncpy(cc, yytext+ii+1, 2);
                        cc[2] = 0;
                        yylval.sval[jj++] = strtol(cc, NULL, 16);
                        ii+=2;
                    }
                }
                yylval.sval[jj] = 0;
                return STRING;
            }
            else
            {
                return ERROR;
            }
        }
%%
int ssowrap(void)
{
        return 1;
}

void start_http()
{
    init_content(); /* initialize content count */
    BEGIN(HTTP);
}

void start_urlencoded()
{
    BEGIN(FORM_PARAM);
}

void start_xml()
{
    BEGIN(XML);
}


int sso_yyless(int count)
{
    int i;
    if (count&gt;yyleng)
    {
        return 0;
    }
    for (i=0;i&lt;yyleng-count;i++)
    {
        unput(yytext[yyleng-1-i]);
        yytext[yyleng-1-i] = '\0';
    }
    return 0;
}

void allprint(wchar_t cc)
{
    if (isprint(cc))
    {
        fprintf(stdout, "'%c' 0x%x", cc, cc);
    }
    else
    {
        fprintf(stdout, "%x", cc);
    }
}

void sprint(wchar_t *pc)
{
    fprintf(stdout, "%s", pc);
}
</code></pre>

<p>The execution gets stuck to loop <code>while ( /*CONSTCOND*/1 )</code> and keeps entering twice in <code>case YY_END_OF_BUFFER:</code> and then once in <code>case 126:</code> in ssolex.c file. The line that is stated in <code>case 126</code> is the line <code>%%</code> in lex file.</p>

<pre><code>/** The main scanner function which does all the work.
 */
YY_DECL
{
    yy_state_type yy_current_state;
    char *yy_cp, *yy_bp;
    int yy_act;

    if ( !(yy_init) )
        {
        (yy_init) = 1;

#ifdef YY_USER_INIT
        YY_USER_INIT;
#endif

        if ( ! (yy_start) )
            (yy_start) = 1; /* first start state */

        if ( ! ssoin )
            ssoin = stdin;

        if ( ! ssoout )
            ssoout = stdout;

        if ( ! YY_CURRENT_BUFFER ) {
            ssoensure_buffer_stack ();
            YY_CURRENT_BUFFER_LVALUE =
                sso_create_buffer(ssoin,YY_BUF_SIZE );
        }

        sso_load_buffer_state( );
        }

    {
#line 44 "ssolex.l"


#line 1265 "&lt;stdout&gt;"

    while ( /*CONSTCOND*/1 )        /* loops until end-of-file is reached */
        {
        yy_cp = (yy_c_buf_p);

        /* Support of ssotext. */
        *yy_cp = (yy_hold_char);

        /* yy_bp points to the position in yy_ch_buf of the start of
         * the current run.
         */
        yy_bp = yy_cp;

        yy_current_state = (yy_start);
        yy_current_state += YY_AT_BOL();
yy_match:
        do
            {
            YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)] ;
            if ( yy_accept[yy_current_state] )
                {
                (yy_last_accepting_state) = yy_current_state;
                (yy_last_accepting_cpos) = yy_cp;
                }
            while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
                {
                yy_current_state = (int) yy_def[yy_current_state];
                if ( yy_current_state &gt;= 802 )
                    yy_c = yy_meta[(unsigned int) yy_c];
                }
            yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
            ++yy_cp;
            }
        while ( yy_base[yy_current_state] != 1067 );

yy_find_action:
        yy_act = yy_accept[yy_current_state];
        if ( yy_act == 0 )
            { /* have to back up */
            yy_cp = (yy_last_accepting_cpos);
            yy_current_state = (yy_last_accepting_state);
            yy_act = yy_accept[yy_current_state];
            }

        YY_DO_BEFORE_ACTION;

do_action:  /* This label is used only to access EOF actions. */
</code></pre>

<p>Last thing that is printed is Reading a token: which is in yacc <strong>C</strong> file so I think that the problem must be EOF handling in lex.</p>

<pre><code>/* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol.  */
if (yychar == YYEMPTY)
{
  YYDPRINTF ((stderr, "Reading a token: "));
  yychar = yylex ();
  printf("TOKEN %c, %d\n", yychar, yychar);
}
</code></pre>