Comparison of programming languages (syntax)
This article needs additional citations for verification. Please help improve this article by adding reliable references. Unsourced material may be challenged and removed. (October 2009) |
Expressions
Programming language expressions can be broadly classified in three classes:
prefix notation
- Lisp (* (+ 2 3) (expt 4 5))
infix notation
suffix, postfix, or Reverse Polish notation
- Forth 2 3 + 4 5 ** *
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:
- C, C++, C#, Objective-C, Perl, D, Java, PHP, Javascript (semicolon optional), S-Lang, MATLAB (result display suppressed)
Newline terminated:
- AutoHotkey, BASIC, GFA BASIC, Visual Basic, Visual Basic .NET, Fortran, Ruby, Python, AppleScript, Windows PowerShell, Boo, MATLAB (result displayed)
Period terminated:
Statement separators
Semicolon separated:
- ALGOL, Pascal, Object Pascal (Delphi), Javascript, Windows PowerShell, Perl, S-Lang, OCaml, Haskell (in do-notation)
- (Secondary) FORTRAN[1], Eiffel[1], Python[1]
Period separated:
Colon separated:
Whitespace separated:
Newline separated:
- Haskell (in do-notation, when braces are omitted)
Semicolon and comma separated:
- ALGOL 68 has three different kinds of clauses, each separates phrases and the units differently:
- serial-clause using go-on-token (viz. semicolon): begin a; b; c end - units are executed in order.
- collateral-clause using and-also-token (viz. “,”): begin a, b, c end - order of execution is to be optimised by the compiler.
- parallel-clause using and-also-token (viz. “,”): par begin a, b, c end - units must be run in parallel threads.
- MATLAB allows multiple statements on one line provided they are separated by a semicolon (to hide result of preceding statement) or a comma (to display result of same).
Line continuation
Whitespace - Languages that do not need continuations
- C# Lines terminate with semicolon
Backslash as last character of line
Ampersand as last character of line
Backtick as last character of line
Underscore as last character of line
Ellipsis (as three periods–not one special character)
- MATLAB[2]: The ellipsis token need not be the last characters on the line, but any following it will be ignored.[3] (In essence, it begins a comment that extends through (i.e. including) the first subsequent newline character. Contrast this with an inline comment, which extends until the first subsequent newline.)
Some form of inline comment serves as line continuation
- Turbo Assembler:
\
- m4:
dnl
- TeX:
%
Character position
- Fortran 77: A non-comment line is a continuation of the previous non-comment line if any non-space character appears in column 6. Comment lines cannot be continued.
- Cobol: String constants may be continued by not ending the original string in a PICTURE clause with ', then inserting a - in column 7 (same position as the * for comment is used.)
- TUTOR: Lines starting with a tab (after any indentation required by the context) continue the previous command.
[End and Begin] using normal quotes
- C and C++ preprocessor: The string is ended normally and continues by starting with a quote on the next line.
Libraries
File:Wiki letter w.svg | This section requires expansion. |
To import a library is a way to read external, possibly compiled, routines, programs or packages. Imports can be classified by level (module, package, class, procedure,...) and by syntax (directive name, attributes,...)
- File import
ASP: #include file="filename"
,
C, C++: #include "filename"
, #include <filename>
,
MATLAB: addpath(directory)
[4]
PHP include filename
- Package import
Ada: with package
,
C, C++: #include filename
,
Java,
MATLAB: import package.*
,
Oberon: IMPORT module
,
Pascal: uses unit
,
Python: import module
, from module import *
- Class import
Java, MATLAB: import package.class
- Procedure/function import
MATLAB: import package.function
,
Python: from module import function
Blocks
File:Wiki letter w.svg | This section requires expansion. |
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.
Braces (aka Curly brackets) { }
:
- Curly bracket programming languages: C, C++, Java, JavaScript, ECMAScript, C#, D, Perl, PHP, Ruby (
for
&loop
loops, or pass a block as argument), S-Lang, Windows PowerShell, Haskell (in do-notation)
Parentheses
begin ... end:
do ... done:
- Visual Basic, C preprocessor, Fortran, TUTOR (with mandatory indenting of block body), Visual Prolog
do ... end
- Ruby (pass blocks as arguments,
for
loop)
X ... end (e.g. if ... end
):
- Bash (
for
&while
loops), Ruby (if
,while
,until
,def
,class
,module
statements), OCaml (for
&while
loops), MATLAB (if
&switch
conditionals,for
&while
loops,try
clause,package
,classdef
,properties
,methods
,events
, &function
blocks), Lua (if
,for
,while
,do
, &function
)
(begin ...):
(progn ...):
Indentation
- Off-side rule languages: Haskell (in do-notation when braces are omitted), occam, Python
Others
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:
Symbol | Languages |
---|---|
C | Fortran; the 'C' must be in column 1 of a line to indicate a comment. |
REM | Basic, COMMAND.COM, cmd.exe |
# | Perl, Python, Ruby, Windows PowerShell, PHP, Maple, bash |
% | TeX, Prolog, MATLAB[5], Erlang, S-Lang, Visual Prolog |
// | ActionScript, C (C99), C++, C#, Object Pascal (Delphi), Java, JavaScript, PHP |
' | Visual Basic, VBscript, RealBasic |
! | Fortran, Basic Plus |
; | AutoHotkey, Autoit, Lisp, Scheme, many assemblers |
-- | Euphoria, Haskell, SQL, Ada, Applescript, Eiffel, Lua, VHDL |
* | Cobol, many assemblers |
|| | Curl |
" | Vim Script |
\ | Forth |
:: | Batch file[6] |
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:
Symbol | Languages |
---|---|
¢ ~ ¢, # ~ #, co ~ co, comment ~ comment | ALGOL 68 |
/* */ | ActionScript, AutoHotkey, C, C++, C#, Java, JavaScript, PHP, PL/I, SQL, Visual Prolog, CSS |
""" """ | Python, Ruby[7] |
''' ''' | Python[7] |
=begin =cut | Perl |
=begin =end | Ruby |
#<tag> #</tag> | S-Lang |
{- -} | Haskell |
(* *) | Object Pascal (Delphi), ML, Mathematica, Pascal, Applescript, OCaml, Maple |
{ } | Object Pascal (Delphi), Pascal |
<!-- --> | HTML, XML |
|# #| | Curl |
%{ %} | MATLAB[8] |
#| |# | Lisp |
--[[ ]] | Lua |
Unique variants
Fortran
- The indentation of lines in FORTRAN 66/77 is significant. The actual statement is in columns 7 through 72 of a line. Any non-space character in column 6 indicates that this line is a continuation of the previous line. A 'C' in column 1 indicates that this entire line is a comment. Columns 1 though 5 may contain a number which serves as a label. Columns 73 though 80 are ignored and may be used for comments; in the days of punched cards these columns contained a sequence number so that the deck of cards could be sorted into the correct order if someone accidentally dropped the cards. Fortran 90 removed the need for the indentation rule and added traditional inline comments, using the
!
character as the comment delimiter.
Curl
- Curl supports block comments with user-defined tags as in |foo# ... #foo|
Lua
- Like raw strings, there can be any number of equals signs between the square brackets, provided both the opening and closing tags have a matching number of equals signs; this allows nesting as long as nested block comments/raw strings use a different number of equals signs than their enclosing comment: --[[ comment --[=[ nested comment ]=] ]]. Lua discards the first newline (if present) that directly follows the opening tag.
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.
PHP
- PHP supports standard C/C++ style comments, but also supports Perl style as well.
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).
Ruby
- As with Python and Perl, Ruby has no specific block-comment syntax. However, like Perl, documentation blocks can be used as block comments as they are ignored by the interpreter.
S-Lang
- The region of lines enclosed by the #<tag> and #</tag> delimiters are ignored by the interpreter. The tag name can be any sequence of alphanumeric characters that may be used to indicate how the enclosed block is to be deciphered. For example, #<latex> could indicate the start of a block of LaTeX formatted documentation.
Haskell
- Haskell's comments can be stacked as brackets:
{- some {-comments-} here -}
,
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.
- Ada, Eiffel, Euphoria, 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)
- AutoHotkey:
; InlineComment
/* BlockComment */
- 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++, and JavaScript:
/* BlockComment */
// InlineComment
- C#
/* BlockComment */
/** BlockComment */
(XML documentation comment)// InlineComment
/// InlineComment
(XML documentation comment)
- Cobol:
* InlineComment
when * is in column 7
- Curl:
|| InlineComment</code?
|# BlockComment #|
|foo# BlockComment #foo|
- D:
/* BlockComment */
// InlineComment
/+ BlockComment +/
(nestable)
- DCL:
$! InlineComment
- ECMAScript (JavaScript, ActionScript, etc):
/* BlockComment */
// InlineComment
- Forth:
( BlockComment )
(single line only)( before -- after )
stack comment convention\ InlineComment
- FORTRAN 66/77:
C InlineComment
(the letter 'C' in the first column makes the entire line a comment).
- Fortran 90:
! InlineComment
(all characters on the line, from the exclamation mark onwards, are comments)
- HTML (see SGML below)
- Java:
// InlineComment
/* BlockComment */
/** BlockComment */
(Javadoc documentation comment)
- Lisp and Scheme
#| BlockComment |#
; InlineComment
- Lua
-- InlineComment
--''BlockComment ''
- Maple:
# InlineComment
(* BlockComment *)
- Mathematica:
% (* BlockComment *)
- Matlab:
% InlineComment
%{
BlockComment (nestable)
%}
Note: Both percent–bracket symbols must be the only non-whitespace characters on their respective lines.
- Object Pascal (Delphi):
(* BlockComment *)
{ BlockComment }
// InlineComment
- Ocaml:
(* BlockComment (* nestable *) *)
- Pascal, Modula-2, Modula-3, Oberon, and ML:
(* BlockComment *)
(OCaml comments are nestable)
- Perl and Ruby:
# InlineComment
=begin
(POD documentation comment)
BlockComment
=cut__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
- Simula:
comment BlockComment;
! BlockComment;
- 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"
- Smarty:
{* BlockComment *}
- Standard ML:
(* BlockComment *)
- TeX, LaTeX, PostScript, Erlang, and S-Lang:
% InlineComment
- Texinfo:
@c InlineComment
@comment InlineComment
- TUTOR:
* InlineComment
command $$ InlineComment
- Visual Basic:
' InlineComment
Rem InlineComment
- Visual Basic .NET
' InlineComment
''' InlineComment
(XML documentation comment)Rem InlineComment
- Visual Prolog:
/* BlockComment */
% InlineComment
- XML, including XHTML:
<!--BlockComment-->
(comment must not contain--
and must not start or end with single-
)
See also
- Curly bracket programming languages, a broad family of programming language syntaxes
References
- ↑ 1.0 1.1 1.2 1.3 1.4 For multiple statements on one line
- ↑ http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f0-41244.html
- ↑ http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_env/f0-5789.html
- ↑ For an M-file (MATLAB source) to be accessible by name, its parent directory must be in the search path (or current directory).
- ↑ http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_env/brqxeeu-23.html#brqxeeu-34
- ↑ http://www.ss64.com/nt/
- ↑ 7.0 7.1 More precisely, the quoted text forms a string literal.
- ↑ http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_env/brqxeeu-23.html#brqxeeu-34
If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...
- Pages with broken file links
- Articles needing additional references from October 2009
- Articles with invalid date parameter in template
- All articles needing additional references
- Articles to be expanded from December 2009
- All articles to be expanded
- Articles to be expanded from November 2008
- Programming language comparisons
- Programming language topics