user2114865
user2114865

Reputation: 15

recursion in parsing

Here are input file, .l file , .y file and output.

problem is that parser is not able to identify the directions recursively..

it is identifying just first...

i've used same rule for recognizing ports and its working

but not in case of direction..

also it is not displaying .y file code associated with rule(cout statement)

input file .

start a b c d                //ports

a:O b:I c:B d:O    //direction of ports

.l file

[\t]+                   {}

[\n]  {line_num++; cout"line_num:" line_num; }

start                   {  cout< "beggining of file"; return START;}



[a-zA-Z0-9_\-]+:[IOB]    {cout<<"\ndirection:" << strdup(yytext); return DR; }

[a-zA-Z0-9_\-]+     {cout<<"\nfound name:" strdup(yytext); return NAME;}

.y file grammer

doc : START ports dir

ports : NAME ports  { cout<<"\port in .y" $1;}

        | NAME    { cout<<"\nport in .y" $1;}
        ;


dir : DR dir       { cout<<"\ndirection in .y" $1;}

    | DR            { cout<<"\ndirection in .y"<<$1;    }
    ;

output is .

beginning of file

found name:a

found name:b

found name:c 

found name:d

 line no-2

direction:a:O

Upvotes: 0

Views: 298

Answers (1)

rici
rici

Reputation: 241861

The only clear error you're making is that you are not setting the value of yylval in your flex actions, so $1 is some uninitialized value in all of your bison actions. Your flex actions should look something like this:

[a-zA-Z0-9_\-]+     { yylval = strdup(text);
                      cout  <<  "\nfound name:" << yylval;
                      return NAME;
                    }

Also, make sure you specify that the type of the tokens DR and NAME is const char *.

Finally, don't forget to free() the strings when you don't need them any more.

Upvotes: 1

Related Questions