parsing - Creating Simple Parser in F# -
i attempting create extremely simple parser in f# using fslex , fsyacc. @ first, functionality trying achieve allowing program take in string represents addition of integers , output result. example, want parser able take in "5 + 2" , output string "7". interested in string arguments , outputs because import parser excel using excel dna once extend functionality support more operations. however, struggling simple integer addition work correctly.
my lexer.fsl file looks like:
{ module lexer open system open microsoft.fsharp.text.lexing open parser let lexeme = lexbuffer<_>.lexemestring let ops = ["+", plus;] |> map.oflist } let digit = ['0'-'9'] let operator = "+" let integ = digit+ rule lang = parse | integ {int(int32.parse(lexeme lexbuf))} | operator {ops.[lexeme lexbuf]}
my parser.fsy file looks like:
%{ open program %} %token <int>int %token plus %start input %type <int> input %% input: exp {$1} ; exp: | int { $1 } | exp exp plus { $1 + $2 } ;
additionally, have program.fs file acts (extremely small) ast:
module program type value = | int of int type op = plus
finally, have file main.fs supposed test out functionality of interpreter (as import function excel).
module main open exceldna.integration open system open program open parser [<excelfunction(description = "")>] let main () = let x = "5 + 2" let lexbuf = microsoft.fsharp.text.lexing.lexbuffer<_>.fromstring x let y = input lexer.lang lexbuf y
however, when run function, parser doesn't work @ all. when build project, parser.fs , lexer.fs files created correctly. feel there simple missing, have no idea how make function correctly.
Comments
Post a Comment