This article compares the syntax of many notable programming languages.
Programming language expressions can be broadly classified into four syntax structures:
(* (+ 2 3) (expt 4 5))
(2 + 3) * (4 ** 5)
2 3 + 4 5 ** *
(2 + 3)(45) $$ note implicit multiply operator
A language that supports the statement construct typically has rules for one or more of the following aspects:
Some languages define a special character as a terminator while some, called line-oriented, rely on the newline. Typically, a line-oriented language includes a line continuation feature whereas other languages have no need for line continuation since newline is treated like other whitespace. Some line-oriented languages provide a separator for use between statements on one line.
[Direct_function ⋄]
Listed below are notable line-oriented languages that provide for line continuation. Unless otherwise noted the continuation marker must be the last text of the line.
\
dnl
%
'
-
*
The C compiler concatenates adjacent string literals even if on separate lines, but this is not line continuation syntax as it works the same regardless of the kind of whitespace between the literals.
Languages support a variety of ways to reference and consume other software in the syntax of the language. In some cases this is importing the exported functionality of a library, package or module but some mechanisms are simpler text file include operations.
Import can be classified by level (module, package, class, procedure,...) and by syntax (directive name, attributes,...).
#include <filename>
#include "filename"
addpath(directory)
COPY filename.
:-include("filename").
#include file="filename"
#import "filename"
#import <filename>
Import["filename"]
include 'filename'
include "filename";
include [filename] program
#include [filename] program
include!("filename");
load "filename"
load %filename
require('filename')
require "filename";
require "filename"
source(""filename"")
@import("filename");
#include filename
#[path = "filename"] mod altname;
@import module;
<<name
:-use_module(module).
from module import *
extern crate libname;
extern crate libname as altname;
mod modname;
library("package")
IMPORT module
import altname "package/name"
import package.module;
import altname = package.module;
import Module
import qualified Module as M
import package.*
import "modname";
import altname from "modname";
import package
import package._
import module
require('modname')
require "gem"
use module
use module, only : identifier
use Module;
use Module qw(import options);
use Package.Name
uses unit
with package
@import("pkgname");
from module import Class
import package.class
import class from "modname";
import {class} from "modname";
import {class as altname} from "modname";
import package.{ class1 => alternativeName, class2 }
use Namespace\ClassName;
use Namespace\ClassName as AliasName;
from module import function
import package.module : symbol;
import package.module : altsymbolname = symbol;
import Module (function)
import function from "modname";
import {function} from "modname";
import {function as altname} from "modname";
import package.function
import package.class.function
import package.class.{ function => alternativeName, otherFunction }
use Module ('symbol');
use function Namespace\function_name;
use Namespace\function_name as function_alias_name;
use module::submodule::symbol;
use module::submodule::{symbol1, symbol2};
use module::submodule::symbol as altname;
use const Namespace\CONST_NAME;
The above statements can also be classified by whether they are a syntactic convenience (allowing things to be referred to by a shorter name, but they can still be referred to by some fully qualified name without import), or whether they are actually required to access the code (without which it is impossible to access the code, even with fully qualified names).
open module
A block is a grouping of code that is treated collectively. Many block syntaxes can consist of any number of items (statements, expressions or other units of code) – including one or zero. Languages delimit a block in a variety of ways – some via marking text and others by relative formatting such as levels of indentation.
{
}
(
)
[
]
begin
end
for
do/while
do/until
do
done
while
if
until
def
class
module
switch
try
package
classdef
properties
methods
events
function
then
else
end if
fi
od
:If
:EndIf
:End
case
esac
IF
END-IF
PERFORM
END-PERFORM
.
repeat
If
EndIf
For
EndFor
While
EndWhile
End If
Next
Do
Loop
With respect to a language definition, the syntax of Comments can be classified many ways, including:
Other ways to categorize comments that are outside a language definition:
C
REM
::
NB.
⍝
#
//
!
;
--
||
"
*>
In these examples, ~ represents the comment content, and the text around it are the delimiters. Whitespace (including newline) is not considered delimiters.
~
comment
¢
co
/*
*/
#cs
#ce
/+
+/
/#
#/
<#
#>
<!--
-->
=begin
=cut
#`(
=end
#<TAG>
#</TAG>
#stop
EOF
#iffalse
#endif
#ifntrue
#if false
#if !true
{-
-}
(*
*)
{#
#}
{{!
}}
{{!--
--}}
{{
|#
#|
%{
%}
#=
=#
#[
]#
--[[
]]
--[=[
]=]
--[=
=[
]=
=]
(comment
#If COMMENT Then
#End If
#if COMMENT
' comment _
REM comment _
Indenting 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 prior 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 often 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 line comments, using the ! character as the comment delimiter.
In fixed format code, line indentation is significant. Columns 1–6 and columns from 73 onwards are ignored. If a * or / is in column 7, then that line is a comment. Until COBOL 2002, if a D or d was in column 7, it would define a "debugging line" which would be ignored unless the compiler was instructed to compile it.
/
D
d
Cobra supports block comments with "/# ... #/" which is like the "/* ... */" often found in C-based languages, but with two differences. The # character is reused from the single-line comment form "# ...", and the block comments can be nested which is convenient for commenting out large blocks of code.
Curl supports block comments with user-defined tags as in |foo# ... #foo|.
|foo# ... #foo|
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.
--[[comment --[=[ nested comment ]=] ]]
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 supports standard C/C++ style comments, but supports Perl style as well.
The use of the triple-quotes to comment-out lines of source, does not actually form a comment.[19] The enclosed text becomes a string literal, which Python usually ignores (except when it is the first statement in the body of a module, class or function; see docstring).
The above trick used in Python also works in Elixir, but the compiler will throw a warning if it spots this. To suppress the warning, one would need to prepend the sigil ~S (which prevents string interpolation) to the triple-quoted string, leading to the final construct ~S""" ... """. In addition, Elixir supports a limited form of block comments as an official language feature, but as in Perl, this construct is entirely intended to write documentation. Unlike in Perl, it cannot be used as a workaround, being limited to certain parts of the code and throwing errors or even suppressing functions if used elsewhere.[20]
~S
~S""" ... """
Raku uses #`(...) to denote block comments.[21] Raku actually allows the use of any "right" and "left" paired brackets after #` (i.e. #`(...), #`[...], #`{...}, #`<...>, and even the more complicated #`{{...}} are all valid block comments). Brackets are also allowed to be nested inside comments (i.e. #`{ a { b } c } goes to the last closing brace).
#`(...)
#`
#`[...]
#`{...}
#`<...>
#`{{...}}
#`{ a { b } c }
Block comment in Ruby opens at =begin line and closes at =end line.
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.
#<tag>
#</tag>
#<latex>
The next complete syntactic component (s-expression) can be commented out with #; .
#;
ABAP supports two different kinds of comments. If the first character of a line, including indentation, is an asterisk (*) the whole line is considered as a comment, while a single double quote (") begins an in-line comment which acts until the end of the line. ABAP comments are not possible between the statements EXEC SQL and ENDEXEC because Native SQL has other usages for these characters. In the most SQL dialects the double dash (--) can be used instead.
EXEC SQL
ENDEXEC
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), is considered a "comment".
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. LineComment in italics is used here to indicate line comment style.
BlockComment
LineComment
-- LineComment
comment BlockComment;
¢ BlockComment ¢
comment BlockComment comment co BlockComment co # BlockComment # £ BlockComment £
comment BlockComment comment
co BlockComment co
# BlockComment #
£ BlockComment £
⍝ LineComment
(* BlockComment *)
; LineComment
/* BlockComment */
# LineComment
<# BlockComment #>
<<EOFBlockCommentEOF
: 'BlockComment'
'LineComment
*LineComment (not all dialects) !LineComment (not all dialects) REM LineComment
*LineComment (not all dialects)
!LineComment (not all dialects)
REM LineComment
// LineComment
/// LineComment
/** BlockComment */
#if COMMENT BlockComment#endif
* LineComment
*> LineComment
|| LineComment
|# BlockComment #|
|foo# BlockComment #|
/# BlockComment #/
/// Documentation LineComment
/** Documentation BlockComment */
/+ BlockComment +/ (nestable)/++ Documentation BlockComment +/ (nestable, ddoc comments)
/+ BlockComment +/
/++ Documentation BlockComment +/
$! LineComment
~S"""BlockComment"""
@doc """BlockComment"""
@moduledocBlockComment"""
@typedocBlockComment"""
\ LineComment
( BlockComment )
( before -- after ) stack comment convention
( before -- after )
C LineComment
! LineComment
#if 0 BlockComment#endif
{- BlockComment -}
/** BlockComment */ (Javadoc documentation comment)
#= BlockComment =#
#| BlockComment |#
--[==[ BlockComment]==]
% LineComment
%{BlockComment (nestable)%}
#[ BlockComment ]#
{ BlockComment }
(* BlockComment (* nestable *) *)
=beginBlockComment=cut
__END__Comments after end of code
R:LineComment
! BlockComment !
''' BlockComment '''""" BlockComment """
(Documentation string when first line of module, class, method, or function)
#`{BlockComment}
=comment This comment paragraph goes until the next POD directive or the first blank line.[23][24]
=comment This comment paragraph goes until the next POD directive or the first blank line.
/// LineComment ("Outer" rustdoc comment) //! LineComment ("Inner" rustdoc comment)
//! LineComment
/** BlockComment */ ("Outer" rustdoc comment) /*! BlockComment */ ("Inner" rustdoc comment)
/*! BlockComment */
* BlockComment;
! BlockComment;
"BlockComment"
{* BlockComment *}
@c LineComment
@comment LineComment
command $$ LineComment
' LineComment
Rem LineComment
' BlockComment _BlockComment
Rem BlockComment _BlockComment
''' LineComment (XML documentation comment) Rem LineComment
''' LineComment
#If COMMENT Then BlockComment#End If
rem LineComment
/* ... */
_