Regular Expression Matching String

I am implementing a compiler, and one thing I would like to do is concatenate strings using "+", for example:

str_cnct = "hi" + "dear" 

So now the value is "hidear".

The problem is that my regular expression in flex captures all this directly as a string giving "hi + dear". My current regular expression 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 “hello” and then “+” as the operator and then “dear”?

Thank you in advance

0
source share
2 answers

Try the following:

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

$ 1 will capture “hello” without quotes, and $ 2 will capture “dears” without quotes for the string “hello” + “dear”.

0
source

Finally I went through 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); } 
0
source

Source: https://habr.com/ru/post/1382345/


All Articles