Happy 解析器中令人费解的错误消息

发布于 2025-01-03 18:03:17 字数 4335 浏览 1 评论 0原文

我正在使用 Happy 在 Haskell 中构建 JavaScript 解析器,并且遇到一条错误消息,无论我如何努力,我都无法调试。

我无法在这里发布所有代码,因为它有数千行长。我会尝试发布相关内容,如果有人可以帮助我,我将不胜感激!

此错误消息很长,请耐心等待。我省略了大部分 HappyAbsSyn 部分。如果有帮助的话,我可以提供完整的错误消息。

Parser.hs:800:28:
    Couldn't match expected type `Expression'
                with actual type `PrimaryExpr'
    Expected type: Int
                   -> Int
                   -> Token
                   -> HappyState
                        Token
                        (HappyStk
                           (HappyAbsSyn
                              FuncDecl
                              ... etc etc ...
                              PostFix)
                         -> P a1)
                   -> [HappyState
                         Token
                         (HappyStk
                            (HappyAbsSyn
                               FuncDecl
                              ... etc etc ...
                               PostFix)
                          -> P a1)]
                   -> HappyStk
                        (HappyAbsSyn
                           FuncDecl
                              ... etc etc ...
      Actual type: Int
                   -> Int
                   -> Token
                   -> HappyState
                        Token
                        (HappyStk
                           (HappyAbsSyn
                              FuncDecl
                              ... etc etc ...
                              t630)
                         -> P a0)
                   -> [HappyState
                         Token
                         (HappyStk
                            (HappyAbsSyn
                               FuncDecl
                              ... etc etc ...
                               t630)
                          -> P a0)]
                   -> HappyStk
                        (HappyAbsSyn
                           FuncDecl
                              ... etc etc ...
                           t630)
                   -> P a0
    In the first argument of `happyGoto', namely `action_90'
    In the expression: happyGoto action_90

明白了吗?好的,所以我做的第一件事就是查看 Parser.hs 中的第 800 行(Happy 生成的文件)。

799: action_60 (7) = happyGoto action_71
800: action_60 (28) = happyGoto action_90
801: action_60 (33) = happyGoto action_15

action_90 定义为:

action_90 (100) = happyShift action_224
action_90 _ = happyFail

除非我们理解代码对应的内容,否则这显然没有多大意义。我要求 Happy 在编译时生成一个信息文件,这是(我认为)适当的部分:

-----------------------------------------------------------------------------
Grammar
-----------------------------------------------------------------------------
    %start_parse -> program                            (0)
    ... etc etc ...
    statement -> ID ':' statement                      (28)
    ... etc etc ...
    primaryExpr -> THIS                                (100)
    ... etc etc ...

所以看起来它与 ID 语句有关。对于这一点我真的很困惑。有人对我应该从这里去哪里有任何建议吗?我猜想要解决这个问题,您需要查看抽象语法。这是其中的一小部分。再次强调,如果有帮助的话,我很乐意展示更多内容。

data Expression
    = Assignment    Assignment 
    deriving Show

data Assignment
    = CondExpr        CondExpr
    | Assign          LeftExpr AssignOp Assignment 
    | AssignFuncDecl  FuncDecl
    deriving Show

data FuncDecl
    = FuncDecl      (Maybe String) [String] [Source]
    deriving Show

data Statement
    = EmptyStmt
    | IfStmt        IfStmt
    | IterativeStmt IterativeStmt
    | ExprStmt      Expression
    | Block         [Statement]
    | VarStmt       [VarDecl]
    | TryStmt       TryStmt
    | ContinueStmt  (Maybe String)
    | BreakStmt     (Maybe String)
    | ReturnStmt    (Maybe Expression)
    | WithStmt      Expression Statement
    | LabelledStmt  String Statement
    | Switch        Switch
    | ThrowExpr     Expression
    deriving Show

data PrimaryExpr
    = ExpLitInt     Integer
    | ExpLitStr     String
    | ExpId         String
    | ExpBrackExp   PrimaryExpr
    | ExpThis
    | ExpRegex      String
    | ExpArray      ArrayLit
    | ExpObject     [(Property, Assignment)]
    deriving Show

