Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Magic Behind Escape(\) Character

How does the C/C++ compiler manipulate the escape character ["\"] in source code? How is compiler grammar written for processing that character? What does the compiler do after encountering that character?

like image 651
Mahesh Avatar asked Nov 27 '08 10:11

Mahesh


People also ask

What do the \n and \t escape characters represent?

It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.

What does an escape character do?

In computing and telecommunication, an escape character is a character that invokes an alternative interpretation on the following characters in a character sequence. An escape character is a particular case of metacharacters.

Is backslash a special character?

As we've seen, a backslash \ is used to denote character classes, e.g. \d . So it's a special character in regexps (just like in regular strings).


2 Answers

Most compilers are divided into parts: the compiler front-end is called a lexical analyzer or a scanner. This part of the compiler reads the actual characters and creates tokens. It has a state machine which decides, upon seeing an escape character, whether it is genuine (for example when it appears inside a string) or it modifies the next character. The token is output accordingly as the escape character or some other token (such as a tab or a newline) to the next part of the compiler (the parser). The state machine can group several characters into a token.

like image 186
Yuval F Avatar answered Oct 20 '22 18:10

Yuval F


An interesting note on this subject is On Trusting Trust [PDF link].

The paper describes one way a compiler could handle this problem exactly, shows how the c-written-in-c compiler does not have an explicit translation of the codes into ASCII values; and how to bootstrap a new escape code into the compiler so that the understanding of the ASCII value for the new code is also implicit.

like image 33
dmckee --- ex-moderator kitten Avatar answered Oct 20 '22 17:10

dmckee --- ex-moderator kitten