microsoft / prose

Microsoft Program Synthesis using Examples SDK is a framework of technologies for the automatic generation of programs from input-output examples. This repo includes samples and sample data for the Microsoft Program Synthesis using Example SDK.
https://microsoft.github.io/prose/
Other
620 stars 100 forks source link

Parse humanReadable program string #50

Open nhamlv-55 opened 3 years ago

nhamlv-55 commented 3 years ago

Hello, I was following the tutorial, but at this step var ast = ProgramNode.Parse("Id(simpleInput)", grammar, ASTSerializationFormat.HumanReadable); I got Unhandled exception. System.NotSupportedException: Deserializing human-readable ASTs is not supported. In which Id is my identity function that does nothing.

If I use the equivalent XML as the input program_string = "<NonterminalNode symbol=\"simpleOutput\" rule=\"Id\"><VariableNode symbol=\"simpleInput\" /></NonterminalNode>"; and parse with ASTSerializationFormat.XML, then it works.

I look into the code and it seems like only the XML format is currently supported. So given a more complicated function, like "Substring(x, PositionPair(AbsolutePosition(x, 0), AbsolutePosition(x, 5)))", how do I manually construct the XML to test? Thanks.

danpere commented 3 years ago

Sorry, we have not updated the tutorial after we removed support for parsing ASTSerializationFormat.HumanReadable. The ambiguity of parsing such strings caused enough problems that we decided to disable that feature.

I think the XML for that program would be something like this:

<NonterminalNode symbol="?" rule="Substring">
    <VariableNode symbol="x" />
    <NonterminalNode symbol="?" rule="PositionPair">
    </NonterminalNode>
    <NonterminalNode symbol="?" rule="AbsolutePosition">
        <VariableNode symbol="x" />
        <LiteralNode symbol="k"><![CDATA[0]]></LiteralNode>
    </NonterminalNode>
    <NonterminalNode symbol="?" rule="AbsolutePosition">
        <VariableNode symbol="x" />
        <LiteralNode symbol="k"><![CDATA[5]]></LiteralNode>
    </NonterminalNode>
</NonterminalNode>

(I don't know the symbol names off the top of my head, so I've put in ? for those. You should be able to find them in the grammar.)

nhamlv-55 commented 3 years ago

Thank you. Where could I read more about the XML grammar? The XML that you have written for me doesn't look straightforward for me to apply somewhere else. For example, I could not have guessed the <![CDATA[5]]> part. Any pointer to read more about that? Thanks a lot.

My use case is that I need to test my program first (kinda like running it 'forward', before asking Prose to learn the params for it via witness functions) to make sure that it indeed does the transformation that I want.

danpere commented 3 years ago

Sorry, there's no documentation on that. (Apologies, our documentation is a bit lacking; we know.) I found that XML example by running ProgramNode.PrintAst() and seeing what it looked like.

nhamlv-55 commented 3 years ago

Thanks. But given that I only have the grammar and the semantics, how can I construct the ProgramNode? It is also the reason I want to parse the humanReadable string in the first place.