Comparison of programming languages (syntax)


This article compares the syntax of many notable programming languages.

Expressions

Programming language expressions can be broadly classified into four syntax structures:
;prefix notation
;infix notation
;suffix, postfix, or Reverse Polish notation
;math-like notation
  • TUTOR $$ note implicit multiply operator

Statement delimitation

A language that supports the statement construct typically has rules for one or more of the following aspects:
  • Statement terminator marks the end of a statement
  • Statement separator demarcates the boundary between two statements; not needed for the last statement
  • Line continuation escapes a newline to continue a statement on the next line
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.
LanguageStatement delimitation
ABAPperiod separated
Adasemicolon terminated
ALGOLsemicolon separated
ALGOL 68semicolon and comma separated
APLnewline terminated, separated
AppleScriptnewline terminated
AutoHotkeynewline terminated
Awknewline or semicolon terminated
BASICnewline terminated, colon separated
Boonewline terminated
Csemicolon terminated, comma separated expressions
C++semicolon terminated, comma separated expressions
C#semicolon terminated
COBOLwhitespace separated, sometimes period separated, optionally separated with commas and semi-colons
Cobranewline terminated
CoffeeScriptnewline terminated
CSSsemicolon terminated
Dsemicolon terminated
Eiffelnewline terminated, semicolon separated
Erlangcolon separated, period terminated
F#newline terminated, semicolon
Fortrannewline terminated, semicolon separated
Forthsemicolons terminate word definitions; space terminates word use
GFA BASICnewline terminated
Gosemicolon separated
Haskellin do-notation: newline separated,
in do-notation with braces: semicolon separated
Javasemicolon terminated
JavaScriptsemicolon separated
Kotlinsemicolon separated
Luawhitespace separated
Mathematica a.k.a. Wolframsemicolon separated
MATLABnewline terminated, separated by semicolon or comma
MUMPS a.k.a. Mnewline terminates line-scope, the closest to a "statement" that M has, a space separates/terminates a command, allowing another command to follow
Nimnewline terminated
Object Pascal semicolon separated
Objective-Csemicolon terminated
OCamlsemicolon separated
Pascalsemicolon separated
Perlsemicolon separated
PHPsemicolon terminated
Pick Basicnewline terminated, semicolon separated
PowerShellnewline terminated, semicolon separated
Prologcomma separated, semicolon separated, period terminated
Pythonnewline terminated, semicolon separated
Rnewline terminated, semicolon separated
Rakusemicolon separated
Redwhitespace separated
Rubynewline terminated, semicolon separated
Rustsemicolon terminated, comma separates expressions
Scalanewline terminated, semicolon separator
Seed7semicolon separated
Simulasemicolon separated
S-Langsemicolon separated
Smalltalkperiod separated
Standard MLsemicolon separated
Swiftsemicolon separated
Tclnewline or semicolon terminated
V (Vlang)newline terminated, comma or semicolon separated
Visual Basicnewline terminated, colon separated
Visual Basic (.NET)newline terminated, colon separated
Xojonewline terminated
Zigsemicolon terminated

Line continuation

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.
;Ampersand
;Backslash
;Backtick
;Hyphen
;Underscore
;Ellipsis
  • MATLAB: The ellipsis need not end the line, but text following it is ignored. It begins a comment that extends through the first subsequent newline. Contrast this with a line comment which extends until the next newline.
;Comma delimiter
;Left bracket delimiter
;Operator symbol
  • Ruby: as last object of line; comment may follow operator
  • AutoHotkey: As the first character of continued line; any expression operators except ++ and --, and a comma or a period
;Some form of line comment serves as line continuation
;Character position
  • Fortran 77: A non-comment line is a continuation of the prior 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
  • TUTOR: Lines starting with a tab continue the prior command.
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.

Consuming external software

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 and by syntax.
;File include#include <filename> or #include "filename" C preprocessor used in conjunction with C and C++ and other development tools
;File importaddpath MATLAB
;Package import#include filename Cimport module; C++# mod altname; Rust@import module; Objective-C<<name Mathematica, Wolfram Language
;Class importfrom module import Class Pythonimport package.class Java, MATLAB, kotlinimport class from "modname"; JavaScriptimport from "modname"; JavaScriptimport from "modname"; JavaScriptimport package.class Scalaimport package. Scalaimport package._ Scalause Namespace\ClassName; PHPuse Namespace\ClassName as AliasName; PHPusing namespace::subnamespace::Class; C++
;Procedure/function importfrom
module import function Pythonimport package.module : symbol; Dimport package.module : altsymbolname = symbol; Dimport Module Haskellimport function from "modname"; JavaScriptimport from "modname"; JavaScriptimport from "modname"; JavaScriptimport package.function MATLABimport package.class.function Scalaimport package.class. Scala
  • Perluse function Namespace\function_name; PHPuse Namespace\function_name as function_alias_name; PHPusing namespace::subnamespace::symbol; C++use module::submodule::symbol; Rustuse module::submodule::; Rustuse module::submodule::symbol as altname; Rust
