Log in Sign up
Back to Discover
💻

Syntax (programming languages)

technology Maturity 11-13

Computers use special rules to talk.

Python add5 syntax.svg
Python add5 syntax.svg
These rules help them read code. You must use the right words. If the rules are wrong, the computer gets stuck. It helps us tell them what to do. Do you like to play with computers?

45 words

Computers use special rules to read code.

Python add5 syntax.svg
Python add5 syntax.svg
These rules are called syntax. You must follow them to talk to a computer. Some code uses text. Other code uses pictures.
Python add5 parse.svg
Python add5 parse.svg
If you make a mistake, the computer sees an error. It might get stuck. The rules help the computer understand your instructions. It checks the words and the order. This helps the computer do its job. Using the right rules makes coding fun!

78 words

Computers use special rules to read code. We call these rules syntax. Syntax tells a computer how to read words and symbols.

Python add5 syntax.svg
Python add5 syntax.svg
Without these rules, the computer would not understand.

There are three main levels of syntax. First is the lexical level. This is the level of words. A tool called a lexer looks at characters. It turns them into tokens. Tokens are like the building blocks of a language.

Next is the grammar level. This level looks at phrases. A tool called a parser checks the tokens. It makes sure they follow the rules. It often builds an abstract syntax tree. This is a simple map of the code.

Python add5 parse.svg
Python add5 parse.svg

The last level is context. This step checks if names and types are right.

Syntax is not the same as meaning. Syntax is about the form of the code. Meaning is called semantics. A sentence can follow all the rules but still make no sense. For example, a sentence might be grammatically correct but tell a lie. In coding, a program can have perfect syntax but still have errors in meaning.

187 words

Computers need rules to understand instructions. We call these rules syntax. Syntax is the way code is structured and ordered. Without these rules, a computer would not know how to read a program.

Python add5 syntax.svg
Python add5 syntax.svg
Most programming languages use text. These languages use strings of characters to build commands. Some use visual tools instead. These use shapes and lines to show how things connect. If a person writes code that breaks the rules, a syntax error happens. A tool like a compiler or an interpreter will find the mistake.

Syntax works in three main steps. First is the lexical level, which is about words. A tool called a lexer looks at every single character. It turns them into small pieces called tokens.

Python add5 parse.svg
Python add5 parse.svg
Second is the grammar level, which is about phrases. A tool called a parser takes those tokens and organizes them. It builds a structure called an abstract syntax tree. This tree is a simple map that shows how the code fits together. Third is the context level. This step checks if the names and types in the code are correct.

People have used many tools to make this work. Some designers start by writing examples of good and bad code. They use these examples to find the general rules. There are tools like lex-yacc that can build a parser automatically. This is called declarative programming. However, many people still write parsers by hand. They might do this to make the code run faster. They might also want to show better error messages. People write these tools in many different languages like Python, C, or Haskell.

Different languages use different math to define their rules. Many languages use a Type-2 grammar. This is called a context-free language. This means the rules for phrases do not depend on what is around them. Some languages are more complex. For example, Perl and Lisp have very special rules. In Perl, the computer might actually run code while it is still reading it. This makes the parsing process very tricky. In Lisp, special tools called macros can also run during this phase.

It is important to know that syntax is not the same as meaning. The meaning of code is called semantics. Syntax is just about the form or the shape of the code.

Python add5 syntax.svg
Python add5 syntax.svg
You can have a sentence that follows every grammar rule but makes no sense. For example, "Colorless green ideas sleep furiously" is a grammatically correct sentence. Yet, it does not have a real meaning. Code can be the same way. A program might have perfect syntax but still have a semantic error. This happens if the instructions do not do what the person intended.

452 words

Syntax is the set of rules that defines the structure and order of a programming language. Just as human languages have grammar to make sentences understandable, computer languages use syntax to ensure instructions are valid. Without these rules, a computer would not know how to process source code. Most programming languages are text-based, using strings of characters to form commands. Some are visual, using graphical elements and relationships to define structure. When a programmer writes code that violates these rules, it results in a syntax error. A tool, such as a compiler or an interpreter, detects these errors during processing.

Python add5 syntax.svg
Python add5 syntax.svg

To process code, computers follow a modular three-level system: lexical, grammar, and context. This modularity allows each level to be handled independently. The first step is lexical analysis, or lexing. A tool called a lexer takes a linear sequence of characters and turns them into tokens. Tokens are the basic building blocks of the language. The second step is parsing, which occurs at the grammar level. A parser takes the tokens and organizes them into a hierarchical structure called a syntax tree. This ensures the tokens follow the formal grammar of the language. This process can produce a concrete syntax tree, which is very detailed, or an abstract syntax tree (AST), which is a simplified, usable version.

Python add5 parse.svg
Python add5 parse.svg

The third level is contextual analysis. This step determines what specific names or variables refer to and checks if their types are valid. While many languages allow these steps to be separate, some real-world languages require them to overlap. For example, in the C language, the lexer hack is used because tokenization depends on the context. The levels of syntax often correspond to the Chomsky hierarchy of grammars. The lexical level uses regular languages, which are Type-3 grammars defined by regular expressions. The phrase level uses context-free languages, or Type-2 grammars, often defined by production rules in Backus–Naur form (BNF).

Language designers use specific tools to define these rules. They might use regular expressions for lexical structure and BNF for grammatical structure. These rules use terminal symbols, which are concrete characters like keywords (for example, "if" or "void"), and nonterminal syntactic categories. Some designers use automatic tools like the lex-yacc pair to generate lexers and parsers. This is known as declarative programming. However, many developers still write parsers manually. They might do this to improve performance, allow for easier grammar changes, or provide better error reporting. Parsers are often written in languages like Haskell, Python, C, or C++.

Python add5 syntax.svg
Python add5 syntax.svg

Complexity varies greatly between different programming languages. Most phrase grammars are context-free, meaning the rules do not change based on surrounding text. However, the overall syntax of many languages is context-sensitive, which is classified as Type-1. Some languages, like Perl and Lisp, are even more complex. In Perl, certain constructs can execute code during the parsing phase itself. This can make parsing an "undecidable" problem, meaning the process might never finish. In Lisp, macros can also execute during parsing. This requires the compiler to have a full Lisp run-time system present just to understand the code.

Python add5 parse.svg
Python add5 parse.svg

It is vital to distinguish between syntax and semantics. Syntax describes the form and structure of a program, but it does not provide meaning. Semantics is the study of what the code actually does or what it means when executed. A program can have perfect syntax but be semantically incorrect. For example, in the C language, a programmer might try to use an uninitialized variable. While the code follows the rules of the language's structure, the operation is not semantically defined. This is similar to a human sentence like "Colorless green ideas sleep furiously." The sentence follows grammar rules, but it lacks a logical meaning.

Python add5 syntax.svg
Python add5 syntax.svg

Ultimately, syntax is the essential foundation for all computation. It provides the formal framework that allows a human's intent to be translated into machine instructions. By organizing code into lexical tokens, hierarchical trees, and contextual meanings, computers can navigate complex logic. Whether a language uses a simple LL parser or a more powerful LR parser, the goal remains the same. The system must turn a string of characters into a structured set of actions. Understanding these layers helps us see how the bridge between human thought and machine execution is built.

722 words
🖼️ Images & Media (2)
File:Python add5 syntax.svg
Python add5 syntax.svg
File:Python add5 parse.svg
Python add5 parse.svg
Up Next
💻
Programming language
Technology
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.