抱歉,这个时间太长了。我已经无计可施了。任何帮助或指示都会很棒。

I'm building a JavaScript parser in Haskell using Happy and I'm running into an error message that, no matter how hard I try, I can't debug.

I can't really post all the code here as it is thousands of lines long. I'll try to post the relevant bits and if anyone can help me I'd be immensely grateful!

This error message is very long so please bear with me. I've left out most of the HappyAbsSyn part. I can provide the full error message if it will help.

Parser.hs:800:28:
    Couldn't match expected type `Expression'
                with actual type `PrimaryExpr'
    Expected type: Int
                   -> Int
                   -> Token
                   -> HappyState
                        Token
                        (HappyStk
                           (HappyAbsSyn
                              FuncDecl
                              ... etc etc ...
                              PostFix)
                         -> P a1)
                   -> [HappyState
                         Token
                         (HappyStk
                            (HappyAbsSyn
                               FuncDecl
                              ... etc etc ...
                               PostFix)
                          -> P a1)]
                   -> HappyStk
                        (HappyAbsSyn
                           FuncDecl
                              ... etc etc ...
      Actual type: Int
                   -> Int
                   -> Token
                   -> HappyState
                        Token
                        (HappyStk
                           (HappyAbsSyn
                              FuncDecl
                              ... etc etc ...
                              t630)
                         -> P a0)
                   -> [HappyState
                         Token
                         (HappyStk
                            (HappyAbsSyn
                               FuncDecl
                              ... etc etc ...
                               t630)
                          -> P a0)]
                   -> HappyStk
                        (HappyAbsSyn
                           FuncDecl
                              ... etc etc ...
                           t630)
                   -> P a0
    In the first argument of `happyGoto', namely `action_90'
    In the expression: happyGoto action_90

Got that? Ok, so the first thing I did was look at line 800 in Parser.hs (The file generated by Happy).

799: action_60 (7) = happyGoto action_71
800: action_60 (28) = happyGoto action_90
801: action_60 (33) = happyGoto action_15

action_90 is defined as:

action_90 (100) = happyShift action_224
action_90 _ = happyFail

This obviously doesn't make much sense unless we understand what the codes correspond to. I asked Happy to generate an info file when I compiled and this is (I think) the appropriate part:

-----------------------------------------------------------------------------
Grammar
-----------------------------------------------------------------------------
    %start_parse -> program                            (0)
    ... etc etc ...
    statement -> ID ':' statement                      (28)
    ... etc etc ...
    primaryExpr -> THIS                                (100)
    ... etc etc ...

So it looks like it's something to do with an ID statement. I'm really quite confused by this point. Does anyone have any suggestions of where I should go from here? I'm guessing that to work through this, you'd need to see the Abstract Syntax. Here is a bit of it. Again, I'm happy to show more if it helps.

data Expression
    = Assignment    Assignment 
    deriving Show

data Assignment
    = CondExpr        CondExpr
    | Assign          LeftExpr AssignOp Assignment 
    | AssignFuncDecl  FuncDecl
    deriving Show

data FuncDecl
    = FuncDecl      (Maybe String) [String] [Source]
    deriving Show

data Statement
    = EmptyStmt
    | IfStmt        IfStmt
    | IterativeStmt IterativeStmt
    | ExprStmt      Expression
    | Block         [Statement]
    | VarStmt       [VarDecl]
    | TryStmt       TryStmt
    | ContinueStmt  (Maybe String)
    | BreakStmt     (Maybe String)
    | ReturnStmt    (Maybe Expression)
    | WithStmt      Expression Statement
    | LabelledStmt  String Statement
    | Switch        Switch
    | ThrowExpr     Expression
    deriving Show

data PrimaryExpr
    = ExpLitInt     Integer
    | ExpLitStr     String
    | ExpId         String
    | ExpBrackExp   PrimaryExpr
    | ExpThis
    | ExpRegex      String
    | ExpArray      ArrayLit
    | ExpObject     [(Property, Assignment)]
    deriving Show

I'm sorry this is so long. I'm at my wits end here. Any help or pointers would be amazing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文