Questions tagged [syntax]

Syntax refers to the actual language elements and symbols themselves. Questions should be tagged as syntax when the question specifically and almost completely relates to syntax alone. This tag should be used with a specific language tag

Definition

In computer science, the syntax of a programming language is the set of rules that define the combinations of symbols that are considered to be correctly structured programs in that language. The syntax of a language defines its surface form. Text-based programming languages are based on sequences of characters, while visual programming languages are based on the spatial layout and connections between symbols (which may be textual or graphical).

The lexical grammar of a textual language specifies how characters must be chunked into tokens. Other syntax rules specify the permissible sequences of these tokens and the process of assigning meaning to these token sequences is part of semantics.

The syntactic analysis of source code usually entails the transformation of the linear sequence of tokens into a hierarchical syntax tree (abstract syntax trees are one convenient form of syntax tree). This process is called parsing, as it is in syntactic analysis in linguistics. Tools have been written that automatically generate parsers from a specification of a language grammar written in Backus-Naur form, e.g., Yacc (yet another compiler compiler).

Source: Syntax (Programming Languages) - Wikipedia

Usage

On StackOverflow, questions should be tagged as syntax when the question specifically and almost completely relates to syntax alone. Additionally, a tag should be added corresponding to the language that is being used, as the syntax only makes sense in the context of the language. Adding the correct language tag will ensure that the question will reach an audience capable of answering it.

21432 questions
408
votes
32 answers

Accessing dict keys like an attribute?

I find it more convenient to access dict keys as obj.foo instead of obj['foo'], so I wrote this snippet: class AttributeDict(dict): def __getattr__(self, attr): return self[attr] def __setattr__(self, attr, value): self[attr]…
Izz ad-Din Ruhulessin
  • 5,955
  • 7
  • 28
  • 28
408
votes
10 answers

What is /dev/null 2>&1?

I found this piece of code in /etc/cron.daily/apf #!/bin/bash /etc/apf/apf -f >> /dev/null 2>&1 /etc/apf/apf -s >> /dev/null 2>&1 It's flushing and reloading the firewall. I don't understand the >> /dev/null 2>&1 part. What is the…
resting
  • 16,287
  • 16
  • 59
  • 90
405
votes
8 answers

What is the correct syntax of ng-include?

I’m trying to include an HTML snippet inside of an ng-repeat, but I can’t get the include to work. It seems the current syntax of ng-include is different than what it was previously: I see many examples using
Jakob Jingleheimer
  • 30,952
  • 27
  • 76
  • 126
402
votes
10 answers

Explain the encapsulated anonymous function syntax

Summary Can you explain the reasoning behind the syntax for encapsulated anonymous functions in JavaScript? Why does this work: (function(){})(); but this doesn't: function(){}();? What I know In JavaScript, one creates a named function like…
Prem
  • 15,911
  • 11
  • 31
  • 35
399
votes
13 answers

Else clause on Python while statement

I've noticed the following code is legal in Python. My question is why? Is there a specific reason? n = 5 while n != 0: print n n -= 1 else: print "what the..." Many beginners accidentally stumble on this syntax when they try to put an…
Ivan
  • 28,999
  • 6
  • 24
  • 21
397
votes
8 answers

How do you express binary literals in Python?

How do you express an integer as a binary number with Python literals? I was easily able to find the answer for hex: >>> 0x12AF 4783 >>> 0x100 256 and octal: >>> 01267 695 >>> 0100 64 How do you use literals to express binary in Python? Summary…
Justin Standard
  • 21,347
  • 22
  • 80
  • 89
393
votes
17 answers

Why avoid increment ("++") and decrement ("--") operators in JavaScript?

One of the tips for jslint tool is: ++ and -- The ++ (increment) and -- (decrement) operators have been known to contribute to bad code by encouraging excessive trickiness. They are second only to faulty architecture in enabling to viruses and…
artlung
  • 33,305
  • 16
  • 69
  • 121
391
votes
5 answers

What is the 'open' keyword in Swift?

The ObjectiveC.swift file from the standard library contains the following few lines of code around line 228: extension NSObject : Equatable, Hashable { /// ... open var hashValue: Int { return hash } } What does open var mean in this…
Clashsoft
  • 11,553
  • 5
  • 40
  • 79
389
votes
11 answers

What is :: (double colon) in Python when subscripting sequences?

I know that I can use something like string[3:4] to get a substring in Python, but what does the 3 mean in somesequence[::3]?
Aillyn
  • 23,354
  • 24
  • 59
  • 84
385
votes
6 answers

Case statement with multiple values in each 'when' block

The best way I can describe what I'm looking for is to show you the failed code I've tried thus far: case car when ['honda', 'acura'].include?(car) # code when 'toyota' || 'lexus' # code end I've got about 4 or 5 different when…
Nick
  • 9,493
  • 8
  • 43
  • 66
377
votes
8 answers

What does the `forall` keyword in Haskell/GHC do?

I'm beginning to understand how the forall keyword is used in so-called "existential types" like this: data ShowBox = forall s. Show s => SB s This is only a subset, however, of how forall is used and I simply cannot wrap my mind around its use in…
JUST MY correct OPINION
  • 35,674
  • 17
  • 77
  • 99
376
votes
2 answers

In Ruby, how do I skip a loop in a .each loop, similar to 'continue'

In Ruby, how do I skip a loop in a .each loop, similar to continue in other languages?
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
373
votes
13 answers

Is there a difference between "pass" and "continue" in a for loop in Python?

Is there any significant difference between the two Python keywords continue and pass like in the examples for element in some_list: if not element: pass and for element in some_list: if not element: continue I should be…
Aufwind
  • 25,310
  • 38
  • 109
  • 154
370
votes
9 answers

What is the formal difference in Scala between braces and parentheses, and when should they be used?

What is the formal difference between passing arguments to functions in parentheses () and in braces {}? The feeling I got from the Programming in Scala book is that Scala's pretty flexible and I should use the one I like best, but I find that some…
Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
367
votes
6 answers

`from ... import` vs `import .`

I'm wondering if there's any difference between the code fragment from urllib import request and the fragment import urllib.request or if they are interchangeable. If they are interchangeable, which is the "standard"/"preferred" syntax (if there…
wchargin
  • 15,589
  • 12
  • 71
  • 110