;Constant importuse const Namespace\CONST_NAME; PHP
The above statements can also be classified by whether they are a syntactic convenience, or whether they are actually required to access the code.
;Syntactic convenienceimport
package.* Javaimport package.class Javaopen module OCamlusing namespace namespace::subnamespace; C++use module::submodule::*; Rust
;Required to access codeimport
module;C++import altname "package/name" Goimport altname from "modname";JavaScriptimport module''Python

Block delimitation

A block is a grouping of code that is treated collectively. Many block syntaxes can consist of any number of items 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.
;Curley braces :
  • Curly brace languages: A defining aspect of curly brace languages is that they use curly braces to delimit a block.
;Parentheses
;Square brackets
;begin... end:
;do... end:
;do... done:
;do... end
;X... end :
;:
;:
;:
;Indentation
;Others

Comments

With respect to a language definition, the syntax of Comments can be classified many ways, including:
  • Line vs. block a line comment starts with a delimiter and continues to the end of the line whereas a block comment starts with one delimiter and ends with another and can cross lines
  • Nestable whether a block comment can be inside another block comment
  • How parsed with respect to the language; tools may also parse comments but that may be outside the language definition
Other ways to categorize comments that are outside a language definition:
  • Inline vs. prologue an inline comment follows code on the same line and a prologue comment precedes program code to which it pertains; line or block comments can be used as either inline or prologue
  • Support for API documentation generation which is outside a language definition

Line comment

SymbolLanguages
CFortran I to Fortran 77
REMBASIC, Batch files, Visual Basic
::Batch files, COMMAND.COM, cmd.exe
NB.J; from the common abbreviation Nota bene, the Latin for "note well".
APL; the mnemonic is that the glyph resembles a desk lamp, and hence "illuminates" the foregoing.
#Boo, Bourne shell and other UNIX shells, Cobra, Perl, Python, Ruby, Seed7, PowerShell, PHP, R, Make, Maple, Elixir, Julia, Nim
%TeX, Prolog, MATLAB, Erlang, S-Lang, Visual Prolog, PostScript
//ActionScript, Boo, C (C99), C++, C#, D, F#, Go, Java, JavaScript, Kotlin, Object Pascal, Objective-C, PHP, Rust, Scala, Sass, Swift, Xojo, V (Vlang), Zig
'Monkey, Visual Basic, VBScript, Small Basic, Gambas, Xojo
!Factor, Fortran, Basic Plus, Inform, Pick Basic
;Most assembly languages, AutoHotkey, AutoIt, Lisp, Common Lisp, Clojure, PGN, Rebol, Red, Scheme
--Euphoria, Haskell, SQL, Ada, AppleScript, Eiffel, Lua, VHDL, SGML, PureScript, Elm
*Assembler S/360, COBOL I to COBOL 85, PAW, Fortran IV to Fortran 77, Pick Basic, GAMS
||Curl
"Vimscript, ABAP
\Forth
*>COBOL 90

Block comment

In these examples, ~ represents the comment content, and the text around it are the delimiters. Whitespace is not considered delimiters.
SyntaxLanguages
comment ~ ;ALGOL 60, SIMULA
¢ ~ ¢,
# ~ #, co ~ co,
comment ~ comment
ALGOL 68
/* ~ */ActionScript, AutoHotkey, C, C++, C#, CSS, D, Go, Java, JavaScript, Kotlin, Objective-C, PHP, PL/I, Prolog, Rexx, Rust, Scala, SAS, SASS, SQL, Swift, V (Vlang), Visual Prolog
#cs ~ #ceAutoIt
/+ ~ +/D
/# ~ #/Cobra
<# ~ #>PowerShell
<!-- ~ -->HTML, XML
=begin ~ =cutPerl
#`Raku
=begin ~ =endRuby
#<TAG> ~ #TAG>,
#iffalse ~ #endif,
#if false ~ #endif,
S-Lang
Haskell, Elm
Delphi, ML, Mathematica, Object Pascal, Pascal, Seed7, AppleScript, OCaml, Standard ML, Maple, Newspeak, F#
Delphi, Object Pascal, Pascal, PGN, Red
Nunjucks, Twig
Mustache, Handlebars
Handlebars
|# ~ #|Curl
%MATLAB
#| ~ |#Lisp, Scheme, Racket.
#= ~ =#Julia
##Nim
-- ~ ,
--=],
--] etc.
Lua
" ~ "Smalltalk
Clojure
#If COMMENT Then ~ Visual Basic (.NET)
#if COMMENT ~ #endifC#
' comment _,
Classic Visual Basic, VBA, VBScript

