天道酬勤,学无止境

fparsec

以乘法的优先级解析“xyz”(Parsing “x y z” with the precedence of multiply)

问题 我正在尝试使用 FParsec 为 F# 中的 Mathematica 语言编写解析器。 我为 MiniML 编写了一个支持语法fxy = (f(x))(y)的函数应用程序具有高优先级。 现在我需要使用相同的语法来表示f*x*y ,因此,与乘法具有相同的优先级。 特别是, xy + 2 = x*y + 2而xy ^ 2 = x * y^2 。 如何做到这一点? 回答1 正如 Stephan 在评论中指出的那样,您可以将运算符解析器拆分为两个单独的解析器,并将您自己的解析器放在中间以进行空格分隔的表达式。 以下代码演示了这一点: #I "../packages/FParsec.1.0.1/lib/net40-client" #r "FParsec" #r "FParsecCS" open FParsec open System.Numerics type Expr = | Int of BigInteger | Add of Expr * Expr | Mul of Expr * Expr | Pow of Expr * Expr let str s = pstring s >>. spaces let pInt : Parser<_, unit> = many1Satisfy isDigit |>> BigInteger.Parse .>> spaces let high =

2021-09-19 15:58:17    分类:技术分享    f#   fparsec

是否可以使用 fparsec 解析“off-side”(基于缩进的)语言?(Is possible to parse “off-side” (indentation-based) languages with fparsec?)

问题 我希望将 FParsec 用于类似 python 的语言,基于缩进。 我知道这必须在词法分析阶段完成,但 FParsec 没有词法分析阶段。 是否可以使用 FParsec,或者,如何在词法分析后提供它? PD:我是 F# 新手,但对其他语言有经验 回答1 是的,这是可能的。 这是 FParsec 作者的一篇相关文章。 如果你想更深入地研究这个主题,这篇论文可能值得一读。 论文指出,有多个基于Parsec的缩进感知解析包,Parsec是启发FParsec的解析器组合器。 FParsec 没有单独的词法分析阶段,而是将词法分析和解析融合到一个阶段。 与解析器生成器 (fslex/fsyacc) 相比,IMO 缩进感知解析更适合使用解析器组合器 (FParsec) 完成。 原因是您需要手动跟踪当前缩进并根据上下文报告良好的错误消息。

2021-09-16 02:24:21    分类:技术分享    parsing   f#   indentation   fparsec

Indentation, expressions, statements and StackOverflowException with FParsec - Errors

I test indentation with FParsec, according to this implementation, but when I make it a little more complex by adding expressions (literals, lists, tuples and arithmetic operations), allowing expressions to top-level, and adding a variable creation statement; I first get a StackOverflowException error . In my opinion, this is because the expression parser is solicited in such a way as to make an infinite loop in the program. I see no other reason, however, I don't know how to fix this problem. If I remove the attempt pexpression from my parser data statement, there is no more

2021-09-06 00:31:56    分类:问答    f#   fparsec

FParsec only parses expr between parentheses

I am coding a parser (for learning pourpuses). I want it to parse constructions like let myVar be 40 plus 2 and let myVar be (40 plus 2) With no problems... but my parser does not "understand" the former. It sees the 40 and thinks "well, it's a Literal Numeric 40". When I put parentheses, my parser works great. I am having a hard time to understand why. Parser: type value = | Boolean of bool | Numeric of float | String of string type arithmetic = Sum | Sub | Mul | Div | Pow type logic = And | Or | Equal | NotEqual | Greater | Smaller type identifier = | Identifier of string type expression = |

2021-09-05 16:51:54    分类:问答    parsing   f#   trace   fparsec

Differentiating logical from other infix operators

I'm trying to parse SQL search conditions and having trouble getting the parser to differentiate logical (AND, OR) from other infix operators. I'm parsing them as different nodes (perhaps that's difficult to do), but simplifies the evaluation phase. Here's the relevant code snippet (I can include more if necessary). let opp = OperatorPrecedenceParser<_,_,_>() let scalarExpr = opp.ExpressionParser opp.TermParser <- constant <|> id <|> between lparen rparen scalarExpr <|> scalarExpr //infix operators added here let comparison = //(e.g., 1 < 2) let compareExpr = pipe3 scalarExpr compareOp

2021-08-31 01:15:57    分类:问答    sql   parsing   f#   parser-combinators   fparsec

FParsec: how to parse date in fparsec (newbie)

I am using the Bill Casarin post on how to parse delimited files with fparsec, I am dumbing the logic down to get an understanding of how the code works. I am parsing a multi row delimited document into Cell list list structure (for now) where a Cell is a string or a float. I am a complete newbie on this. I am having issues parsing the floats - in a typical case (a cell delimitted by tabs, containing a numeric) it works. However when a cell happens to be a string that starts with a number - it falls apart. How do I modify pFloatCell to either parse (although the way through the tab) as a float

2021-08-12 07:21:16    分类:问答    f#   parsec   fparsec

FParsec: backtracking `sepBy`

Consider the following toy grammar and parser: (* in EBNF: ap = "a", { "ba" } bp = ap, "bc" *) let ap = sepBy1 (pstring "a") (pstring "b") let bp = ap .>> (pstring "bc") let test = run bp "abababc" I get the following output: Error in Ln: 1 Col: 7 abababc ^ Expecting: 'a' Clearly sepBy1 sees the last b and expects it to lead into another a, failing when it doesn't find one. Is there a variant of sepBy1 which would backtrack over b and make this parse succeed? Is there any reason why I shouldn't use that instead?

2021-07-29 18:52:22    分类:问答    f#   parser-combinators   fparsec

Is possible to parse “off-side” (indentation-based) languages with fparsec?

I wish to use FParsec for a python-like language, indentation-based. I understand that this must be done in the lexing phase, but FParsec don't have a lexing phase. Is possible to use FParsec, or, how can feed it after lexing? P.D: I'm new at F#, but experienced in other languages

2021-07-13 03:47:00    分类:问答    parsing   f#   indentation   fparsec

FParsec failing on optional parser

I am currently learning the FParsec library, but I have come across an issue. When I want to parse an optional string and continue parsing as normal afterwards, FParsec will return a fatal error on the optional parser, rather than returning None as I expect. The below working code sample illustrates my point: open System open FParsec type AccountEntity = | Default | Entity of string let pEntity = let isEntityFirstChar c = isLetter c let isEntityChar c = isLetter c || isDigit c (many1Satisfy2L isEntityFirstChar isEntityChar "entity") .>> skipString "/" let pOptEntity = opt pEntity |>> (fun

2021-06-25 02:50:25    分类:问答    parsing   f#   fparsec

Fparsec 递归语法抛出 StackOverflowException(Fparsec recursive grammatics throw StackOverflowException)

问题 我有这个代码 type Exprs = | Val of float | Mult of Exprs * Exprs | Plus of Exprs * Exprs let pexpr, exprRef = createParserForwardedToRef<Exprs, unit>() let pval = pfloat |>> Val let binaryOp s = (ws >>. pexpr.>> ws) .>>. (ws >>. str s >>. ws >>. pexpr) let pplus = binaryOp "+" |>> Plus let pmuil = binaryOp "*" |>> Mult do exprRef := choice [ attempt pmuil pplus pval ] let expression = ws >>. pexpr .>> ws 当它评估时,它会抛出 StackoverflowExcpetion。 所以问题是如何在没有无限递归的情况下编写它?

2021-06-24 06:06:52    分类:技术分享    parsing   f#   fparsec