Clarithromycin Market Estimated to Flourish

The clarithromycin market refers to the industry that produces and sells the antibiotic drug clarithromycin, which is used to treat various bacterial infections. Clarithromycin belongs to a class of…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How much do you like sugar ?

Everybody love sugar. It is written deep down in our brain. And every software developer is lazy at least a little bit. So you use programming languages instead of burning bits directly in your computer. (It did this don’t laugh)

Maya Shaving came up with a nice trick to remember the definition:

But the most important keyword to remember is productivity.

If your code is shorter, easier to read, write and review, you are finally more productive… or not. Lets dig into that.

Every “modern” programming language share two main characteristics:

Sound complicated ? Well not that much.

A computer process data ( “The strip of tape”) by executing a sequence of instructions(“table of rules”) coming from a finite set of possible instructions defined by the language’s grammar.

Example (in BASIC)

The instruction is “PRINT” which print a text on the console. and the data is “Hello, World” stored somewhere in memory.

A formal grammar can be represented as a set of rules. And a formal grammar is context free when the same set of rules can always be used. That become tricky.

The best counter example of a context free grammar is most spoken languages liker French or English.

Calculator grammar in ANTLR (shortened)

The following text : 17 = 5 + 4 *3 can be processed by a parser implementing this grammar and will generate the following parser tree :

Complex isn’t it ? We start with 5 symbols (17,=,5,+,4,*, 3) and we end up with 17 nodes.

Well some of them are useless. guess which ones. The blue ones are obviously useless (although not totally useless). The green ones could be simplified.

In this case the tree would look like the following:

Abstract Syntax Tree for the equation : 17 = 5 + 4 * 3

A second interesting point is that you can now execute that tree to get the result. But how ?

Well, use a stack.

You start with the stacked symbols (terminals and productions ) and at each step you “reduce” the elements on the stack.

The symbolic program looks like:

In a more “assembly language” style you get:

As you have seen in the previous example, a program is first converted in a parse tree and this parse tree is executed after some transformations.

In the last example when going from the concrete syntax tree to to abstract syntax tree we removed useless nodes and replaced them by more meaningful ones.

But what if instead we replaced a single node by a whole sub-tree ?

Well, that is exactly what syntactic sugar is all about : defining in the language’s grammar new rules (productions) containing only the meaningful elements and let our compiler (or interpreter) replace the generated nodes produced by these rules by the ones that would have been generated by the rules “replaced” by our new rules.

But there is a caveat : You must modify your compiler (or interpreter).

Well one language went around it : C.

Engineers at the Bell Labs defined around 1973 a new “language” that could be embedded in normal C code and “pre-processed” to replace it with actual code. The major features of this language are:

Why isn’t the C pre-processor not used by Python, Java or JavaScript ?

Are there downsides to using pre-processors that could explain the absence of the C pre-processor in another languages?

Pre-processors still exists albeit in the shadows. In Java, there are still the annotations which are basically a sort of pre-processing functionality embedded directly in the compiler.

The main drawback of the C pre-processor is that it is doing textual replacement. The pre-processor is unaware of the C language. It has no clue if the generated code is valid C code or not.

Even worse, while the initial idea is to make your code easier to read or shorter, it has been widely used to make it more cross-platform/universal. The downside of making your code more universal is that you make it also less readable, which is the total opposite of syntactic sugar and make your code taste bad.

There are other issues with (custom) syntactic sugar:

Syntactic sugar has a long history. From a smart trick to make you life easier to a complex set of features (C pre-processor), it is another tool that can be used for good or evil.

Use is wisely and you improve your productivity, use it too much and you ends up screwing up yourself.

Add a comment

Related posts:

Make Google Sheets Better with Named Ranges

Spreadsheets are real game changers for doing almost any kind of work with data. However, if they get complex it can get very confusing to see a formula like =$A2+C5/(1+D12. After all, can you…

Flexible fuel cells turn sweat into power

When we think of the term fuel cell, the image that comes to our mind is that of a bulky car-battery-sized device which is normally not portable. But recently, researchers turned that visual into a…

Auto loan approval renig?

I was approved for an auto loan from a credit union i recently joined. the conditions were that i get a co-signer, my aunt agreed to co-sign. the car that i originally had in mind was just at the…