我使用FsLex和FsYacc在F#应用程序中进行字符串解析。在抽象语法树( AST )创建过程中,解析器必须决定如何创建AST(创建不同的树、抛出异常等)。解析器的行为必须依赖于几个参数。
这里 --我发现它被允许声明如下:
%type < (context -> context) > toplevel但是我找不到如何使用这个构造,而且在项目编译过程中,代码1中出现了"fsyacc.exe“。
问题是:在使用FsYacc进行解析时,是否有可能以及如何使用上下文参数?
我尝试过的示例:
%start startAst
%type < (bool -> MyAst) > startAst
%%
startAst:
| MyAst EOF {
(fun (x : bool) ->
if x then
MyAst.Type1 $1
else
MyAst.Type2)
}
...我期待着这样的用法:
let lexbuf = Lexing.LexBuffer<char>.FromString text
lexbuf |> Lexer.tokenize |> Parser.startAst (ctx: bool)提前感谢
在执行过程中生成异常和调用堆栈之后的更新:
Unhandled Exception: System.Exception: more than one input given
at FSharp.PowerPack.FsYacc.Driver.clo@67-5.Invoke(String x)
at <StartupCode$fsyacc>.$Arg.findMatchingArg$cont@104-1(FSharpRef`1 cursor, FSharpFunc`2 other, String usageText, FSharpList`1 argSpecs, String arg, Unit unitVar)
at <StartupCode$fsyacc>.$Arg.findMatchingArg@64(FSharpRef`1 cursor, String[] argv, FSharpFunc`2 other, String usageText, Int32 nargs, FSharpList`1 argSpecs, String arg, FSharpList`1 args)
at Internal.Utilities.ArgParser.ParsePartial(FSharpRef`1 cursor, String[] argv, IEnumerable`1 arguments, FSharpOption`1 otherArgs, FSharpOption`1 usageText)
at Internal.Utilities.ArgParser.Parse(IEnumerable`1 arguments, FSharpOption`1 otherArgs, FSharpOption`1 usageText)
at <StartupCode$fsyacc>.$FSharp.PowerPack.FsYacc.Driver.main@()发布于 2012-07-06 09:20:55
很抱歉耽搁了很长时间,但终于有时间了。
是谁写的FsYacc ( F#团队?)错过了功能,可以看到自己在页面上的评论链接。我尝试了几个变体,但这是我唯一能够工作的(注意:这需要在#nowarn "62"文件中的.fsy文件中传播到.fs文件,或者为整个项目提供--nowarn:62 ):
%{
open Ast
type ('a,'b) Fun = 'a -> 'b
%}
%start startAst
%token <string> MyAst
%token EOF
%type < (bool, MyAst) Fun > startAst
%%
startAst:
| MyAst EOF { (fun (x : bool) ->
if x then
MyAst.Type1 $1
else
MyAst.Type2) }我不知道为什么(而且没有时间查看FsYacc的消息来源,至少现在没有)。
https://stackoverflow.com/questions/11342379
复制相似问题