0

I am implementing a compiler and one thing I'd like to do is the string concatenation using '+', eg:

str_cnct = "hi" + "dear"

So the value now is "hidear".

The problem is that my regex in flex captures all of it directly as a string giving "hi + dear". My current regex is: \".*\"

{string}                {
                            yylval.struct_val.val.chain = (char *)malloc(sizeof(char)*yyleng);
                            strncpy(yylval.struct_val.val.chain,yytext,yyleng);
                            remove_char(yylval.struct_val.val.chain);
                            yylval.struct_val.length = yyleng;
                            yylval.struct_val.line = yylineno;
                            yylval.struct_val.column = columnno + yyleng + 2;
                            printf("--- String: %s\n", yylval.struct_val.val.chain);
                            return(STRING);
                    }

How to avoid this and capture "hi" then '+' as operator and then "dear"?

Thanks in advance

nwellnhof
  • 32,319
  • 7
  • 89
  • 113
jbondia
  • 253
  • 2
  • 7

2 Answers2

0

Try something like the following:

^\"([^\"]*)\"\s*\+\s*\"([^\"]*)\"$

$1 will capture "hi" w/o quotes and $2 will capture "dear" w/o quotes for string '"hi" + "dear"'.

crazyh
  • 325
  • 2
  • 10
  • Thank you! It worked but in a few situations it gave me some problems. E.g, it does not accept characters like '!', spaces, etc. I posted the way I finally could accomplish my target. – jbondia Jan 10 '14 at 09:55
0

I finally went through it like this:

%x MATCH_STR
quotes \"
%%

{quotes}                { BEGIN(MATCH_STR); }

<MATCH_STR>[\n]         { yyerror("String not closed"); }

<MATCH_STR>[^"^\n]*     {
                        yylval.struct_val.val.chain = (char *)malloc(sizeof(char)*yyleng);
                        strncpy(yylval.struct_val.val.chain,yytext,yyleng);
                        remove_char(yylval.struct_val.val.chain);
                        yylval.struct_val.length = yyleng;
                        yylval.struct_val.line = yylineno;
                        yylval.struct_val.column = columnno + yyleng + 2;
                        printf("--- String: %s\n", yylval.struct_val.val.chain);
                        return(STRING);
                        }

<MATCH_STR>{quotes}     { BEGIN(INITIAL); }
jbondia
  • 253
  • 2
  • 7