Table of Contents

What is a syntax?

Syntax is the set of rules that define how symbols, words, or statements must be structured to form valid expressions in a language. In programming, syntax determines how code must be written so that a computer can interpret, compile, and execute commands correctly.

Syntax is the bridge between human-readable code and machine-executable instructions. It provides the framework for expressing computational logic in a way that both developers and computers can understand. 

While natural languages are often comprehensible despite minor grammatical errors, programming languages require strict adherence to syntactical rules for code to execute properly.

Why do programming languages have strict syntax rules?

Strict syntax ensures consistency, prevents errors, and enables precise translation into machine instructions. These rules help compilers and interpreters unambiguously process code while improving readability and maintainability for developers. 

By standardizing structure, strict syntax allows developers to write reliable, error-free programs.

Core components of syntax

Every programming language has specific syntactical elements that form its foundation, though these elements may vary between languages:

  1. Keywords and reserved words

These are special words with predefined meanings and purposes. Because they are reserved, they cannot be used as variable names. 

For example, contract in Solidity and function in JavaScript serve specific roles in the language's structure.

  1. Operators and symbols

Programming languages use various symbols to perform operations, evaluate expressions, and control the flow of code. These include:

  • Arithmetic operators (+, -, *, /) for mathematical calculations.
  • Comparison operators (==, !=, >, <) for comparing values.
  • Assignment operators (=, +=, -=) for assigning values to variables.
  • Logical operators (&&, ||, !) for combining and evaluating logical conditions.
  • Bitwise operators (&, |, ^) for manipulating data at the bit level.
  • Other operators (++, --, %, etc.) depending on the language.
  1.  Punctuation and delimiters

Punctuation marks serve crucial roles in code organization and syntax:

  • Semicolons (;) often mark the end of statements.
  • Curly braces {} define code blocks and scope.
  • Parentheses () group expressions, control precedence, and enclose function parameters.
  • Square brackets [] commonly used for arrays, indexing, and element access.
  • Commas (,) separate items in lists, parameters, and function arguments.
  • Colons (:) are commonly used for labels, case statements, and type annotations
  • Periods (.) are used to access the properties, methods, or attributes of objects or structures.
  • Quotation marks ("", '', ``) delimit string literals.

  1. Variable and function declarations

Every programming language has specific rules for declaring and using variables and functions. These rules ensure proper memory allocation, data typing, and code organization. Variable declarations typically specify the variable's name and data type, while function declarations define the function's name, parameters, return type, and code block. 

Proper declarations are essential for writing correct and maintainable code.

Common syntax rules in programming

Programming languages share some key syntax rules to ensure clarity and consistency:

  • Statement termination: While many programming languages require explicit statement termination, often using semicolons or new lines, some, like Python, rely on indentation instead.
  • Block structure: To organize code and define scope boundaries, code blocks are typically defined by indentation or enclosed in curly braces.
  • Indentation and whitespace: Consistent indentation, while also used for readability, is syntactically required in some languages (like Python) and always helps clarify code structure.
  • Case sensitivity: In most programming languages, variables are case-sensitive, meaning Variable and variable are distinct. However, some languages like SQL, are not case-sensitive.
  • Comments: Comments are used to explain code to human readers and are ignored by the compiler or interpreter. Different languages use different syntax for comments (e.g., // or /* */).

Solidity vs. Vyper syntax comparison example

Both Solidity and Vyper are smart contract languages for Ethereum, but they differ in syntax, structure, and design philosophy. Below is a comparison based on the Tipping contract example:

Solidity: Tipping.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Tipping {
   // State variables
   mapping(address => uint256) public userTips;
   uint256 public totalTips;
  
   // Event
   event TipReceived(address indexed tipper, uint256 amount);
  
   // Payable function to receive tips
   function sendTip() external payable {
       require(msg.value > 0, "Tip amount must be positive");
      
       userTips[msg.sender] += msg.value;
       totalTips += msg.value;
      
       emit TipReceived(msg.sender, msg.value);
   }
  
   // View function to get user's total tips
   function getUserTips(address user) external view returns (uint256) {
       return userTips[user];
   }
}

Vyper: Tipping.vy

# @version ^0.3.9

# State variables
user_tips: public(HashMap[address, uint256])
total_tips: public(uint256)

# Event
event TipReceived:
    tipper: indexed(address)
    amount: uint256

# Payable function to receive tips
@external
@payable
def send_tip():
    assert msg.value > 0, "Tip amount must be positive"
    
    self.user_tips[msg.sender] += msg.value
    self.total_tips += msg.value
    
    log TipReceived(msg.sender, msg.value)

# View function to get user's total tips
@view
@external
def get_user_tips(user: address) -> uint256:
    return self.user_tips[user]

Key syntax differences of the Tipping contract explained:

Syntax feature Solidity Vyper
Basic contract structure contract ContractName {} No explicit contract declaration; state variables and functions are written directly
Function declarations function functionName() {} with explicit visibility (external, view, payable) @external and @payable decorators
Variable declarations Mappings with mapping(address => uint256) public userTips; Mappings with user_tips: public(HashMap[address, uint256])
Control structures require() assert
Similarity to other languages Syntactically similar to JavaScript. Similar to Python in terms of syntax and structure.

Related Terms

No items found.