Getting information about the line number at the stage of semantic analysis (using Alex, Happy)

I am doing semantic analysis for an experimental language. I use Alex and Happy to create a lexer and a parser (in fact, I use the BNFC tool to generate Alex and Happy files). I wanted to get an error message with a row number and a column number whenever there is a semantic error, say a type error.

It seems I will need to store the line number information when building my character table or AST. My problem would be solved if I could somehow access the position information in the rules sections of the Happy file.

Any suggestions in this regard would be greatly appreciated.

I tried to implement the answer suggested below, but, unfortunately, was not successful with this. Let's look at a very simple grammar: -

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

My lexer for this looks below.

%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 

}

My Yacc file is below and this is where I struggle. How to insert location information into my syntax tree.

{
{-# 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"

}

When I try to compile the generated Haskell file, I get the following type errors.

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

Can you guys suggest me how to make this work work?

+4
source share
1 answer

Happy, . , , , GHC SrcLoc Haskell.

, posn Alex :

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

( L Token); ( , , Expr + Expr L (combinedPosn [getPos $1, getPos $2, getPos $3] $ PlusExpr (unPos $1) (unPos $3).

+5

Source: https://habr.com/ru/post/1624719/


All Articles