Search Results

Search found 2 results on 1 pages for 'fparsec'.

Page 1/1 | 1 

  • How to extract data from F# list

    - by David White
    Following up my previous question, I'm slowly getting the hang of FParsec (though I do find it particularly hard to grok). My next newbie F# question is, how do I extract data from the list the parser creates? For example, I loaded the sample code from the previous question into a module called Parser.fs, and added a very simple unit test in a separate module (with the appropriate references). I'm using XUnit: open Xunit [<Fact>] let Parse_1_ShouldReturnListContaining1 () = let interim = Parser.parse("1") Assert.False(List.isEmpty(interim)) let head = interim.Head // I realise that I have only one item in the list this time Assert.Equal("1", ???) Interactively, when I execute parse "1" the response is: val it : Element list = [Number "1"] and by tweaking the list of valid operators, I can run parse "1+1" to get: val it : Element list = [Number "1"; Operator "+"; Number "1"] What do I need to put in place of my ??? in the snippet above? And how do I check that it is a Number, rather than an Operator, etc.?

    Read the article

  • Parsec: backtracking not working

    - by Nathan Sanders
    I am trying to parse F# type syntax. I started writing an [F]Parsec grammar and ran into problems, so I simplified the grammar down to this: type ::= identifier | type -> type identifier ::= [A-Za-z0-9.`]+ After running into problems with FParsec, I switched to Parsec, since I have a full chapter of a book dedicated to explaining it. My code for this grammar is typeP = choice [identP, arrowP] identP = do id <- many1 (digit <|> letter <|> char '.' <|> char '`') -- more complicated code here later return id arrowP = do domain <- typeP string "->" range <- typeP return $ "("++domain++" -> "++range++")" run = parse (do t <- typeP eof return t) "F# type syntax" The problem is that Parsec doesn't backtrack by default, so > run "int" Right "int" -- works! > run "int->int" Left "F# type syntax" unexpected "-" expecting digit, letter, ".", "`" or end of input -- doesn't work! The first thing I tried was to reorder typeP: typeP = choice [arrowP, identP] But this just stack overflows because the grammar is left-recursive--typeP never gets to trying identP because it keeps trying arrowP over and over. Next I tried try in various places, for example: typeP = choice [try identP, arrowP] But nothing I do seems to change the basic behaviours of (1) stack overflow or (2) non-recognition of "-" following an identifier. My mistake is probably obvious to anybody who has successfully written a Parsec grammar. Can somebody point it out?

    Read the article

1