รับแผนผังการแยกวิเคราะห์จากสตริงซอร์สโค้ดใน Poly/ML

ฉันกำลังพยายามรวบรวมสตริงซอร์สโค้ดและพิมพ์แผนผังการแยกวิเคราะห์โดยใช้ Poly/ML รหัสต่อไปนี้คอมไพล์ แต่ต้นไม้แยกว่างเปล่า:

fun main () =
    let
        val stream = TextIO.openString "let val a = \"abc\"; val b = \"def\"; val c = a ^ b in print c end";
        val _ = PolyML.compiler (fn () => TextIO.input1 stream, []);
        val (_, parseTree) = !PolyML.IDEInterface.parseTree
    in
        PolyML.print (parseTree);
        PolyML.print (List.length parseTree);
        List.map PolyML.print (parseTree);
        ()
    end

กำลังดำเนินการสิ่งนี้:

$ ./a.out
[...]
0
$

ฉันต้องทำอย่างไรจึงจะได้ parse tree จากคอมไพเลอร์? ฉันยังลองใช้รูปแบบโดยใช้พารามิเตอร์คอมไพเลอร์ CPCompilerResultFun แต่สิ่งนี้ก็ไม่ได้ผลเช่นกัน:

fun main () =
    let
        fun useTree (NONE, _) () =
            (PolyML.print "not parsed"; ())
          | useTree (SOME parseTree, _) () =
            (PolyML.print "parsed"; PolyML.print parseTree; ());

        val stream = TextIO.openString "let val a = \"abc\"; val b = \"def\"; val c = a ^ b in print c end";
        val _ = PolyML.compiler (fn () => TextIO.input1 stream, [PolyML.Compiler.CPCompilerResultFun useTree]);
    in
        ()
    end

การรันสิ่งนี้ไม่ได้สร้างเอาต์พุตใดๆ


person eatonphil    schedule 23.02.2016    source แหล่งที่มา


คำตอบ (1)


ฉันสามารถรับมันได้โดยการระบุตัวเลือกคอมไพเลอร์ PolyML.Compiler.CPCompilerResultFun ช่วยให้คุณสามารถเข้าถึงและบันทึกแผนผังการแยกวิเคราะห์ อย่างไรก็ตาม ฉันไม่สามารถพูดได้มากเกินไปเกี่ยวกับวิธีการนำเสนอต้นไม้แยกวิเคราะห์จริงๆ มีเอกสารประกอบบางอย่างที่นี่ (เว็บไซต์ใช้งานไม่ได้ สำหรับฉัน) แต่ฉันก็ยังไม่เข้าใจมันมากนัก

val resultTrees : PolyML.parseTree list ref = ref [];

fun compilerResultFun (parsetree, codeOpt) =
  let
    val _ =
      case parsetree of
        SOME pt => resultTrees := !resultTrees @ [pt]
      | NONE => ()
  in
    fn () => raise Fail "not implemented"
  end;

val stream = TextIO.openString "val a = 1";

val _ = PolyML.compiler (fn () => TextIO.input1 stream, [
  PolyML.Compiler.CPCompilerResultFun compilerResultFun
]);

val [(a, [PolyML.PTfirstChild b])] = !resultTrees;
val (_, [PolyML.PTfirstChild c, PolyML.PTparent d, PolyML.PTprint e]) = b ();
person Ionuț G. Stan    schedule 24.02.2016
comment
คุณช่วยแสดงให้เห็นคุณค่าของ parse tree ได้ไหม? ฉันใช้รหัสของคุณและลองพิมพ์ (a, b, c ฯลฯ) และมันพิมพ์ fn โดยไม่คำนึงถึง - person eatonphil; 24.02.2016
comment
@eatonphil ฉันไม่รู้ว่าจะสำรวจต้นไม้ได้อย่างไร ฉันจะมองลึกลงไปคืนนี้ ทั้งหมดที่ฉันสามารถพูดได้คืออย่า PolyML.print พวกเขา รันสคริปต์ภายใน REPL มันให้การพิมพ์ที่ค่อนข้างดีกว่า - person Ionuț G. Stan; 24.02.2016
comment
จริงๆแล้วฉันเข้าใจสิ่งที่ฉันทำผิด ค่าเหล่านั้นคือฟังก์ชันที่มีค่าการพิมพ์ที่ค่อนข้างเป็น fn ฉันโทรหาพวกเขาและสามารถตรวจสอบสิ่งที่ฉันคาดหวังได้ชัดเจนขึ้นเล็กน้อย - person eatonphil; 24.02.2016