Академический Документы
Профессиональный Документы
Культура Документы
KEN THOMPSON
printf("char\ts[ ] = {kn");
for(i=0; s[i]; i++) c = next( );
printf("~t%d, \n", s[i]); if(c != ' \ V )
printf("%s", s); return(c);
I c = next( );
Here are some simple transliterations to allow if(c = = ' \ V )
a non-C programmer to read this code. return('\\');
= assignment if(c = = 'n')
== equal to .EQ. return('kn ');
!= not equal to .NE.
++ increment
'x' single character constant F I G U R E 2.2.
"xxx" multiple character string
%d format to convert to decimal
%s format to convert to string c = next( );
kt tab character if(c ~= '\v)
kn newline character return(c);
c = next( );
if(c = = ' \ V )
F I G U R E 1. return('kV);
if(c = = 'n')
STAGE II retum('kn');
The C compiler is written in C. What I am about to if(c = = 'v')
describe is one of m a n y "chicken and egg" problems return('\v');
that arise when compilers are written in their own lan-
guage. In this case, I will use a specific example from
the C compiler. F I G U R E 2.1.
C allows a string construct to specify an initialized
character array. The individual characters in the string c = next( );
can be escaped to represent unprintable characters• For if(c != ' \ V )
example, return(c);
c = next( );
"Hello w o r l d \ n " if(c == '\v)
represents a string with the character "\n," representing return('\\');
the new line character. if(c = = 'n')
Figure 2.1 is an idealization of the code in the C return('\ n');
compiler that interprets the character escape sequence. if(c = = 'v')
return(11 );
This is an amazing piece of code. It "knows" in a com-
pletely portable way what character code is compiled
for a new line in any character set. The act of knowing F I G U R E 2.3.