Unique variants

;Fortran
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.
;COBOL
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.
;Cobra
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
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 --=] . Lua discards the first newline 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. 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 supports Perl style as well.
;Python
The use of the triple-quotes to comment-out lines of source, does not actually form a comment. The enclosed text becomes a string literal, which Python usually ignores.
;Elixir
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 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.
;Raku
Raku uses #` to denote block comments. Raku actually allows the use of any "right" and "left" paired brackets after #`. Brackets are also allowed to be nested inside comments.
;Ruby
Block comment in Ruby opens at =begin line and closes at =end line.
;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.
;Scheme and Racket
The next complete syntactic component can be commented out with #;.
;ABAP
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.
;Esoteric languages
Many esoteric programming languages follow the convention that any text not executed by the instruction pointer or otherwise assigned a meaning, 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.
LineComment in italics is used here to indicate line comment style.
LanguageIn-line commentBlock comment
Ada, Eiffel, Euphoria, Occam, SPARK, ANSI SQL, and VHDL-- LineComment
ALGOL 60comment BlockComment;
ALGOL 68¢ BlockComment ¢

comment BlockComment comment

co BlockComment co

# BlockComment #

£ BlockComment £
APLLineComment
AppleScript-- LineComment
Assembly language ; LineComment one example
AutoHotkey; LineComment/* BlockComment */
AWK, Bourne shell, C shell, Maple, PowerShell# LineComment<# BlockComment #>
Bash# LineComment<
BlockComment
EOF


: '
BlockComment
'
BASIC :'LineComment

*LineComment

!LineComment

REM LineComment
C, CHILL, PL/I, REXX/* BlockComment */
C (C99), C++, Go, Swift, JavaScript, V (Vlang)// LineComment/* BlockComment */
C#// LineComment
/// LineComment
/* BlockComment */
/** BlockComment */
#if COMMENT
BlockComment
#endif
COBOL I to COBOL 85 * LineComment
COBOL 2002*> LineComment
Curl|| LineComment|# BlockComment #|

|foo# BlockComment #|
Cobra# LineComment/# BlockComment #/
D// LineComment
/// Documentation LineComment
/* BlockComment */
/** Documentation BlockComment */

/+ BlockComment +/
/++ Documentation BlockComment +/
DCL$! LineComment
ECMAScript // LineComment/* BlockComment */
Elixir# LineComment~S"""
BlockComment
"""

@doc """
BlockComment
"""

@moduledoc
BlockComment
"""

@typedoc
BlockComment
"""
Forth\ LineComment

stack comment convention
FORTRAN I to FORTRAN 77C LineComment
Fortran 90 and later! LineComment#if 0
BlockComment
#endif
Haskell-- LineComment
JNB.
Java// LineComment/* BlockComment */

/** BlockComment */
Julia# LineComment #= BlockComment =#
Lisp, Scheme; LineComment#| BlockComment |#
Lua-- LineComment --]
Maple# LineComment
Mathematica
Matlab% LineComment%
Nim# LineComment ##
Object Pascal// LineComment
OCaml
Pascal, Modula-2, Modula-3, Oberon, ML:
Perl, Ruby# LineComment =begin
BlockComment
=cut


__END__
Comments after end of code
PGN, Red; LineComment
PHP# LineComment
// LineComment
/* BlockComment */
/** Documentation BlockComment */
PILOTR:LineComment
PLZ/SYS! BlockComment !
PL/SQL, TSQL-- LineComment/* BlockComment */
Prolog% LineComment/* BlockComment */
Python# LineComment BlockComment
""" BlockComment """


R# LineComment
Raku# LineComment #`

=comment
This comment paragraph goes until the next POD directive
or the first blank line.
Rust// LineComment

/// LineComment

//! LineComment
/* BlockComment */

/** BlockComment */

/*! BlockComment */
SAS* BlockComment;
/* BlockComment */
Seed7# LineComment
Simulacomment BlockComment;
! BlockComment;
Smalltalk"BlockComment"
Smarty
Standard ML
TeX, LaTeX, PostScript, Erlang, S-Lang% LineComment
Texinfo@c LineComment
@comment LineComment
TUTOR* LineComment
command $$ LineComment
Visual Basic' LineComment
Rem LineComment
' BlockComment _
BlockComment

Rem BlockComment _
BlockComment
Visual Basic (.NET)' LineComment

' LineComment

Rem
LineComment
#If COMMENT Then
BlockComment
#End If
Visual Prolog% LineComment/* BlockComment */
Wolfram Language
Xojo' LineComment
//
LineComment
rem
LineComment
-
Zig// LineComment
///
LineComment
//!
LineComment''