Tuesday, September 20, 2011

Let's Build a Compiler

September 20, 2011 11:01 am

I decided to LaTeX-ify Jack Crenshaw's 'Let's Build a Compiler' series. Between 1988 and 1995 Jack Crenshaw wrote a sixteen-part series on how to build a compiler. This was a non-technical introduction (No Dragon book necessary) and appears to have encouraged many people to try their hand at developing a compiler.

The entire series does not seem to be available now on the original web site but I tracked down a word doc that I used as the basis for this LaTeX version. You can download a pdf copy here. There may still be some tuning of the formatting but the document is in a decent shape. For Delphi users, the compiler series used Pascal.

Download pdf

The LaTeX source code is available on GitHub 

For those interested in writing interpreters, I would also highly recommend the book by Kernighan and Pike where the authors introduce yacc, lex and the programming language hoc. This is one of the easiest introductions to writing an interpreter I have come across (apart perhaps from the book by Brown). What is nice about hoc is how the authors introduce the interpreter step by step. Both books can be bought very cheaply on the second-hand market.

Kernighan, Brian W.; Pike, Rob (1984). The Unix Programming Environment. Prentice Hall. ISBN 013937681X.

Brown, P.J (1992) Writing Interactive Compilers and Interpreters John Wiley & Sons Inc, ISBN 10: 0471100722

Wednesday, September 7, 2011

Scientists Patent the Scientific Method

Originally Posted on  by hsauro

There has been a lot of talk in the news about the patent office awarding obvious or frivolous patents. I recently came across this one awarded to Palsson, Covert and Herrgard. I only read the abstract which is given here:

“The present invention provides a method of refining a biosystem reaction network. The method consists of: (a) providing a mathematical representation of a biosystem; (b) reconciling said mathematical representation of said biosystem; (c) determining differences between observed behavior of a biosystem and in silico behavior of said mathematical representation of said biosystem under similar conditions; (d) modifying a structure of said mathematical representation of said biosystem, and (e) determining differences between said observed behavior of said biosystem and in silico behavior of said modified mathematical representation of said biosystem under similar conditions.”

If I am not mistaken this sounds very much like the scientific method. Does this mean we’re going to have to pay these scientists every time I study cellular biology? There might be more to it in the main text, but from the abstract it sure sounds like they’ve just patented the way we study the world, albeit a specific part of the real world, in this case, biochemical reaction networks.

Sunday, September 4, 2011

Useful tips for fireMonkey and Delphi XE2

Originally Posted on  by hsauro

Here is a list of useful tips for using fireMonkey.

Update (7 Sept 2011): A report from Embarcadero indicates that Anchor support will eventually be provided in FireMonkey.

1. Fonts too fuzzy? You may find that font rendered in controls such as TMemo or TEdit are a bit fuzzy, particularly on a white background. To improve font quality, follow a suggestion from Jermey North:

This could be due to the Direct 2D canvas. Try the GDI+ plus canvas and see if this makes the fonts clearer.

Before Application.Initialize in the project file, add this line:

FMX.Types.GlobalUseDirect2D := False;

You need to put FMX.Types and FMX.Canvas.GDIP in the uses clause as

Works for me. Update 12 Sept: For an even better solution see tip #11.

2. Qualified Font Styles. From Jon Souter, can’t get this to compile?


Use this instead (from Jeremy North and Chris Rolliston):


Styles must now be qualified.

3. Font Size Differences: In VCL, font sizes are specified using points, eg 10 point, that is 72 points per inch. In fireMonkey font sizes are specified using device-independent pixels (DIPs), which at set at 96 per logical inch. A font set at 10 point in foreMonkey will therefore be slightly smaller compared so the same size in a VCL application.

4. Mouse Position: In Windows world we were used to getting the mouse position using GetCursorPos(). For crossplatform, we have to use a different approach. Lukily there is a platform object from which we can obtain the mouse coordinates (from Ralph Wesseling):

uses FMX.Platform;

p : TPointF

p := Platform.GetMousePos;

5. Unit Scope Helper: Drs Bob’s has release a simple browser tool to help you convert VCL unit names to their equivalent FMX names. http://www.bobswart.nl/blog/

6. Multiple Select: If you select multiple controls on a form, the usual selection cues are not visible. However the group of controls is selected, you can move them about as if there were a group.

