0

with reference to Reading new line giving syntax error in LEX YACC lex file we are using

 %{
        /*
            parser for ssa;
        */

    #include<stdio.h>
    #include<stdlib.h>
    #include"y.tab.h"


    %}
    %%
    [\t]+   ;
    \n  ;



    [if]+       printf("first input\n");
    [else]+     return(op);
    [=]+        return(equal);
    [+]+        return(op);
    [*]+        return(op);
    [-]+        return(op);

    [\<][b][b][ ]+[1-9][\>] {return(bblock);}

    ([[_][a-z]])|([a-z][_][0-9]+)|([0-9]+)  {return(var);}

    .   ;




    %%

what should i do if i want to get token as a string i.e a_2 how to do it????

input file is

a_2 = _6 + b_3; 
  a_8 = b_7 - c_5;

Selcuk
  • 57,004
  • 12
  • 102
  • 110
william
  • 3
  • 1
  • 4
  • You know that `[if]+` will match any non-empty string containing only the letters `f` and `i`, right? So it will match f, fififi, iffif, and a lot of other things which don't seem like what you're looking for. To be honest, I don't understand your usage the regex `+` operator at all. Why would you want "any number of `=`" to be a single token? – rici Mar 22 '14 at 17:36
  • yes,i know code needs to be modified......i posted code to get info about string token.....ill modify it....thanks dear :) – william Mar 23 '14 at 07:07

1 Answers1

1

You can define token type in your bison file:

%union{
 char *string;
}

%token <string> var

and then replace

return(var);

with

yylval.string=malloc(yyleng); sprintf(yylval.string,"%s",yytext);return var;
ziollek
  • 1,973
  • 1
  • 12
  • 10
  • 1
    The malloc() is one too short. The whole thing can be replaced by yylval.string = strdup(yytext); – user207421 Mar 22 '14 at 11:24
  • thanks ziollek, EJP. if i want VAR as parameter in a function in my yacc file, how can i do that, i have used $$,$1 but it is giving error.....cant i take VAR as a parameter in my function? – william Jun 03 '14 at 06:11
  • 1
    Sorry, but who is responsible for freeing that string after parsing is done? – firegurafiku Mar 26 '16 at 13:46