Comparison of programming languages (syntax): Difference between revisions
→Line continuation: C and C++ preprocessor, Visual Basic, Turbo Assembler, m4 |
→Line continuation: add missing parenthesis to m4 link |
||
Line 35: | Line 35: | ||
'''Some form of [[#Inline comments|inline comment]] serves as line continuation''' |
'''Some form of [[#Inline comments|inline comment]] serves as line continuation''' |
||
* [[Turbo Assembler]]: <code>\</code> |
* [[Turbo Assembler]]: <code>\</code> |
||
* [[M4 (computer language|m4]]: <code>dnl</code> |
* [[M4 (computer language)|m4]]: <code>dnl</code> |
||
== Blocks == |
== Blocks == |
Revision as of 18:27, 11 February 2007
Statements
Programming language statements typically have conventions for:
- statement separators;
- statement terminators; and
- line continuation
A statement separator is used to demarcate boundaries between two separate statements. A statement terminator is used to demarcate the end of an individual statement. Line continuation is a convention in languages where the newline character could potentially be misinterpreted as a statement terminator. In such languages, it allows a single statement to span more than just one line.
Statement terminators
Semicolon terminated:
Newline terminated:
Statement separators
Semicolon separated:
This section needs expansion. You can help by adding to it. |
Line continuation
This section needs expansion. You can help by adding to it. |
Backslash as last character of line
Underscore as last character of line
Some form of inline comment serves as line continuation
- Turbo Assembler:
\
- m4:
dnl
Blocks
A block is a notation for a group of two or more statements, expressions or other units of code that are related in such a way as to comprise a whole. In a curly bracket programming language this is done with braces, while e.g. Pascal uses "begin" and "end".
Comments
Comments can be classified by:
- style (inline/block)
- parse rules (ignored/interpolated/stored in memory)
- recursivity (nestable/non-nestable)
- uses (docstrings/throwaway comments/other)
Inline comments
Inline comments are generally those that use a newline character to indicate the end of a comment, and an arbitrary delimiter or sequence of tokens to indicate the beginning of a comment.
Examples:
# ;; Perl, Python, Ruby, Windows PowerShell, PHP % ;; TeX, Prolog, MATLAB // ;; C, C++, C#, Java, JavaScript, PHP ' ;; Visual Basic, VBscript ; ;; Autoit, AutoHotkey, many assemblers -- ;; Euphoria, Haskell, SQL
Block comments
Block comments are generally those that use a delimiter to indicate the beginning of a comment, and another delimiter to indicate the end of a comment. In this context, whitespace and newline characters are not counted as delimiters.
Examples:
/* */ ;; JavaScript, C, C++, Java, C#, PHP
""" """ ;; Python[1]
''' ''' ;; Python[1]
{- -} ;; Haskell
(* *) ;; ML, Mathematica
Unique variants
Fortran:
- The indentation of lines in FORTRAN 66/77 is significant; a whole-line comment in FORTRAN is introduced by any non-numeric character in column 1.
C
is the traditional way to introduce a comment in FORTRAN. (Fortran 90 removed the need for the indentation rule and added traditional inline comments, using the!
character as the comment delimiter.)
Perl:
- Block comments in Perl are considered part of the documentation, and are given the name Plain Old Documentation (POD). Technically, Perl does not have a convention for including block comments in source code, but POD is routinely used as a workaround.
Python:
- The use of the triple-(double)quotes although sometimes used to comment-out lines of source, does not actually form a comment. The enclosed text becomes a string, usually a string statement. Python usually ignores a lone string as a statement (except when a string is the first statement in the body of a module, class or function; see docstring).
Esoteric languages:
- Many esoteric programming languages follow the convention that any text not executed by the instruction pointer (e.g., Befunge) or otherwise assigned a meaning (e.g., Brainfuck, ETA) is considered a "comment".
Comment comparison
There is a wide variety of syntax styles for declaring comments in source code.
BlockComment
in italics is used here to indicate block comment style.
InlineComment
in italics is used here to indicate inline comment style.
- ActionScript:
/* BlockComment */
// InlineComment
- Ada, Eiffel, Euphoria, Lua, Occam, SPARK, ANSI SQL, ToolBook OpenScript, and VHDL:
-- InlineComment
- ALGOL 60:
comment BlockComment;
- ALGOL 68:
¢ BlockComment ¢
comment BlockComment comment
co BlockComment co
# BlockComment #
£ BlockComment £
- AppleScript:
(* BlockComment *)
-- InlineComment
- Assembly language: (varies)
; InlineComment
one example (most assembly languages use line comments only)
- AWK, Bash, Bourne shell, C shell, Maple, Python, R, Tcl, and Windows PowerShell:
# InlineComment
- BASIC (various dialects):
'InlineComment
(not all dialects)REM InlineComment
- C (K&R, ANSI/C89/C90), CHILL, CSS, PL/I, and REXX:
/* BlockComment */
- C (C99), C++, C#, and JavaScript:
/* BlockComment */
// InlineComment
- D:
/* BlockComment */
// InlineComment
/+ BlockComment +/
- Delphi (Object Pascal):
(* BlockComment *)
{ BlockComment }
// InlineComment
- DCL:
$! InlineComment
- Forth:
( BlockComment )
\ InlineComment
- FORTRAN:
C InlineComment
(any character, other than a number, in the first column, makes the entire line a comment)
- Fortran 90:
! InlineComment
(all characters on the line, after the exclamation mark, are comments)
- HTML (see SGML below)
- Java:
/** BlockComment */
(Javadoc documentation comment)/* BlockComment */
// InlineComment
- Lisp and Scheme
#| BlockComment |#
; InlineComment
- Matlab:
% BlockComment;
%{
Use this to comment out multiple lines without placing a "%" in front of every single one.
Nothing besides %{ must appear on that line
%}
- Mediawiki:
- <!-- BlockComment -->
- Pascal, Modula-2, Modula-3, Oberon, and ML:
(* BlockComment *)
- Perl and Ruby:
# InlineComment
=begin
BlockComment
=end__END__
Comments after end of code
- PHP:
# InlineComment
// InlineComment
/* BlockComment */
- PILOT:
R:InlineComment
- PL/SQL and TSQL:
/* BlockComment */
-- InlineComment
- REALbasic:
' InlineComment
// InlineComment
rem InlineComment
- SAS:
* BlockComment;
/* BlockComment */
- Seed7:
(* BlockComment *)
# InlineComment
- SGML, including HTML:
- A comment declaration starts with
<!
, followed by zero or more comments, followed by>
. A comment starts and ends with--
, and does not contain any occurrence of--
. Valid examples are:
<!-- BlockComment -- -- BlockComment -->
,<!------ BlockComment -->
, or<!>
.
- A comment declaration starts with
- Smalltalk:
"BlockComment"
- Standard ML:
(* BlockComment *)
- TeX, LaTeX, PostScript, and Erlang:
% InlineComment
- Texinfo:
@c InlineComment
@comment InlineComment
- TUTOR:
* BlockComment
command $$ BlockComment
- Visual Basic:
'InlineComment
Rem InlineComment
- XML, including XHTML:
<!--BlockComment-->
(comment must not contain--
and must not start or end with single-
)
References
- ^ a b More precisely, the quoted text forms a string literal.