0

我刚刚写完这段模式匹配(下面附上)。无论我如何尝试修复它,当我尝试编译它时仍然存在“错误:语法错误”。

这段代码在这里:

let rec stmt (s:Ast.stmt) : X86.inst list = 
        let get_label e = 
            let inst = stmt e in 
            let pattern = (List.nth (List.rev inst) 0) in
            let label = match pattern with 
                        |Memory (Store (Addr (Label l), Reg r)) -> l 
                        |_ -> "Error"
        in 
        match s with
        |Exp e -> (match e with
                |Int i -> [movei RAX i; store new_temp() RAX]
                |Var v -> [load RBX v; store new_temp() RBX]                
                |Binop (e1, op, e2) -> let (label1, label2) = (get_label e1, get_label e2) 
                                        in let operation = 
                                          match op with
                                          |Plus -> [Arith (Add (RAX, Reg RBX))]
                                          |Minus -> [Arith (Sub (RAX, Reg RBX))]
                                          |Times -> [Arith (Mul RBX)]
                                          |Div -> [Arith (Div RBX)]
                                          |Eq -> [Arith (Cmp (RAX, RBX))]
                                          |Neq -> [Arith (Cmp (RAX, Reg RBX)); set NE RAX] 
                                          |Lt -> [Arith (Cmp (RAX, Reg RBX)); set L RAX]
                                          |Lte -> [Arith (Cmp (RAX, Reg RBX)); set LE RAX]
                                          |Gt -> [Arith (Cmp (RAX, Reg RBX)); set G RAX]
                                          |Gte -> [Arith (Cmp (RAX, Reg RBX)); set GE RAX]
                                        in [load RAX label1; load RBX label2] @ operation @ [store (new_temp()) RAX] 
                                        

                |Not e -> let label = get_label e in
                          [load RBX label; Arith (Neg RBX); store (new_temp()) RAX] 
                |And (e1, e2) -> let (label1, label2) = (get_label e1, get_label e2) in
                          [load RAX label1; load RBX label2; Bitop (And (RAX, Reg RBX)); store (new_temp()) RAX]
                |Or (e1, e2) -> let (label1, label2) = (get_label e1, get_label e2) in
                          [load RAX label1; load RBX label2; Bitop (Or (RAX, Reg RBX)); store (new_temp()) RAX]
                |Assign (e1, e2) -> let label = get_label e2 in
                          [movei RAX label; store e1 RAX; store (new_temp()) RAX] 
                |_ -> [])
        |Block e  -> []
        |If (l, m, r) -> []
        |While (l, r) -> []
        |For (l, m1, m2, r) -> []
        |Return e -> []
        |_ -> []

let compile (p : Ast.program) : result =
    let _ = reset() in
    let _ = collect_vars(p) in
    let insts = List.concat_map stmt p in
    { code = insts; data = VarSet.elements !variables }

错误在let compile (p: Ast.program) : result =

这是(来自外壳):

110 | let compile (p : Ast.program) : result =
      ^^^
Error: Syntax error
4

1 回答 1

1

该错误源于未正确完成该get_label功能:

        let get_label e = 
            let inst = stmt e in 
            let pattern = (List.nth (List.rev inst) 0) in
            let label = match pattern with 
                        |Memory (Store (Addr (Label l), Reg r)) -> l 
                        |_ -> "Error"

因此,尽管您有缩进,但in以下它与let label = ..., not相关联。let get_label e = ...

我怀疑你想要的是跳过label绑定:

        let get_label e = 
            let inst = stmt e in 
            let pattern = (List.nth (List.rev inst) 0) in
            match pattern with 
            | Memory (Store (Addr (Label l), Reg r)) -> l 
            | _ -> "Error"

使用类似的工具可能是个好主意ocp-indent,它会根据编译器解释代码的方式来缩进你的代码,而不是你希望编译器如何解释它。这通常有助于避免这样的错误:)

于 2021-04-03T17:46:00.617 回答