February 2006

Syntax and Context / Grammar and Programming

Comparing a spoken language to most contemporary programming languages might be a reach [1]. Is it possible that the structure of grammar might be comparable to programming? There are two extremely simple ways that grammar, English grammar in particular [2], can be compared to programming statements: syntax and context.


One definition of syntax is [3]:

That part of grammar which treats of the construction of sentences; the due arrangement of words in sentences in their necessary relations, according to established usage in any language. [1913 Webster]

On the C Programming Language

The C language has very strict typing and checking. In a sense, the C language can be compared to when grammar is used in very strict terms or perhaps an easier way to think about it - not hackney. Not all languages, including English, apply the rule of being overly strict all of the time. In very simple sentences correct syntax is the rule of thumb.

        The boy is tall.

Pretty straightforward, if the structure changes, and the rules for English broken, the sentence becomes very difficult to follow:

        Tall is the boy

Is this saying the same thing? No, it might be asking a question or is just malformed by accident (or you are Yoda). Doing something similar in C can simply break it:

        char *boy  = "tall";

Works - but -

        "tall" = char *boy;

does not. In general terms, strict grammatical and some programming syntax share a similar set of relations. A more complicated example might be looking at iteration. Iteration is not so easy to do in typed words because pages do not loop by themselves. Conversation can create an iterative function:

"Is it nice out today?"
"Then I am going to work"

Simple iteration; look for something and either act upon it, look for something else or go on.

/* in some other part of the program */
char *weather = "lousy";


if (strcmp(weather,"lousy"))

return 0;

Although the action of iteration will appear similar between many programming languages it is still important to note that strong typing and syntax are among the most important aspects of C. Understanding this makes looking at context all that much more interesting.


A good definition for context (within the context of this article) is [4]:

n 1: discourse that surrounds a language unit and helps to determine its interpretation [syn: {linguistic context}, {context of use}]
2: the set of facts or circumstances that surround a situation or event; "the historical context" [syn: {circumstance}]

The Perl Programming Language

Whereas C is based on strict structure and typing, Perl is much more context based. By this, it means a basic Perl data type, the scalar, can be anything based on the context it is represented in. The similarity to grammar is the concept of context itself.

English context for some can be difficult to master because different words have different meanings in different contexts. Such as:

"To say I like that job is akin to loving glass eating."
"To say I like that beer is similar to asking me if I mind eating to live."

The first sentence is a negative statement. The second statement positive (if not a lame attempt at humor). Both use the word like. The sentence connotation is distinguished by information that comes later and puts the word like in the proper context. By means of pure context the most basic Perl variable, a scalar, is similar:

        $like = "yes";
        $like = 1;
        $like = 'no';
        $like = `NO`;

All of those are a scalar of the same name but defined later in the statement by more information, in this case, the actual data itself surrounded by (or not) ticks, quotes or double quotes. What can be even more interesting (or distressing depending upon your point of view) is how Perl can change the literal context:

[mui@vela:~$] cat foo.pl 
$bar = "ooga wooga";
@foo = $bar;
print "@foo\n";
push (@foo, "booga googa");
push (@foo, 42);
print "@foo\n";

[mui@vela:~$] perl foo.pl 
ooga wooga
ooga wooga booga googa 42

The above is perfectly legal and of course quite useful.

Context also dictates that the actions of something later can affect the context of a word. In Perl, this can very easily be summarized:

sub somesub {
        if ("somedir") {
                return "somedir";
        } else {
                return 1;

If the function is assigning a variable then that variable can have a totally different type of value depending on the circumstances. In truth, they are just bits and bytes of course and Perl magically translates the difference, semantically, however, they are two different types of data.

Other Comparisons

Other programming languages can be laid side by side to metaphors. Object oriented programming is probably one of the most obvious programming language types that does so in a very literal sense. List Processing languages are based on recursive thought mechanisms (a simplification of course).


To know something such as the difference between strict syntax and loose context can help a programmer to better understand why things work (or don't) the way they do.

  1. There are natural language programming languages.
  2. English is used in this text, however, others could be used for sure.
  3. From The Collaborative International Dictionary of English v.0.48 [gcide]
  4. From WordNet (r) 2.0 [wn]