It’s a lovely evening here in <?xml:namespace prefix = st1 ns = “urn:schemas-microsoft-com:office:smarttags” />Brussels/st1:place/st1:City and I am sitting on my balcony, I think summer might have finally arrived, except every time I think that we get two days of clouds and rain. The only problem with this is below are two restaurants with out terraces who have plenty of customers, the only problem with this is the smell drifts up into my apartment makes me hungry. <?xml:namespace prefix = o ns = “urn:schemas-microsoft-com:office:office” />/o:p

 /o:p

Anyway I have been neglecting my blog lately, there is a reason for this, but nether the less I thought I should try and do something to correct this./o:p

 /o:p

This sample was inspired by my current programming project. We are building a performance management system for a large Belgian bank; it’s all based found a SQL Server OLAP database/cube. We want to be able to offer our users, the banks performance managers who have a statically finance rather than technical back ground, the opportunity input formulas and have them transformed into MDX to query the cube./o:p

 /o:p

To do this we need to write a parser, it is not yet clear how we will do this, but obviously F# would be a good choice. So in this sample I’ll demonstrate how to build a parser for a simple mathematical language. It’s beyond the scope of the sample to generate MDX, merely of the because there is no practical way I could distribute a OLAP database with the sample. So we’ll generate some MSIL instead as if you are interested in F# you will already have the means to run MSIL./o:p

 /o:p

The parser comes in 3 parts a lexer to generate tokens from the text, the parser definition itself written in fslex and the abstract syntax tree (ast) that will be the result of the parser./o:p

 /o:p

The lexer looks like this:/o:p

 /o:p

let digit = [‘0’-‘9’]/o:p

let whitespace = [’ ‘ ‘\t’ ]/o:p

let newline = (’\n’ | ‘\r’ ‘\n’)/o:p

 /o:p

rule token = parse/o:p

| whitespace      { token lexbuf }/o:p

| newline { token lexbuf }/o:p

| “(”               { LPAREN }/o:p

| “)”               { RPAREN }/o:p

| “*”               { MULTI }/o:p

| “/”               { DIV }/o:p

| “+”               { PLUS }/o:p

| “-”               { MINUS }/o:p

| ‘[’[^‘]’]+‘]’   { ID(lexeme lexbuf) }/o:p

| [‘-’]?digit+(‘.‘digit+)?([‘e”E’]digit+)?   { FLOAT (Double.Parse(lexeme lexbuf)) }/o:p

| eof   { EOF }/o:p

 /o:p

The parser looks like this:/o:p

 /o:p

Expr: ID { Val($1) }/o:p

    | FLOAT {  Float($1)  }/o:p

    | LPAREN Expr RPAREN {  $2  }/o:p

    | Expr MULTI Expr{  Multi($1, $3)  }/o:p

    | Expr DIV Expr{  Div($1, $3)  }/o:p

    | Expr PLUS Expr{  Plus($1, $3)  }/o:p

    | Expr MINUS Expr{  Minus($1, $3)  }/o:p

 /o:p

The ast looks like this:/o:p

 /o:p

type expr = /o:p

  | Val of string /o:p

  | Float of System.Double/o:p

  | Multi of expr * expr/o:p

  | Div of expr * expr/o:p

  | Plus of expr * expr/o:p

  | Minus of expr * expr/o:p

 /o:p

 /o:p

All in all just 62 lines of code. The interping the ast to make IL, takes just another 11 lines of code./o:p

 /o:p

let generate_il e il =/o:p

    let param_count = ref 0 in/o:p

    let rec generate_il_inner e (il : ILGenerator) = /o:p

        match e with/o:p

        | Val name -> il.Emit(OpCodes.Ldarg, !param_count); inc param_count/o:p

        | Multi (e1 , e2) -> generate_il_inner e1 il; generate_il_inner e2 il; il.Emit(OpCodes.Mul)/o:p

        | Div (e1 , e2) -> generate_il_inner e1 il; generate_il_inner e2 il; il.Emit(OpCodes.Div)/o:p

        | Plus (e1 , e2) -> generate_il_inner e1 il; generate_il_inner e2 il; il.Emit(OpCodes.Add)/o:p

        | Minus (e1 , e2) -> generate_il_inner e1 il; generate_il_inner e2 il; il.Emit(OpCodes.Sub)/o:p

        | Float x -> il.Emit(OpCodes.Ldc_R8, x) in/o:p

    generate_il_inner e il;/o:p

    il.Emit(OpCodes.Ret)/o:p

 /o:p

There a further 100 or so lines of F# in the sample, but this is just dealing with creating a form to expose the parser and its results./o:p

 /o:p

The sample also demonstrates using F# with form written in C#. Thing that is sightly unsual about this is we compile the C# form into a library and then use it from F#, a lot of of people would do this the other way round. There are a couple of advantages to this, one that you can use F# lambdas with the form’s events, and secondly you can easily use the ast discimiating union type, which do not work well in C#./o:p

 /o:p

Theres quite a bit more to the sample, and I’ll dig into the details in another post (maybe …). But for now you can download the full sample here./o:p

 /o:p

 /o:p

Feedback:

Feedback was imported from my only blog engine, it’s no longer possible to post feedback here.

re: A simple parser in F# - Dmitri

This is cool! It would be nice to see an MDX generator, though :)