Swift Grammar: Its a language thing.

Usually when I learn a new language I tend to jump straight into the “Hello World” example and get my hands wet immediately. Because I have already used the first iteration of Swift and more recently in a project I used Swift 2, I’m going to do this deep dive into the language a little bit differently.

Rather than starting with examples, I’m going to start with the language grammar. Traditionally this is not how people are taught a language, but I find that looking at the grammar of a language gives you a much better understanding of how the language works.

I’m going to work from the grammar as it is defined in the language reference. See, “About the Language Reference” under the title “How to Read the Grammar” for an explanation of the syntax.

First things first, the grammar is made up of tokens. A token consists of the following: An identifier, a keyword, punctuation, a literal or an operator. This can seem very confusing if you have never looked at syntax before, so lets break down a simple statement.

var number = 2

This is a valid token in Swift. var is a keyword, number is our identifier, = is an assignment operator and 2 is a numerical decimal literal. We know that this token is valid because the Swift grammar tells us that it is. In this case the grammar is:

statement → declaration ;optional

declaration → variable-declaration

variable-declarationvariable-declaration-head pattern-initializer-list

variable-declaration-head → attributesoptional declaration-modifiersoptional var

pattern-initializer-list → pattern-initializer | pattern-initializer, pattern-initializer-list

pattern-initializer → pattern initializeroptional

pattern → identifier-pattern type-annotationoptional

identifier-pattern → identifier

identifier → identifier-head identifier-charactersoptional

identifier-head → Upper- or lowercase letter A through Z

identifier-characters → identifier-character identifier-charactersoptional

identifier-character → identifier-head

initializer= expression

expression → try-operatoroptional prefix-expression binary-expressionsoptional

prefix-expression → prefix-operatoroptional postfix-expression

postfix-expression → primary-expression

primary-expression → literal-expression

literal-expression → literal

literal → numeric-literal | string-literal | boolean-literal | nil-literal

numeric-literal → optional integer-literal | optional floating-point-literal

integer-literal → decimal-literal

decimal-literal → decimal-digit decimal-literal-charactersoptional

decimal-digit → Digit 0 through 9

That is an amazing amount of steps that Swift need to evaluate to determine whether such a simple variable declaration is valid grammar or not. As I was going through the grammar I chose the shortest possible path and I’m glad I only chose one digit. I did not even touch on the full grammar for each of the tokens.

As you are learning Swift I’d encourage you to take a look at the grammar of even your simplest statements to get a more in depth understanding of the language.

I personally find this fascinating to go through code like this, but I’m sure that this post is not for everyone. Hang in there, we’ll get to code soon, but from time to time I might refer back to the grammar.

Enjoy digging into the grammar of Swift.

Geek aside

The manual uses a simple way to represent the grammar of Swift using Bachus-Naur Form (BNF). Some might argue this because of the use of the → symbol over a more accepted ::= sequence of characters, but in the end, its just a representation of how the syntax for the language is structured. Read up on BNF to get a better understanding of language grammars and as an added bonus in your learning, do some reading on finite-state machines (FSM), which could be used as a more visual way to represent a language grammar.

Leave a Reply

Your email address will not be published. Required fields are marked *