473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Escape sequences and printing

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.
Nov 13 '05 #1
5 4066
Kit wrote:
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

Nov 13 '05 #2
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.
Nov 13 '05 #3
Colin Newell wrote:
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...

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

Colin.
Nov 13 '05 #5
Colin Newell wrote:
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.

Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Joe | last post by:
I'm using Python 2.4 on Windows XP SP2. I'm trying to receive a command line argument that is a newline (\n) Here is the command line to use sample.py "\n" Here is a sample.py script
14
by: Jon Maz | last post by:
Hi, I have been getting hopelessly confused with escaping escape characters in JScript! All I want to do is write a simple funtion: function DoubleUpBackSlash(inputString) { ??????? }
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
3
by: Ken | last post by:
HI: I'm reading a string that will be displayed in a MessageBox from a resource file. The string in the resource file contains escape sequences so they will be broken up into multiple lines. ...
5
by: nummertolv | last post by:
Hi, My application is receiving strings, representing windows paths, from an external source. When using these paths, by for instance printing them using str() (print path), the backslashes are...
5
by: Anton81 | last post by:
Hi all! I used escape sequences to produce colour output, but a construct like print "%8s" % str_with_escape doesn't do the right thing. I suppose the padding counts the escape characters,...
5
by: chandanlinster | last post by:
consider the following program /****************************************************/ #include <stdio.h> #include <stdlib.h> int main(void) { int c;
4
by: JJ | last post by:
Is there a way of checking that a line with escape sequences in it, has no strings in it (apart from the escape sequences)? i.e. a line with \n\t\t\t\t\t\t\t\r\n would have no string in it a...
5
by: vlsidesign | last post by:
The printf function returns "warning: unknown escape sequence: \040" for a backslash-space combination. If the ascii decimal number for space is 32 and the backslash is 92, why this particular...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.