The Department of Computer Science & Engineering |
STUART C. SHAPIRO: CSE
305
|
Before discussing what a name looks like, we need to discuss what aren't names:
C
or
*
in column 1 to indicate that the line is a comment.
Other languages typically have one comment symbol to indicate that the rest of the line is a comment, and a pair of brackets to indicate that the enclosing material is a comment. For example,
Language | Rest of Line Comment | Open Comment | Close Comment |
---|---|---|---|
C | // |
/* | */ |
C++ | // |
/* | */ |
Fortran90 | ! |
| |
Java | // |
/* | */ |
Common Lisp | ; |
#| | |# |
Perl | # |
= | =cut |
Prolog | % |
/* | */ |
There are also common practices, which IDE's and tools are sometimes
sensitive to. Such as, in Java, the open comment bracket
/**
begins a JavaDoc comment. And in Lisp
;;;
is used at the beginning of a line, for comments
that are outside any function definition;
;;
is used at the beginning of an indented line
(indented like other lines of code), for comments within a function
definition
;
is used after, but on the same line as normal code,
to comment on that line of code.
Fortran, however, treats the newline as indicating the end of a
statement, unless it is continued by any character other than a blank
or 0
in column 6. Fortran also ignores spaces (blanks).
For example in the Do statement,
if the comma is omitted, the statement will be interpreted as the assignment statementDo 50 n = 1, 9999
Do50n = 19999
a[i]
, the brackets separate the tokens a
and
i
and prevent the expression from looking like the
identifier ai
.
Operators usually separate other tokens. For example, in Java,= > < ! ~ ? : == <= >= != && || ++ -- + - * / & | ^ % << >> >>> += -= *= /= &= |= ^= %= <<= >>= >>>=
x+y
is the same as x + y
. In Lisp, however,
most of these symbols are ordinary characters, so that while (+
x y)
is an expression that evaluates to the sum of
x
and y
, (+xy)
is a call to the
function of no arguments whose name is +xy
.
5
and
78.34
, but many languages have literals of other types,
such as the Java boolean literals true
and
false
, and Java's null
.
There is generally an involved syntax for numeric literals,
including optional signs, decimal points, exponentiation marks, and
radix indicators. For example, in C++ and Java 0x57
is a
hexadecimal integer equal to the decimal integer 87
, and
in Lisp, -3745e-2
is a floating point number equal to
-37.45
. Lisp also has literals of a ratio type, such as
3/5
.
"An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter. An identifier cannot have the same spelling (Unicode character sequence) as a keyword, boolean literal, or the null literal." [The Java Standard, Section 3.8.]In Fortran77, a name may only be 1-6 letters and/or digits, the first of which must be a letter. Fortran90 allows names up to 31 characters long, and allows them to include the
_
character.
Common Lisp allows names to be of arbitrary length, and treats as a
name any token that cannot be interpreted as a number. So Lisp names
include
Also, the symbols that are operators in other languages, such as1+ /5 ^/- 734ff 89..93
+
and >
are names in Common Lisp.
In fact, Common Lisp treats any character preceded by the escape
character \
to be an alphabetic character. So the
following are also Lisp names
and evenab\(c quo\"te
several\ words\ strung\ together
, which includes
internal spaces. Even the newline character may be included in a Lisp
name if preceded by an escape character.
Common Lisp also includes
escape brackets:
|several words strung together|
is the same name as
several\ words\ strung\ together
Moreover, Common Lisp puts the attributes of characters in the
control of the programmer. For example, the programmer could make
(
and )
be considered simple alphabetic
characters, and make [
and ]
serve the role
(
and )
normally do.
Languages also differ about the significance of upper- and
lower-case letters. Most modern languages distinguish between them.
So HashTable
is a different name from Hashtable
.
Prolog considers a name that starts with an upper-case letter to be a variable, while one that begins with a lower-case letter is considered to be a literal symbol.
In Perl, every variable name must start with a "funny character".
The name of a scalar variable, such as one that stores a number or
string, must start with a $
, such as $x
.
The name of a variable whose value is an array must start with an
@
, such as @monthTable
. The name of a
variable whose value is a hash table, called simply a "hash", must
start with a %
, such as %addressBook
.
Fortran allows lower-case letters in names, but considers them equivalent to the upper-case version.
Versions of Common Lisp before ACL 6 differentiated upper-case from lower-case letters, but automatically upper-cased non-escaped lower-case letters.
Although Emacs-Lisp is not a version of Common Lisp, like ACL 6, it differentiates upper- from lower-case letters, and does not change either to the other.