Related articles |
---|
How do I handle multiple line quoted strings in fLex? tperry@blinksoft.com (Tim Perry) (1997-09-03) |
Re: How do I handle multiple line quoted strings in fLex? hbabu@sequent.com (1997-09-07) |
Re: How do I handle multiple line quoted strings in fLex? tperry@blinksoft.com (Tim Perry) (1997-09-12) |
From: | hbabu@sequent.com (Haren Babu Myneni) |
Newsgroups: | comp.compilers |
Date: | 7 Sep 1997 15:04:03 -0400 |
Organization: | Sequent Computer Systems, Inc. |
References: | 97-09-011 |
Keywords: | lex |
Tim Perry <tperry@blinksoft.com> wrote:
>I wan't to be able to handle multiple line strings in flex, where I return
>one token to bison of that string minus the quotes. I have a pattern such
>as this that currently works, with a few snags...
>
>\"[^\"]*\" { yyval.string = strdup(yytext[yyleng]); RETURN STRING }
>
>This doen't get rid of the " nor does it supply a method of counting the
>current line numbers since the \n rule that I have will not get executed.
>
>I have thought about makeing a STRING state that when it encounters the
>first " I initialize a state that accepts everything besides a \n, I
>discard the \n since I don't want them in my string anyway, increase my
>linecounter and then strcat the yytext to a temp buffer till I get the
>closeing ".
>[I'd do what you suggest, use the STRING state to recognize a line at
>a time and buffer up the string myself, since it's unwise to expect
>lex to handle enormous single tokens. -John]
I was working a some parser which has similar string (String starts
with " and ending with ". It also takes any escape character.). I
used lex for lexical analyzer. I implemented the follwing way to
increment line number if '\n' occurs in the middle of the
string. Implementation is as follows.
\" {
yyleng = 0; c1=0; c2=0;
end_str = FALSE;
c1 = input();
c2 = c1;
while (!end_str) {
if (c1 == '"' && c2 != '\\') {
end_str = TRUE;
}
yytext[yyleng++] = c1;
if (c1 == '\n') {
lineno++;
}
c2 = c1;
c1 = input();
}
/*
* To remove the last quotation
*/
yytext[yyleng-1] = '\0';
yylval.sliteral = (char *)strdup(yytext);
unput(c1);
RETURN_VAL(LITERAL);
}
It is giving correct line numbers. I hope I understand your problem correctly.
Good luck
Haren Myneni
[This won't work in flex nor any other version of lex where yytext isn't an
array. -John]
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.