7. OnKeyDown/KeyUp in Forms: Unlike a VCL form which has over 40 events that one can tap into, a FM form has only 9 events exposed. In particular it has no OneKeyDown or OnKeyUp exposed. In addition, FM forms have no KeyPreview. As recently mentioned by Chris Rolliston, “Looking at the source, I’ve found the situation is in effect an enforced KeyPreview=True – the only window from an OS POV is the form itself, which
then passes on key events to the controls. While there are no OnKeyDown and OnKeyUp events exposed by TForm, you can just override the form’s KeyDown and KeyUp methods:”

  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure KeyDown(var Key: Word; var KeyChar: Char; Shift:
TShiftState); override;


procedure TForm1.KeyDown(var Key: Word; var KeyChar: Char; Shift:
  if Key = vkEscape then

Rolliston goes on to suggest that adding KeyPreview to FM forms should be straightforward and supplies the necessary code to accomplish this.

From https://forums.embarcadero.com/thread.jspa?threadID=60054&tstart=0.

8. Where are my Anchors gone? Possibly one of the biggest surprises for many was the absence of the anchor property in FM controls. This has been discussed before and refer the reader to Update 2 on this page.

9. Glyphs on buttons Question (Gaetan Maerten): I have made a simple app with an OK and Cancel button, and now I am trying to find how I can add the standard OK and Cancel glyphs?

Answer (Chris Rolliston): With the TButton object selected, add a TImage (*not* a TImageControl) to it, assign the image’s Bitmap property, and set the image’s HitTest property to False so that any mouse events float through to the button. You also might want to use a TLabel control for the button’s text instead of using the button’s own Text property, so as to make sure it all aligns nicely.

10. Writing text on a TImage (GIAN 55)

Code to write text on a TImage canvas:

procedure TForm2.Button1Click(Sender: TObject);
LRect: TRectF;
myimage.Bitmap.LoadFromFile(CurrDir+'\bg.bmp');  // delphi need a brackground to write on

LRect.Create(0, 0, 300, 300);
myimage.Bitmap.Canvas.Font.Family:='Times New Roman';
myimage.Bitmap.Canvas.FillText(LRect, 'Delphi XE2 for iOS', True, 255, [],TTextAlign.taLeading,TTextAlign.taCenter);

11. More on Fuzzy Font. Not happy with the fuzzy fonts rendered by fireMonkey? Want to use cleartype? If so consider using a replacement canvas for fireMonkey by Mattias Anderson. Here is the contents of the readme file:



FMX.Canvas.VPR is a new FireMonkey TCanvas implementation that uses the
open source polygon rasterizer VPR for all of its rendering tasks.

How to use:

Add FMX.Canvas.VPR as the first unit in the project file of your
FireMonkey project.


- perfect antialiasing (superior to GDI+ and D2D);
- high performance;
- minimal API dependencies;
- support for gamma correction;
- support for cleartype font rendering;
- can be easily ported to other platforms.

Copyright (C) 2011 by Mattias Andersson

12. FastMM for Delphi XE2. For user of FastMM, Pierre Le Riche has released a version for Delphi XE2. FastMM is a memory manager and debugger tool. For more information consult this blog and this stackoverflow question.

13. Using Lua with Delphi XE2Lua is a high performance, light-weight and easy to use scripting language that games writers often use as a lightweight embedded language to develop the game’s AI for example. Lua4Delphi allows someone to use Lua from within a Delphi application. Delphi3Lua provides full Lua integration.

14. Creating Components at Runtime. Here is a simple example from Peter Soderman for creating a string grid at runtime:

procedure TForm1.Button1Click(Sender: TObject);
  p: TPanel;
  sg: TStringGrid;
  p := TPanel.Create(Self);
  sg := TStringGrid.Create(Self);
  sg.Parent := p;
  p.Parent := Self;
  sg.Align := TAlignLayout.alClient;
  p.Align := TAlignLayout.alLeft;

15. Adding a column to StringGrid. Peter Soderman reports how to add a column to a stringgrid at runtime as well as setting a cell’s text:


To set the text in a given cell:

StringGrid1.Cells[aCol,aRow] := ‘Hello!’;

16. How do I set the colour of the text of a TLabel Set the color of the font in the style editor but other on attributes in the property editor.