Simple lambda calculator with FParsec

I am new to F # and am having a pretty nasty problem. I want to analyze the following grammar:

Application := Expression Expression
Expression  := "(" "lambda" Name "." Application ")"
             | Name
Name        := [a-z]+

This will fit things like (lambda x. (lambda y. x y)) zand (lambda x. x) y.

My problem is that the two rules depend on each other:

let popen = pchar '('
let pclose = pchar ')'
let pname = many1 letter |>> Seq.toArray |>> System.String |>> NameNode
let plambda = pstring "lambda"
let pdot = pchar '.'
let phead = plambda >>. pname .>> pdot
let pexpression = 
        popen >>. pname .>>. papplication .>> pclose |>> ExpressionNode
    <|> pname
let papplication = pexpression .>>. pexpression

pexpressiondepends on papplicationand vicebersa. How can I get rid of this addiction?

+6
source share
1 answer

createParserForwardedToRef. "", , , . "", - , .

"", .

let pexpression, pexpressionImpl = createParserForwardedToRef()
let papplication = pexpression .>>. pexpression
pexpressionImpl := 
       popen >>. pname .>>. papplication .>> pclose |>> ExpressionNode
   <|> pname
+8

All Articles