haskell - 在语义分析阶段获取行号信息(使用 Alex,Happy)

标签 haskell compiler-construction happy alex

我正在为一种实验语言进行语义分析。我正在使用 Alex 和 Happy 来生成词法分析器和解析器(实际上我正在使用 BNFC 工具来生成 Alex 和 Happy 文件)。每当出现语义错误(例如类型错误)时,我都想收到带有行号和列号的错误消息。

似乎我必须在构建符号表或 AST 时存储行号信息。如果我能以某种方式访问​​ Happy 文件的规则部分中的位置信息,我的问题就会得到解决。

在这方面的任何建议将不胜感激。

我尝试实现下面建议的答案,但不幸的是没有取得任何成功。让我们考虑一个非常简单的语法:-

Expr -> Expr + Term
       | Term
Term -> Int

我的词法分析器如下所示。
%wrapper "posn"

$digit = 0-9            -- digits
$alpha = [a-zA-Z]       -- alphabetic characters

tokens :-

  $white+               ;
  "--".*                ;
  $digit+               { \p s -> L {getPos = p , unPos = Tok_Int (read s) }}
  \+                    { \p s -> L {getPos = p , unPos = Tok_Plus} }


{
data L a = L{ getPos :: AlexPosn, unPos :: a } deriving (Eq,Show)

data Token =
      Tok_Plus 
    | Tok_Int Int 
    deriving (Eq,Show)


getToken :: IO [L Token]
getToken = do 
    args <- getArgs
    case length args == 0 of
        True  -> do 
               error $ "\n****************Error: Expecting file name as an argument.\n" 
        False -> do
            let fname  = args !! 0 
            conts <- readFile fname
            let tokens = alexScanTokens conts 
            return tokens 

}

我的 Yacc 文件如下,这就是我苦苦挣扎的地方。如何在我的语法树中嵌入位置信息。
{
{-# OPTIONS_GHC -fno-warn-incomplete-patterns -fno-warn-overlapping-patterns #-}
module Parser where
import Lexer

}

%name pExpr Exp 
%name pTerm Term 

%tokentype {L Token}
%error { parseError }

%token
      int             { L { getPos = _,unPos = Tok_Int $$ } }
      '+'             { L { getPos = _,unPos = Tok_Plus } }

%%
Exp :: {L Expr} 
Exp  : Exp '+' Term           { L { getPos =  getPos $1 , unPos = EAdd (unPos $1) (unPos $3) } }
     | Term                   { $1 }

Term :: {L Expr}
Term : int                   { L {getPos =  getPos $1, unPos =  EInt (unPos $1) } } 

{

data Expr =  EAdd Expr Expr 
            | EInt Int 
            deriving (Eq,Show)


returnM :: a -> Err a
returnM = return

thenM :: Err a -> (a -> Err b) -> Err b
thenM = (>>=)


parseError :: [L Token] -> a
parseError _ = error "Parse error"

}

尝试编译生成的 Haskell 文件时出现以下类型错误。
Parser.hs:109:39:
    Couldn't match expected type `L a0' with actual type `Int'
    In the first argument of `getPos', namely `happy_var_1'
    In the `getPos' field of a record
    In the first argument of `HappyAbsSyn5', namely
      `(L {getPos = getPos happy_var_1,
           unPos = EInt (unPos happy_var_1)})'

Parser.hs:109:73:
    Couldn't match expected type `L Int' with actual type `Int'
    In the first argument of `unPos', namely `happy_var_1'
    In the first argument of `EInt', namely `(unPos happy_var_1)'
    In the `unPos' field of a record

你们能建议我如何让这个东西工作吗?

最佳答案

如果您的词法分析器输出中可用,您可以访问 Happy 规则中的位置信息。这正是例如GHC 本身把 SrcLoc s 到它自己的 Haskell 代码的内部表示。

基本上,你会使用 the posn Alex wrapper将位置信息注入(inject)您的 token 类型:

data L a = L{ getPos :: AlexPosn, unPos :: a }

(因此您的 Alex 标记器将返回 L Token 值);然后您将 Happy 规则中的各个标记位置组合成非终结符的位置(例如,您可以拥有从 Expr + ExprL (combinedPosn [getPos $1, getPos $2, getPos $3] $ PlusExpr (unPos $1) (unPos $3) 的规则。

关于haskell - 在语义分析阶段获取行号信息(使用 Alex,Happy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34847211/

相关文章:

c++ - VC++ : Weird compiler errors

haskell - 在快乐解析器中指定替代项的顺序

haskell写大字符串

haskell - Functor -> Applicative -> Monad 层次结构的意义是什么

haskell - 生成范围标准时类型不匹配

java - 从编译器中提取源代码元数据

haskell - 修改类似 Fedora 的发行版上的 XMonad 配置

compiler-construction - 基于寄存器的体系结构比堆栈体系结构快多少?

Haskell Happy 实现分配给变量

haskell - Happy 中的 %% 是什么意思?