ArticleslgStudy

science

Syntax highlighting

Syntax highlighting is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Syntax highlighting rather than just read about it. In short: Syntax highlighting is a feature of text editors that is used for programming, scripting, or markup languages, such as HTML. The feature displays text, especially source code, in different colours and fonts according to the category of terms.

Syntax highlighting — main illustration
Syntax highlighting — illustration

Key takeaways

  • Syntax highlighting belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Syntax highlighting to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Syntax highlighting from memory before moving on to harder problems.

Reference excerpt

Syntax highlighting is a feature of text editors that is used for programming, scripting, or markup languages, such as HTML. The feature displays text, especially source code, in different colours and fonts according to the category of terms. This feature facilitates writing in a structured language such as a programming language or a markup language as both structures and syntax errors are visually distinct. This feature is also employed in many programming related contexts (such as programming manuals), either in the form of colourful books or online websites to make understanding code snippets easier for readers. Highlighting does not affect the meaning of the text itself; it is intended only for assisting human readers. Syntax highlighting is a form of secondary notation, since the highlights are not part of the text meaning, but serve to reinforce it. Some editors also integrate syntax highlighting with other features, such as spell checking or code folding, as aids to editing which are external to the language.

Practical benefits

Syntax highlighting is one strategy to improve the readability and context of the text, especially for code that spans several pages. The reader can easily ignore large sections of comments or code, depending on what they are looking for. Syntax highlighting also helps programmers find errors in their program. For example, most editors highlight string literals in a different colour. Consequently, spotting a missing delimiter is much easier because of the contrasting colour of the text. Brace matching is another important feature with many popular editors. This makes it simple to see if a brace has been left out or locate the match of the brace the cursor is on by highlighting the pair in a different colour. A study published in the conference PPIG evaluated the effects of syntax highlighting on the comprehension of short programs, finding that the presence of syntax highlighting significantly reduces the time taken for a programmer to internalise the semantics of a program. Additionally, data gathered from an eye-tracker during the study suggested that syntax highlighting enables programmers to pay less attention to standard syntactic components such as keywords.

Support in text editors

Some text editors can also export the coloured markup in a format that is suitable for printing or for importing into word-processing and other kinds of text-formatting software; for instance as a HTML, colourized LaTeX, PostScript or RTF version of its syntax highlighting. There are several syntax highlighting libraries or "engines" that can be used in other applications, but are not complete programs in themselves, for example the Generic Syntax Highlighter (GeSHi) extension for PHP. For editors that support more than one language, the user can usually specify the language of the text, such as C, LaTeX, HTML, or the text editor can automatically recognize it based on the file extension or by scanning contents of the file. This automatic language detection presents potential problems. For example, a user may want to edit a document containing:

more than one language (for example when editing an HTML file that contains embedded JavaScript code), a language that is not recognized (for example when editing source code for an obscure or relatively new programming language), a language that differs from the file type (for example when editing source code in an extension-less file in an editor that uses file extensions to detect the language). In these cases, it is not clear what language to use, and a document may not be highlighted or be highlighted incorrectly. Some tools, like Guesslang and PLangRec, are designed to detect the programming language from the source code.

Syntax elements Most editors with syntax highlighting allow different colours and text styles to be given to dozens of different lexical sub-elements of syntax. These include keywords, comments, control-flow statements, variables, and other elements. Programmers often heavily customize their settings in an attempt to show as much useful information as possible without making the code difficult to read. Called syntax decoration, some editors also display certain syntactical elements in more visually pleasing ways, for example by replacing a pointer operator like -> in source code by an actual arrow symbol (→), or changing text decoration clues like /italics/, *boldface*, or _underline_ in source code comments by an actual italics, boldface, or underlined presentation.

Examples Below is a comparison of a snippet of C code:

Below is another snippet of syntax highlighted C++ code:

In the C++ example, the editor recognizes and highlights several C++ keywords. The comment at the beginning is also highlighted in a specific manner to distinguish it from working code.

… excerpt ends here. Continue reading the full article.

Illustrations

Syntax highlighting: HTML syntax highlighting
HTML syntax highlighting
Syntax highlighting: Highlighting the effect of missing delimiter (after watch='false) in JavaScript
Highlighting the effect of missing delimiter (after watch='false) in JavaScript
Syntax highlighting: TextMate Ruby syntax highlighting
TextMate Ruby syntax highlighting

Worked examples

Example 1 — a first encounter with Syntax highlighting

Start with the simplest possible case. Write down what Syntax highlighting claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Syntax highlighting before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Syntax highlighting ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Syntax highlighting

In research
Syntax highlighting appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Syntax highlighting in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Syntax highlighting is common in secondary-school and first-year university syllabi. It links to neighbouring topics Programming language syntax, Text editor features, User interface techniques, so understanding it makes those chapters shorter.
In everyday life
Look for Syntax highlighting outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Syntax highlighting in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Syntax highlighting means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Syntax highlighting out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Syntax highlighting in simple terms?

Syntax highlighting is a feature of text editors that is used for programming, scripting, or markup languages, such as HTML. The feature displays text, especially source code, in different colours and fonts according to the category of terms.

Why does Syntax highlighting matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Syntax highlighting?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Syntax highlighting.

Tags

  • Programming language syntax
  • Text editor features
  • User interface techniques

Keep exploring