473,770 Members | 2,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_s tring_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 4086
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_s tring_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
3456
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
3545
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
96325
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 %% should be used. Wouldn't it have been better (from design perspective) if the same escape character had been used in this case too. Forgive me for posting without verfying things with any standard compiler, i don't have the means for now.
3
5725
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. e.g. This is line 1\n\nThis is line 2. When this string is read using a ResourceManager GetString method the string is returned @-quoted, i.e as if I entered the literal @"This is line1\n\nThis is line 2", causing the escape sequences not to be...
5
3103
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 naturally interpreted as escape characters. >>> print "d:\thedir" d: hedir
5
2369
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, too. What could be a solution?
5
2542
by: chandanlinster | last post by:
consider the following program /****************************************************/ #include <stdio.h> #include <stdlib.h> int main(void) { int c;
4
4280
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 line with \n\t\t\t\thello\t\t\n would hve the string 'hello' in it. In others words, is there a method of removing all escape sequences from a string? I've tried Regex.Unescape(string) but this doesn't not seem to remove the
5
15693
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 number 040? Is it a decimal number from the ASCII code chart? (compiling using gcc on SunOS 5.8, Sparc, Ultra-80) Just curious. Thanks.
0
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10057
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9869
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.