-- File generated by the BNF Converter. -- Parser definition for use with Happy. { {-# OPTIONS_GHC -fno-warn-incomplete-patterns -fno-warn-overlapping-patterns #-} {-# LANGUAGE PatternSynonyms #-} module ParC4 ( happyError , myLexer , pProgr , pListDec , pListStm , pDec , pListIdent , pStm , pExp , pExp0 , pExp1 , pExp2 , pExp3 , pOp1 , pOp2 , pOp0 , pOp , pTyp ) where import Prelude import qualified AbsC4 import LexC4 } %name pProgr_internal Progr %name pListDec_internal ListDec %name pListStm_internal ListStm %name pDec_internal Dec %name pListIdent_internal ListIdent %name pStm_internal Stm %name pExp_internal Exp %name pExp0_internal Exp0 %name pExp1_internal Exp1 %name pExp2_internal Exp2 %name pExp3_internal Exp3 %name pOp1_internal Op1 %name pOp2_internal Op2 %name pOp0_internal Op0 %name pOp_internal Op %name pTyp_internal Typ %monad { Err } { (>>=) } { return } %tokentype {Token} %token '&&' { PT _ (TS _ 1) } '(' { PT _ (TS _ 2) } ')' { PT _ (TS _ 3) } '*' { PT _ (TS _ 4) } '+' { PT _ (TS _ 5) } '++' { PT _ (TS _ 6) } ',' { PT _ (TS _ 7) } '-' { PT _ (TS _ 8) } '--' { PT _ (TS _ 9) } ';' { PT _ (TS _ 10) } '<' { PT _ (TS _ 11) } '=' { PT _ (TS _ 12) } '==' { PT _ (TS _ 13) } '>' { PT _ (TS _ 14) } 'double' { PT _ (TS _ 15) } 'else' { PT _ (TS _ 16) } 'if' { PT _ (TS _ 17) } 'int' { PT _ (TS _ 18) } 'main' { PT _ (TS _ 19) } 'printInt' { PT _ (TS _ 20) } 'return' { PT _ (TS _ 21) } 'while' { PT _ (TS _ 22) } '{' { PT _ (TS _ 23) } '||' { PT _ (TS _ 24) } '}' { PT _ (TS _ 25) } L_Ident { PT _ (TV _)} L_charac { PT _ (TC _)} L_doubl { PT _ (TD _)} L_integ { PT _ (TI _)} L_quoted { PT _ (TL _)} %% Ident :: { (AbsC4.BNFC'Position, AbsC4.Ident) } Ident : L_Ident { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), AbsC4.Ident (tokenText $1)) } Char :: { (AbsC4.BNFC'Position, Char) } Char : L_charac { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), (read (tokenText $1) ) :: Char) } Double :: { (AbsC4.BNFC'Position, Double) } Double : L_doubl { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), (read (tokenText $1) ) :: Double) } Integer :: { (AbsC4.BNFC'Position, Integer) } Integer : L_integ { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), (read (tokenText $1) ) :: Integer) } String :: { (AbsC4.BNFC'Position, String) } String : L_quoted { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), ((\(PT _ (TL s)) -> s) $1)) } Progr :: { (AbsC4.BNFC'Position, AbsC4.Progr) } Progr : 'int' 'main' '(' ')' '{' ListDec ListStm '}' { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), AbsC4.Program (uncurry AbsC4.BNFC'Position (tokenLineCol $1)) (snd $6) (snd $7)) } ListDec :: { (AbsC4.BNFC'Position, [AbsC4.Dec]) } ListDec : {- empty -} { (AbsC4.BNFC'NoPosition, []) } | Dec ';' ListDec { (fst $1, (:) (snd $1) (snd $3)) } ListStm :: { (AbsC4.BNFC'Position, [AbsC4.Stm]) } ListStm : {- empty -} { (AbsC4.BNFC'NoPosition, []) } | Stm ListStm { (fst $1, (:) (snd $1) (snd $2)) } Dec :: { (AbsC4.BNFC'Position, AbsC4.Dec) } Dec : Typ ListIdent { (fst $1, AbsC4.Decl (fst $1) (snd $1) (snd $2)) } ListIdent :: { (AbsC4.BNFC'Position, [AbsC4.Ident]) } ListIdent : Ident { (fst $1, (:[]) (snd $1)) } | Ident ',' ListIdent { (fst $1, (:) (snd $1) (snd $3)) } Stm :: { (AbsC4.BNFC'Position, AbsC4.Stm) } Stm : 'if' '(' Exp ')' Stm 'else' Stm { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), AbsC4.SIf (uncurry AbsC4.BNFC'Position (tokenLineCol $1)) (snd $3) (snd $5) (snd $7)) } | 'printInt' '(' Exp ')' ';' { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), AbsC4.SPrint (uncurry AbsC4.BNFC'Position (tokenLineCol $1)) (snd $3)) } | 'return' Exp ';' { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), AbsC4.SReturn (uncurry AbsC4.BNFC'Position (tokenLineCol $1)) (snd $2)) } | 'while' '(' Exp ')' Stm { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), AbsC4.SWhile (uncurry AbsC4.BNFC'Position (tokenLineCol $1)) (snd $3) (snd $5)) } | '{' ListDec ListStm '}' { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), AbsC4.SBlock (uncurry AbsC4.BNFC'Position (tokenLineCol $1)) (snd $2) (snd $3)) } | Ident '++' ';' { (fst $1, AbsC4.SIncr (fst $1) (snd $1)) } | Ident '--' ';' { (fst $1, AbsC4.SDecr (fst $1) (snd $1)) } | Ident '=' Exp ';' { (fst $1, AbsC4.SAss (fst $1) (snd $1) (snd $3)) } | Stm ';' { (fst $1, (snd $1)) } Exp :: { (AbsC4.BNFC'Position, AbsC4.Exp) } Exp : Exp0 { (fst $1, (snd $1)) } | Exp0 Op Exp0 { (fst $1, AbsC4.EOpA (fst $1) (snd $1) (snd $2) (snd $3)) } | Exp1 Op Exp1 { (fst $1, AbsC4.EOpE (fst $1) (snd $1) (snd $2) (snd $3)) } Exp0 :: { (AbsC4.BNFC'Position, AbsC4.Exp) } Exp0 : Exp1 { (fst $1, (snd $1)) } | Exp1 Op0 Exp1 { (fst $1, AbsC4.EOpB (fst $1) (snd $1) (snd $2) (snd $3)) } Exp1 :: { (AbsC4.BNFC'Position, AbsC4.Exp) } Exp1 : Exp1 Op1 Exp2 { (fst $1, AbsC4.EOpC (fst $1) (snd $1) (snd $2) (snd $3)) } | Exp2 { (fst $1, (snd $1)) } Exp2 :: { (AbsC4.BNFC'Position, AbsC4.Exp) } Exp2 : Exp2 Op2 Exp3 { (fst $1, AbsC4.EOpD (fst $1) (snd $1) (snd $2) (snd $3)) } | Exp3 { (fst $1, (snd $1)) } Exp3 :: { (AbsC4.BNFC'Position, AbsC4.Exp) } Exp3 : '(' Exp ')' { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), (snd $2)) } | Char { (fst $1, AbsC4.EChar (fst $1) (snd $1)) } | Double { (fst $1, AbsC4.EDouble (fst $1) (snd $1)) } | Integer { (fst $1, AbsC4.EInt (fst $1) (snd $1)) } | String { (fst $1, AbsC4.EString (fst $1) (snd $1)) } | Ident { (fst $1, AbsC4.EVar (fst $1) (snd $1)) } Op1 :: { (AbsC4.BNFC'Position, AbsC4.Op) } Op1 : '+' { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), AbsC4.OPlus (uncurry AbsC4.BNFC'Position (tokenLineCol $1))) } | '-' { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), AbsC4.OMinus (uncurry AbsC4.BNFC'Position (tokenLineCol $1))) } Op2 :: { (AbsC4.BNFC'Position, AbsC4.Op) } Op2 : '*' { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), AbsC4.OTimes (uncurry AbsC4.BNFC'Position (tokenLineCol $1))) } Op0 :: { (AbsC4.BNFC'Position, AbsC4.Op) } Op0 : '<' { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), AbsC4.OLt (uncurry AbsC4.BNFC'Position (tokenLineCol $1))) } | '==' { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), AbsC4.OEq (uncurry AbsC4.BNFC'Position (tokenLineCol $1))) } | '>' { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), AbsC4.OGt (uncurry AbsC4.BNFC'Position (tokenLineCol $1))) } Op :: { (AbsC4.BNFC'Position, AbsC4.Op) } Op : '&&' { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), AbsC4.OAnd (uncurry AbsC4.BNFC'Position (tokenLineCol $1))) } | '||' { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), AbsC4.OOr (uncurry AbsC4.BNFC'Position (tokenLineCol $1))) } Typ :: { (AbsC4.BNFC'Position, AbsC4.Typ) } Typ : 'double' { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), AbsC4.TDouble (uncurry AbsC4.BNFC'Position (tokenLineCol $1))) } | 'int' { (uncurry AbsC4.BNFC'Position (tokenLineCol $1), AbsC4.TInt (uncurry AbsC4.BNFC'Position (tokenLineCol $1))) } { type Err = Either String happyError :: [Token] -> Err a happyError ts = Left $ "syntax error at " ++ tokenPos ts ++ case ts of [] -> [] [Err _] -> " due to lexer error" t:_ -> " before `" ++ (prToken t) ++ "'" myLexer :: String -> [Token] myLexer = tokens -- Entrypoints pProgr :: [Token] -> Err AbsC4.Progr pProgr = fmap snd . pProgr_internal pListDec :: [Token] -> Err [AbsC4.Dec] pListDec = fmap snd . pListDec_internal pListStm :: [Token] -> Err [AbsC4.Stm] pListStm = fmap snd . pListStm_internal pDec :: [Token] -> Err AbsC4.Dec pDec = fmap snd . pDec_internal pListIdent :: [Token] -> Err [AbsC4.Ident] pListIdent = fmap snd . pListIdent_internal pStm :: [Token] -> Err AbsC4.Stm pStm = fmap snd . pStm_internal pExp :: [Token] -> Err AbsC4.Exp pExp = fmap snd . pExp_internal pExp0 :: [Token] -> Err AbsC4.Exp pExp0 = fmap snd . pExp0_internal pExp1 :: [Token] -> Err AbsC4.Exp pExp1 = fmap snd . pExp1_internal pExp2 :: [Token] -> Err AbsC4.Exp pExp2 = fmap snd . pExp2_internal pExp3 :: [Token] -> Err AbsC4.Exp pExp3 = fmap snd . pExp3_internal pOp1 :: [Token] -> Err AbsC4.Op pOp1 = fmap snd . pOp1_internal pOp2 :: [Token] -> Err AbsC4.Op pOp2 = fmap snd . pOp2_internal pOp0 :: [Token] -> Err AbsC4.Op pOp0 = fmap snd . pOp0_internal pOp :: [Token] -> Err AbsC4.Op pOp = fmap snd . pOp_internal pTyp :: [Token] -> Err AbsC4.Typ pTyp = fmap snd . pTyp_internal }