Expected list of tokens provided by `%errordefaulthandler explist` only lists shift tokens of most recent state
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
Direzione di ricerca
Esegui il riproduttore in issue265.y e ispeziona il comportamento del parser generato in relazione a getDefault e al gestore degli errori di explist. Traccia il modo in cui le riduzioni predefinite e lo stack degli stati del parser determinano i token attesi. Il lavoro è completato quando il caso 11 segnala sia ';' sia '+', con una copertura adeguata per il comportamento dello stato di riduzione riportato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Here's a reproducer exhibiting two issues:
{
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
-- For ancient GHC 7.0.4
{-# LANGUAGE MultiParamTypeClasses #-}
module Main where
import Control.Monad (when)
import Data.Char
import System.Exit
}
%name parseStmts
%tokentype { Token }
%errorhandlertype explist
%error { handleError }
%monad { ParseM } { (>>=) } { return }
%token
'1' { TOne }
'+' { TPlus }
';' { TSemi }
%%
Stmts : {- empty -} { [] }
| Stmt { [$1] }
| Stmts ';' Stmt { $1 ++ [$3] }
Stmt : Exp { ExpStmt $1 }
Exp : '1' { One }
| Exp '+' Exp %shift { Plus $1 $3 }
{
data Token = TOne | TPlus | TSemi
deriving (Eq,Show)
type Stmts = [Stmt]
data Stmt = ExpStmt Exp
deriving (Eq, Show)
data Exp = One | Plus Exp Exp
deriving (Eq, Show)
type ParseM = Either ParseError
data ParseError
= ParseError [String]
deriving Eq
instance Show ParseError where
show (ParseError exp) = "Parse error. Expected: " ++ show exp
recordParseError :: [String] -> ParseM a
recordParseError expected = Left (ParseError expected)
handleError :: [Token] -> [String] -> ParseM a
handleError ts expected = recordParseError expected
lexer :: String -> [Token]
lexer [] = []
lexer (c:cs)
| isSpace c = lexer cs
| c == '1' = TOne:(lexer cs)
| c == '+' = TPlus:(lexer cs)
| c == ';' = TSemi:(lexer cs)
| otherwise = error "lexer error"
main :: IO ()
main = do
test "11" $ \res -> res == Left (ParseError ["';'","'+'"])
where
test inp p = do
putStrLn $ "testing " ++ inp
let tokens = lexer inp
let res = parseStmts tokens
when (not (p res)) $ do
print res
exitWith (ExitFailure 1)
}
Note that it tests an input 11 which leads to a syntax error after the first 1 and checks whether both ; and + were listed as expected tokens.
But if you compile and run it,
$ happy issue265.y && ghc issue265.hs && issue265
testing 11
Left Parse error. Expected: ["';'"]
You can see that it only suggests ;. That is due to two separate issues:
- In order to generate smaller code,
happyreplaces erroring default actions with the most common reduction (seegetDefault). After we shift the first1and detect the error, we end up default-reducing all the way back up toStmts -> Stmts . ';' Stmt, which neglects the itemExp -> Exp . '+' Exp. - Even if
getDefaultwas "fixed", we'd be stuck in the reduction stateExp -> 1 ., where there is no expected shift token whatsoever. That points out another flaw: The implementation of%errorhandlertype explistis insufficient, because it only reports the tokens to be shifted for the topmost state on the stack. This strategy isn't so bad, but we'd better walk the whole state stack as if we successfully reduced and collect all shiftable tokens we encounter on the way (so it's rather not simply "walking the stack" I'm afraid). It ought to be possible to simulate this to get quite context-sensitive expected token lists. The next best solution would be to consider the set of tokens of the topmost state with a shift or a reduce action.
Of course, (2) is infeasible for non-array-based parsers, or at least would require quite a bit of extra code.
- Lingua principale
- Haskell
- Stelle
- 323
- Fork
- 88
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di haskell/happy
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 55/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
Tutte le issue di haskell/happy
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
haskell/bytestring#730 · 1 commento ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
seal-harness/seal-harness#399 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
fmidue/codeworld-tasks#124 ·
-
allow haskell-src-exts-1.24Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
reflex-frp/reflex#563 ·