Escape sequences and printing

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.
 
M

Matt Gregory

Kit said:
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.

You can do it much more easily with start states.

Matt Gregory
 
C

Colin Newell

The escape characters are a feature of the language, not the printf call.
The escape characters get expanded at compile time, not runtime so they will
not be of any use to you.
 
M

Matt Gregory

Colin said:
The escape characters are a feature of the language, not the printf call.
The escape characters get expanded at compile time, not runtime so they will
not be of any use to you.

But compile time is what he's talking about. He's using lex...
 
C

Colin Newell

He's trying to use printf to expand out a variable, it's definately an
attempt at a runtime hack.

Colin.
 
M

Matt Gregory

Colin said:
He's trying to use printf to expand out a variable, it's definately an
attempt at a runtime hack.

Oh, you're right, sorry. I must learn to pay attention.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top