getting cpp to insert a newline

J

jiversen

Hi all,

I'm trying to get a newline into preprocessed output and I'm not sure
how to do it, if it's at all possible.



For example:

#define _TEST i want this on one line / and this on the next
_TEST

---shoud give me:

i want this one one line
and this on the next



Is there a trigraph or some kind of escape sequence for this? I don't
see one but I may be wrong.

Thanks,
Jason
 
W

Walter Roberson

:I'm trying to get a newline into preprocessed output and I'm not sure
:how to do it, if it's at all possible.

:For example:

:#define _TEST i want this on one line / and this on the next
:_TEST

:---shoud give me:

:i want this one one line
:and this on the next

Sorry, you can't portably do it. By the time preprocessing is finished,
you are at the end of translation phase 4; phase 5 converts
escape literals into the execution character set [e.g., \b gets
converted into backspace character itself]; phase 6
concatenates adjacent string literals, and that takes you to phase 7.
At phase 7, whitespace is not significant. If you back-reason
from phase 7, you see that the result of preprocessing only has to
preserve enough information about whitespacing so that it doesn't
accidently create escape sequences that weren't already present.

To put it another way: the result of pre-processing only be
something with equivilent semantic meaning, and there is no case
in which splitting a line is semantically meaningful in C,
except for preprocessor facilities. However, the preprocessor
doesn't allow you to build up preprocessor facilities -- you cannot,
for example, use a #define to create something that, after parameter
replacement in the program, will result in a -new- #define being processed.

As I was firmly informed just last week, if you are trying to do
something in which the actual -form- of the output of preprocessing
matters, then you are trying to do something not supported by the
C preprocessor in any portable way.
 
J

jiversen

Thank you very much for your reply. Its makes perfect sense.

Its true, I'm attempting to use "cpp" for something thats not C - where
whitespace and newlines ARE significant.

Thanks again,
Jason
 
P

Peter Nilsson

jiversen said:
Hi all,

I'm trying to get a newline into preprocessed output and I'm
not sure how to do it, if it's at all possible.

The C language describes translation phases, not implementation
options like cpp. Typically, preprocessing is mostly translation
phases 1 through 4, but there's quite a gray area.
For example:

#define _TEST i want this on one line / and this on the next
_TEST

---shoud give me:

i want this one one line
and this on the next

Why are you bothering with cpp output?

What is the bigger problem that you're trying to solve?
Is there a trigraph or some kind of escape sequence for this?

No. Standard preprocessing directives are always on one logical
source line. The \ splice mechanism just joins physical source
lines.
 
J

jiversen

I'm doing miscellaneous scripting and I find the cpp macros quite
useful for a number of things. I'm trying to very easily lever the
usefulness of the #includes and #define(x) directives for a data file
that I parse later.

Is there another option for doing preprocessing that would maybe have
an extended syntax?
 
W

Walter Roberson

:Is there another option for doing preprocessing that would maybe have
:an extended syntax?

m4 is the usual answer.
 
A

Alan Balmer

Thank you very much for your reply. Its makes perfect sense.

Its true, I'm attempting to use "cpp" for something thats not C - where
whitespace and newlines ARE significant.
There are general purpose macro processors available. One that I've
used is called M4. Do a Google search for macro processor, and you'll
get links to some.
 
J

jiversen

I'm trying out m4 right now. Odd syntax with the backtick thing but I
think it'll do my bidding.

Thanks all.
 
C

Chris Croughton

Thank you very much for your reply. Its makes perfect sense.

Its true, I'm attempting to use "cpp" for something thats not C - where
whitespace and newlines ARE significant.

If you want something you can hack to get a similar effect, you could
look at my xcpp (http://www.keristor.co.uk/stuff/xcpp-0.5.5.tar.gz). It
doesn't implement #include files (that's not its intent) but it may be a
start. Or it may not, depending how unclear you find my code <g>...

Chris C
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top