Nepedia
Back to Discover
Fluent Reader
💻

Syntax (programming languages)

technology Maturity 11-13 Next article →

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
Python add5 syntax

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
Python add5 parse

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
Python add5 syntax

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.

446 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

🖼️ Images & Media (2)
File:Python add5 syntax.svg
Python add5 syntax.svg
File:Python add5 parse.svg
Python add5 parse.svg
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.