Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add and use custom context parameters during parsing with F# FsYacc?
    text
    copied!<p>I'm using <em>FsLex</em> and <em>FsYacc</em> for string parsing in F# application. During Abstract Syntax Tree (AST) creation parser has to do a decision how to create the AST (make different trees, throw an exception, etc). The parser behaviour must depend on several parameters.</p> <p><a href="http://fsharppowerpack.codeplex.com/wikipage?title=FsYacc" rel="nofollow">Here</a> I've found that it is allowed to declare something like:</p> <pre><code>%type &lt; (context -&gt; context) &gt; toplevel </code></pre> <p>But I could not find how to use this construction and during project compilation have <em>"fsyacc.exe" exited with code 1."</em> error</p> <p>The <strong>Question</strong> is: is it possible and how to use context parameters during parsing with FsYacc?</p> <p><strong>Example</strong> of what I've tried:</p> <pre><code>%start startAst %type &lt; (bool -&gt; MyAst) &gt; startAst %% startAst: | MyAst EOF { (fun (x : bool) -&gt; if x then MyAst.Type1 $1 else MyAst.Type2) } ... </code></pre> <p>and I've expected usage something like this:</p> <pre><code>let lexbuf = Lexing.LexBuffer&lt;char&gt;.FromString text lexbuf |&gt; Lexer.tokenize |&gt; Parser.startAst (ctx: bool) </code></pre> <p>Thanks in advance</p> <p><strong>Update</strong> following exception and call stack is generated during fsyacc.exe execution:</p> <pre><code>Unhandled Exception: System.Exception: more than one input given at FSharp.PowerPack.FsYacc.Driver.clo@67-5.Invoke(String x) at &lt;StartupCode$fsyacc&gt;.$Arg.findMatchingArg$cont@104-1(FSharpRef`1 cursor, FSharpFunc`2 other, String usageText, FSharpList`1 argSpecs, String arg, Unit unitVar) at &lt;StartupCode$fsyacc&gt;.$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 &lt;StartupCode$fsyacc&gt;.$FSharp.PowerPack.FsYacc.Driver.main@() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload