Wikipedia / Comparison of programming languages (syntax)
Wikipedia / 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
Lisp (* (+ 2 3) (expt 4 5))
infix notation
Fortran (2 + 3) * (4 ** 5)
suffix, postfix, or Reverse Polish notation
Forth 2 3 + 4 5 ** *
math-like notation
TUTOR (2 + 3)(45) $$ 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.
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.
bash and other Unix shells
C preprocessor macros; used in conjunction with C, C++ and many other programming contexts
JavaScript – only within single- or double-quoted strings
Vimscript as first character of continued line
Ellipsis (three dots)
MATLAB: The ellipsis need not end the line, but text following it is ignored. It begins a comment that extends through (including) the first subsequent newline. Contrast this with a line comment which extends until the next newline.
Ruby: comment may follow delimiter
Batch file: starting a parenthetical block can allow line continuation
Ruby: left parenthesis, left square bracket, or left curly bracket
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
m4: dnl
TeX: %
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 (same position as the * for comment is used.)
TUTOR: Lines starting with a tab (after any indentation required by the context) 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 (module, package, class, procedure,...) and by syntax (directive name, attributes,...).
File include
#include <filename> or #include "filename" – C preprocessor used in conjunction with C and C++ and other development tools
File import
addpath(directory) – MATLAB
– COBOL
or – C++
:-include("filename"). – Prolog
#include file="filename" – ASP
#include <filename> or #include "filename" – AutoHotkey, AutoIt
#import "filename" or #import <filename> – Objective-C
– Fortran
include "filename"; – PHP
include [filename] program or #include [filename] program – Pick Basic
include!("filename"); – Rust
load "filename" – Ruby
– Red
– Lua
– R
@import("filename"); – Zig
Package import
#include filename – C
import module; – C++
#[path = "filename"] mod altname; – Rust
@import module; – Objective-C
<<name – Mathematica, Wolfram Language
– Prolog:
from module import * – Python
extern crate libname; – or extern crate libname as altname; or mod modname; – Rust
library("package") – R:
IMPORT module – Oberon
import altname "package/name" – Go:
import package.module; or import altname = package.module; – D
import Module or import qualified Module as M – Haskell
import "modname"; – JavaScript
import altname from "modname"; –JavaScript
import package or import package._ – Scala
import module – Swift
import module – V (Vlang)
import module – Python
– Lua
– Crystal
– Ruby
use module – Fortran 90+
use module, only : identifier – Fortran 90+
use Module; – Perl
use Module qw(import options); – Perl
use Package.Name – Cobra
uses unit – Pascal
with package – Ada
@import("pkgname"); – Zig
Class import
from module import Class – Python
import class from "modname"; – JavaScript
import {class} from "modname"; – JavaScript
import {class as altname} from "modname"; – JavaScript
import package.class – Scala
import package.{ class1 => alternativeName, class2 } – Scala
import package._ – Scala
use Namespace\ClassName; – PHP
use Namespace\ClassName as AliasName; – PHP
using namespace::subnamespace::Class; – C++
Procedure/function import
from module import function – Python
import package.module : symbol; – D
import package.module : altsymbolname = symbol; – D
import Module (function) – Haskell
import function from "modname"; – JavaScript
import {function} from "modname"; – JavaScript
import {function as altname} from "modname"; – JavaScript
import package.function – MATLAB
import package.class.function – Scala
import package.class.{ function => alternativeName, otherFunction } – Scala
– Perl
use function Namespace\function_name; – PHP
use Namespace\function_name as function_alias_name; – PHP
using namespace::subnamespace::symbol; – C++
use module::submodule::symbol; – Rust
use module::submodule::{symbol1, symbol2}; – Rust
use module::submodule::symbol as altname; – Rust
Constant import
use const Namespace\CONST_NAME; – PHP
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).
Syntactic convenience
import package.* Java
import package.class Java
open module OCaml
using namespace namespace::subnamespace; – C++
use module::submodule::*; – Rust
Required to access code
import module;C++
import altname "package/name" Go
import altname from "modname";JavaScript
import modulePython
Block delimitation
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 indenting.
Curly braces (a.k.a. curly brackets) { ... }:
Curly brace languages: A defining aspect of curly brace languages is that they use curly braces to delimit a block.
Parentheses ( ... )
Batchfile, F# (lightweight syntax), OCaml, Prolog, Standard ML
Square brackets [ ... ]
Rebol, Red, Self, Smalltalk (blocks are first class objects. a.k.a. closures)
begin ... end:
Ada, ALGOL, F# (verbose syntax), Pascal, Ruby (for, do/while & do/until loops), Crystal, OCaml, SCL, Simula, Erlang.
do ... end:
do ... done:
Bash (for & while loops), F# (verbose syntax) Visual Basic, Fortran, TUTOR (with mandatory indenting of block body), Visual Prolog
do ... end
Lua, Ruby (pass blocks as arguments, for loop), Seed7 (encloses loop bodies between do and end)
X ... end (e.g. if ... end):
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 (then / else & function)
(begin ...):
(progn ...):
(do ...):
Indentation
Off-side rule languages: Boo, Cobra, CoffeeScript, F#, Haskell (in do-notation when braces are omitted), LiveScript, occam, Python, Nemerle (Optional; the user may use white-space sensitive syntax instead of the curly-brace syntax if they so desire), Nim, Scala (Optional, as in Nemerle)
Free-form languages: most descendants from ALGOL (including C, Pascal, and Perl); Lisp languages
Others
Ada, Visual Basic, Seed7: if ... end if
ALGOL 68: begin ... end, ( ... ), if ... fi, do ... od
APL: :If ... :EndIf or :If ... :End
COBOL: IF ... END-IF, PERFORM ... END-PERFORM, etc. for statements; ... . for sentences.* Lua, Pascal, Modula-2, Seed7: repeat ... until
Small Basic: If ... EndIf, For ... EndFor, While ... EndWhile
Visual Basic (.NET): If ... End If, For ... Next, Do ... Loop
Comments
With respect to a language definition, the syntax of Commentss can be classified many ways, including:
Line vs. block – a line comment starts with a delimiter and continues to the end of the line (newline marker) 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 (including compilers and interpreters) 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
Block comment
In these examples, ~ represents the comment content, and the text around it are the delimiters. Whitespace (including newline) is not considered delimiters.
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 indenting rule and added line comments, using the ! character as the comment delimiter.
COBOL
In fixed format code, line indenting 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 --[=[ 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 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 (except when it is the first statement in the body of a module, class or function; see docstring).
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 (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.
Raku
Raku uses #`(...) to denote block comments. 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).
Ruby
Block comment in Ruby opens at =begin line and closes at =end line.
S-Lang
The region of lines enclosed by the and 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, could indicate the start of a block of LaTeX formatted documentation.
Scheme and Racket
The next complete syntactic component (s-expression) 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 (e.g., Befunge) or otherwise assigned a meaning (e.g., Brainfuck), 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.
See also
List of programming languages by type#curly-brace, a broad family of programming language syntaxes
References
Notes