【问题标题】:Getting line number information in the Semantic Analysis Phase (using Alex,Happy)在语义分析阶段获取行号信息(使用 Alex,Happy)
【发布时间】:2016-01-18 04:11:39
【问题描述】:

我正在为一种实验语言进行语义分析。我正在使用 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

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

【问题讨论】:

    标签: haskell compiler-construction happy alex


    【解决方案1】:

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

    基本上,您可以使用the posn Alex wrapper 将位置信息注入您的令牌类型:

    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) 的规则。

    【讨论】:

      猜你喜欢
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      • 2023-04-02
      • 2013-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多