首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在使用F# FsYacc进行解析时添加和使用自定义上下文参数?

如何在使用F# FsYacc进行解析时添加和使用自定义上下文参数?
EN

Stack Overflow用户
提问于 2012-07-05 10:24:56
回答 1查看 376关注 0票数 3

我使用FsLex和FsYacc在F#应用程序中进行字符串解析。在抽象语法树( AST )创建过程中,解析器必须决定如何创建AST(创建不同的树、抛出异常等)。解析器的行为必须依赖于几个参数。

这里 --我发现它被允许声明如下:

代码语言:javascript
复制
%type < (context -> context) > toplevel

但是我找不到如何使用这个构造,而且在项目编译过程中,代码1中出现了"fsyacc.exe“。

问题是:在使用FsYacc进行解析时,是否有可能以及如何使用上下文参数?

我尝试过的示例

代码语言:javascript
复制
%start startAst

%type < (bool -> MyAst) > startAst

%%   

startAst:
| MyAst EOF { 
                    (fun (x : bool) -> 
                        if x then 
                            MyAst.Type1 $1 
                        else 
                            MyAst.Type2) 
                }
...

我期待着这样的用法:

代码语言:javascript
复制
let lexbuf = Lexing.LexBuffer<char>.FromString text
lexbuf |> Lexer.tokenize |> Parser.startAst (ctx: bool)

提前感谢

执行过程中生成异常和调用堆栈之后的更新:

代码语言:javascript
复制
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@()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-06 09:20:55

很抱歉耽搁了很长时间,但终于有时间了。

是谁写的FsYacc ( F#团队?)错过了功能,可以看到自己在页面上的评论链接。我尝试了几个变体,但这是我唯一能够工作的(注意:这需要在#nowarn "62"文件中的.fsy文件中传播到.fs文件,或者为整个项目提供--nowarn:62 ):

代码语言:javascript
复制
%{

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的消息来源,至少现在没有)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11342379

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档