Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I make lex/yacc match strings longer than 9000?

'[^']*\'
I use this rule to make lex match strings, it works fine when the string length is less than 9000, so how do I get lex to match strings longer than 9,000

Whether I should change the rules? Or do I have to set something up? I wish someone could help me

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can change states from the predefined INITIAL state to some other state, SQSTR, when you encounter '. Within the SQSTR state, you switch back to INITIAL when you encounter an unescaped '. Otherwise, you stay in SQSTR and append characters to the token. How you optimally manage errors and string growth wrt memory allocation is an exercise left to the reader. Multi-line strings are also straightforward. And, of course, you should recognize an obvious refactoring opportunity which should be glaring red if you try to add multi-line string support.

%s SQSTR
%%

%{
   char *str;
   int len;
%}

<INITIAL>' {
    str = malloc(1);
    len = 0;
    *str = 0;
    BEGIN(SQSTR);
}
<SQSTR>\\' {
    str = realloc(str, len+1);
    str[len] = '\'';
    str[len+1] = 0;
    len++;
}
<SQSTR>. {
    str = realloc(str, len+1);
    str[len] = *yytext;
    str[len+1] = 0;
    len++;
}
<SQSTR>' {
    BEGIN(INITIAL);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading