one question about iostream cout in C++ - c++

In such code, what it is called, \\n like this?
cout<<"Hello\\n \'world\'!";
What's the basic rule about such characters?

\n is an escape sequence to print a new line. Now if you want to print a \n ( a literal \n that is a slash followed by an n) on the screen you need to escape the \ like \\. So \\n will make \n print on the screen.

I suppose your question is about escape characters? They are a part of string literal declarations, not stream operations. See documentation for more details on escape sequences.
In particular: \n signifies new line, \t signifies TAB character, \" signifies a quote character.

In computing, we call those escape characters.

\n is a newline character; it signals the end of a line of text.
\\ is an escaped backslash, so it will print \. So \\n will just print a literal "\n" to the console.
For more information about C escape sequences, see Escape Sequences (MSDN).

Related

ANTLR4 - Need an explanation on this String Literals

On my assignment, I have this description for the String Lexer:
"String literals consist zero or more characters enclosed by double
quotes ("). Use escape sequences (listed below) to represent special
characters within a string. It is a compile-time error for a new line
or EOF character to appear inside a string literal.
All the supported escape sequences are as follows:
\b backspace
\f formfeed
\r carriage return
\n newline
\t horizontal tab
\" double quote
\ backslash
The following are valid examples of string literals:
"This is a string containing tab \t"
"He asked me: \"Where is John?\""
A string literal has a type of string."
And this is my String lexer:
STRINGLIT: '"'(('\\'('b'|'t'|'n'|'f'|'r'|'\"'|'\\'))|~('\n'))*'"';
Can anybody check for my lexer if it meets the requirement or not? If it's not, please tell me your correction, I don't really understand the requirement and ANTLR4.
With ANTLR4, instead of writing \\ ('b' | 't' | 'n'), you can write \\ [btn]. Also, as J Earls mentioned in a comment, you'll want to include the quote in your negated set, as well as the \r and the literal \.
This ought to do the trick:
STRINGLIT
: '"' ( '\\' [btnfr"'\\] | ~[\r\n\\"] )* '"'
;
try this:
QUOTE: '"';
STRINGLIT: QUOTE ( '\\' [bfrnt"\\] | ~[\b\f\r\n\t"\\] )* QUOTE
{self.text = self.text[1:-1]};

Printing "\" character in C++

This question may be silly but would be great if i understand the behavior.
I try to print
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
using a simple program
char testme [] ="\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\0";
cout<<"testme:"<<testme<<endl;
The out put in this case is
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
I intend to print 64 "\" characters, instead the output is 32 "\" characters.
There seems to be some thing that i am missing since the out put is exactly half.
Edit: The reason why i was asking is becasue , i have to ^ "\" to another char for HMAC encryption and i see some weird things.
in C++11 you can do like this...
char testme [] =R"(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\0)";
cout<<"testme:"<<testme<<endl;
The R"(...)" for Raw Character Strings...
To represent a backslash () in a string literal, we have to precede it with a backslash. To prevent errors (cos of too many backslash), C++ provides raw string literals...
This is called escaping and is a mechanism to insert certain characters into a string. For example, if you want to insert a citation mark into a string, you need to escape it.
char testme [] ="I am a so called \"programmer\".";
There's also \n, \t and other codes. However, this applies to \ itself, since you might want to be able to have a string that says \n without converting it into a newline character.
char testme [] ="This is a backslash followed by the letter n: \\n";
\\ is used to denote a single backslash: \. This is because \ is used in string literals to denote other symbols like \t for a tab, \n for a newline and \" for a quotation character.
So \\ gives you one backslash, \\\\ gives you two and so on.
To print a \ standard states that
C11; 6.4.4.4 Character constants:
The double-quote " and question-mark ? are representable either by themselves or by the
escape sequences \" and \?, respectively, but the single-quote ' and the backslash \
shall be represented, respectively, by the escape sequences \' and \\1.
That mean to print a \ you need an extra backslash \ . To print two \\ you need four backslash \\\\ and hence for 64 backslash you need 128 backslash.
1. Emphasis is mine.
\ is a special character known as Escape Character. For ex:: \n means newline character. So, if you want to print single \, you have to give \\. The first \ says the compiler to not treat the next \ as an escape character.
If it is C++, why not use string:
string testme(64, '\\');
cout << testme << endl;
The backslash \ is a very widespread escape character, and C++ also uses it like that. This means it's used to express special meaning (usually nonprintable characters). For example, to encode a line-feed character (ASCII 10) into a string, you express it as \n in the string literal. Another example, putting a single backslash at the end of a line (that is, before the line's terminating newline character) escapes the newline - so this way, you can continue a macro definition or //-style comment across several source file lines, and they will still count as one logical line.
This of course means that to get a literal backslash character, you have to escape the backslash itself, to get remove its "escape character" status. So typing \\ into a string literal yields a literal \ character.
That's why you get only half the amount of backslashes output - the C++ source code parser consumes two to produce one.
Didn't you notice one thing:
You printed 64 '\' but it printed only 32 of them.
Did you try 60, or 54, or some odd combi. say 33 ?
In C, '\' is escape character. You should have used '\n' for newline didn't you notice then, that '\' is not being printed.
To print '\' you must use '\\'.
A question for you:
Try printing 64 '%'. See what you get. Try understanding the reason for the output.

How to write regex express string literal in scala

String litertal consist zero or more character enclosed by double quote(").
Use escape sequences(listed below) to represent special characters within a string.
It is a compile-time error for a newline or EOF characterto appear inside a string literal.
All the supported escape sequences are as follow:
\b backspace
\f formfeed
\r carriage return
\n newline
\t tab
\" double quote
\ backslash
The following are valid examples of string literal:
" This is a string contain tab \t"
" Hello stackoverflow \"\b"
Can you help me write a regex match string literal?
Thanks so much.
The most general way is to use Pattern.quote() method which returns a regular expression that matches the literal string passed as its argument. You can use it in Scala as well as in Java.
If you want to match e.g. the string represented by the literal "contain tab \t", you would use the regexp "contain tab \t".r—so, there is no need for any special handling of TAB inside the regexp.

Vim regex not matching spaces in a character class

I'm using vim to do a search and replace with this command:
%s/lambda\s*{\([\n\s\S]\)*//gc
I'm trying to match for all word, endline and whitespace characters after a {. For instance, the entirety of this line should match:
lambda {
FactoryGirl.create ...
Instead, it only matches up to the newline and no spaces before FactoryGirl. I've tried manually replacing all the spaces before, just in case there were tab characters instead, but no dice. Can anyone explain why this doesn't work?
The \s is an atom for whitespace; \n, though it looks similar, syntactically is an escape sequence for a newline character. Inside the collection atom [...], you cannot include other atoms, only characters (including some special ones like \n. From :help /[]:
The following translations are accepted when the 'l' flag is not
included in 'cpoptions' {not in Vi}:
\e <Esc>
\t <Tab>
\r <CR> (NOT end-of-line!)
\b <BS>
\n line break, see above |/[\n]|
\d123 decimal number of character
\o40 octal number of character up to 0377
\x20 hexadecimal number of character up to 0xff
\u20AC hex. number of multibyte character up to 0xffff
\U1234 hex. number of multibyte character up to 0xffffffff
NOTE: The other backslash codes mentioned above do not work inside
[]!
So, either specify the whitespace characters literally [ \t\n...], use the corresponding character class expression [[:space:]...], or combine the atom with the collection via logical or \%(\s\|[...]\).
Vim interprets characters inside of the [ ... ] character classes differently. It's not literally, since that regex wouldn't fully match lambda {sss or lambda {\\\. What \s and \S are interpreted as...I still can't explain.
However, I was able to achieve nearly what I wanted with:
%s/lambda\s*{\([\n a-zA-z]\)*//gc
That ignores punctuation, which I wanted. This works, but is dangerous:
%s/lambda\s*{\([\n a-zA-z]\|.\)*//gc
Because adding on a character after the last character like } causes vim to hang while globbing. So my solution was to add the punctuation I needed into the character class.

How is \\n and \\\n interpreted by the expanded regular expression?

Within an ERE, a backslash character (\, \a, \b, \f, \n,
\r, \t, \v) is considered to begin an escape sequence.
Then I see \\n and [\\\n], I can guess though both \\n and [\\\n] here means \ followed by new line, but I'm confused by the exact process to interpret such sequence as how many \s are required at all?
UPDATE
I don't have problem understanding regex in programing languages so please make the context within the lexer.
[root# ]# echo "test\
> hi"
This is dependent on the programming language and on its string handling options.
For example, in Java strings, if you need a literal backslash in a string, you need to double it. So the regex \n must be written as "\\n". If you plan to match a backslash using a regex, then you need to escape it twice - once for Java's string handler, and once for the regex engine. So, to match \, the regex is \\, and the corresponding Java string is "\\\\".
Many programming languages have special "verbatim" or "raw" strings where you don't need to escape backslashes. So the regex \n can be written as a normal Python string as "\\n" or as a Python raw string as r"\n". The Python string "\n" is the actual newline character.
This can becoming confusing, because sometimes not escaping the backslash happens to work. For example the Python string "\d\n" happens to work as a regex that's intended to match a digit, followed by a newline. This is because \d isn't a recognized character escape sequence in Python strings, so it's kept as a literal \d and fed that way to the regex engine. The \n is translated to an actual newline, but that happens to match the newline in the string that the regex is tested against.
However, if you forget to escape a backslash where the resulting sequence is a valid character escape sequence, bad things happen. For example, the regex \bfoo\b matches an entire word foo (but it doesn't match the foo in foobar). If you write the regex string as "\bfoo\b", the \bs are translated into backspace characters by the string processor, so the regex engine is told to match <backspace>foo<backspace> which obviously will fail.
Solution: Always use verbatim strings where you have them (e. g. Python's r"...", .NET's #"...") or use regex literals where you have them (e. g. JavaScript's and Ruby's /.../). Or use RegexBuddy to automatically translate the regex for you into your language's special format.
To get back to your examples:
\\n as a regex means "Match a backslash, followed by n"
[\\\n] as a regex means "Match either a backslash or a newline character".
Actually regex string specified by string literal is processed by two compilers: programming language compiler and regexp compiler:
Original Compiled Regex compiled
"\n" NL NL
"\\n" '\'+'n' NL
"\\\n" '\'+NL NL
"\\\\n" '\'+'\'+'n' '\'+'n'
So you must use the shortest format "\n".
Code examples:
JavaScript:
'a\nb'.replace(RegExp("\n"),'<br>')
'a\nb'.replace(RegExp("\\n"),'<br>')
'a\nb'.replace(RegExp("\\\n"),'<br>')
but not:
'a\nb'.replace(/\\\n/,'<br>')
Java:
System.out.println("a\nb".replaceAll("\n","<br>"));
System.out.println("a\nb".replaceAll("\\n","<br>"));
System.out.println("a\nb".replaceAll("\\\n","<br>"));
Python:
str.join('<br>',regex.split('\n','a\nb'))
str.join('<br>',regex.split('\\n','a\nb'))
str.join('<br>',regex.split('\\\n','a\nb'))