K
Kit
Hey there, and thanks for reading a newbie post. I have a lex
assignment in which we are supposed to grab strings from a C source
file, and print them out to the screen, with escaped characters
"converted". Here's part of the assignment text:
----------------------------------------------------------------
Example:
main() {
char *s = "This is string one";
char *t = "This string has a quote (\"), an escape (\\),\nand a
newline.";
char this_is_not_a_string_constant = '"';
/* "This is not a string constant, either!" */
}
would produce the output
Line 2: This is string one
Line 3: This string has a quote ("), an escape (\),
and a newline.
Note that escaped characters are converted, and string constants in
comments are ignored.
-----------------------------------------------------------------
Now, I have it all working except the conversion of escaped
characters. I've been trying to get a substring search and replace
function working, to no avail. But then it occurred to me - if I'm
printing a character array with printf, shouldn't the escaped
characters be converted automatically? I mean, isn't that what
they're there for? Shouldn't \\ inside a string come out as a single
\ onscreen? And \" as a quote, and so on?
But that's not what I'm getting. I have the following line in my
code:
printf( "Line %d: %s\n", (numLines + 1), yytext );
When given the following for yytext:
/*\\\"**/\\\tr\"\\
It simply prints it back out exactly as above. I think I should get
something like:
/*\"**/\ r"\
What am I overlooking here?
Thanks in advance for your help,
Chris
PS - I should point out that I am not looking for a complete solution;
I want to do the work myself. But it would be nice to have a nudge in
the right direction.
assignment in which we are supposed to grab strings from a C source
file, and print them out to the screen, with escaped characters
"converted". Here's part of the assignment text:
----------------------------------------------------------------
Example:
main() {
char *s = "This is string one";
char *t = "This string has a quote (\"), an escape (\\),\nand a
newline.";
char this_is_not_a_string_constant = '"';
/* "This is not a string constant, either!" */
}
would produce the output
Line 2: This is string one
Line 3: This string has a quote ("), an escape (\),
and a newline.
Note that escaped characters are converted, and string constants in
comments are ignored.
-----------------------------------------------------------------
Now, I have it all working except the conversion of escaped
characters. I've been trying to get a substring search and replace
function working, to no avail. But then it occurred to me - if I'm
printing a character array with printf, shouldn't the escaped
characters be converted automatically? I mean, isn't that what
they're there for? Shouldn't \\ inside a string come out as a single
\ onscreen? And \" as a quote, and so on?
But that's not what I'm getting. I have the following line in my
code:
printf( "Line %d: %s\n", (numLines + 1), yytext );
When given the following for yytext:
/*\\\"**/\\\tr\"\\
It simply prints it back out exactly as above. I think I should get
something like:
/*\"**/\ r"\
What am I overlooking here?
Thanks in advance for your help,
Chris
PS - I should point out that I am not looking for a complete solution;
I want to do the work myself. But it would be nice to have a nudge in
the right direction.