\end{tabular}\\
\begin{tabular}{lll}
-{\nonterminal{Exp}} & {\arrow} &{\nonterminal{Exp1}} {\nonterminal{BOp}} {\nonterminal{Exp1}} \\
- & {\delimit} &{\nonterminal{Ident}} {\terminal{{$=$}}} {\nonterminal{Exp}} \\
+{\nonterminal{Exp}} & {\arrow} &{\nonterminal{Ident}} {\terminal{{$=$}}} {\nonterminal{Exp}} \\
+ & {\delimit} &{\nonterminal{Exp1}} {\nonterminal{BOp}} {\nonterminal{Exp1}} \\
& {\delimit} &{\nonterminal{Exp1}} \\
\end{tabular}\\
& {\delimit} &{\nonterminal{Bool}} \\
& {\delimit} &{\terminal{readInt}} \\
& {\delimit} &{\terminal{readBool}} \\
- & {\delimit} &{\nonterminal{Exp4}} \\
-\end{tabular}\\
-
-\begin{tabular}{lll}
-{\nonterminal{Exp4}} & {\arrow} &{\terminal{(}} {\nonterminal{Exp}} {\terminal{)}} \\
+ & {\delimit} &{\terminal{(}} {\nonterminal{Exp}} {\terminal{)}} \\
\end{tabular}\\
\begin{tabular}{lll}
happy -gca -idebug Parsyntax.y
alex -g Lexsyntax.x
latex Docsyntax.tex; dvips Docsyntax.dvi -o Docsyntax.ps
- ghc -fglasgow-exts --make Testsyntax.hs -o Testsyntax
+ ghc --make Testsyntax.hs -o Testsyntax
+ ghc -fglasgow-exts --make Typechecker.hs -o Typechecker
clean:
-rm -f *.log *.aux *.hi *.o *.dvi
-rm -f Docsyntax.ps
Exp :: { Exp }
-Exp : Exp1 BOp Exp1 { BExp $1 $2 $3 }
- | Ident '=' Exp { EAss $1 $3 }
+Exp : Ident '=' Exp { EAss $1 $3 }
+ | Exp1 BOp Exp1 { BExp $1 $2 $3 }
| Exp1 { $1 }
| Bool { EBool $1 }
| 'readInt' { EReadI }
| 'readBool' { EReadB }
- | Exp4 { $1 }
-
-
-Exp4 :: { Exp }
-Exp4 : '(' Exp ')' { $2 }
+ | '(' Exp ')' { $2 }
ListStm :: { [Stm] }
instance Print Exp where
prt i e = case e of
+ EAss id exp -> prPrec i 0 (concatD [prt 0 id , doc (showString "=") , prt 0 exp])
BExp exp0 bop exp -> prPrec i 0 (concatD [prt 1 exp0 , prt 0 bop , prt 1 exp])
EVar id -> prPrec i 3 (concatD [prt 0 id])
- EAss id exp -> prPrec i 0 (concatD [prt 0 id , doc (showString "=") , prt 0 exp])
EInt n -> prPrec i 3 (concatD [prt 0 n])
ENeg exp -> prPrec i 3 (concatD [doc (showString "-") , prt 3 exp])
EBool bool -> prPrec i 3 (concatD [prt 0 bool])
transExp :: Exp -> Result
transExp x = case x of
+ EAss id exp -> failure x
BExp exp0 bop exp -> failure x
EVar id -> failure x
- EAss id exp -> failure x
EInt n -> failure x
ENeg exp -> failure x
EBool bool -> failure x
import Printsyntax
import Abssyntax
-import Typecheck
-import Control.Monad.State
-import Data.Map as Map hiding (showTree)
-
putStrV :: Verbosity -> String -> IO ()
putStrV v s = if v > 1 then putStrLn s else return ()
-runFile :: Verbosity -> ParseFun Stms -> FilePath -> IO ()
+runFile :: (Print a, Show a) => Verbosity -> ParseFun a -> FilePath -> IO ()
runFile v p f = putStrLn f >> readFile f >>= run v p
-run :: Verbosity -> ParseFun Stms -> String -> IO ()
+run :: (Print a, Show a) => Verbosity -> ParseFun a -> String -> IO ()
run v p s = let ts = myLLexer s in case p ts of
- Bad s -> do
- putStrLn "\nParse Failed...\n"
- putStrV v "Tokens:"
- putStrV v $ show ts
- putStrLn s
- Ok (Program s) -> do
- putStrLn "\nParse Successful!"
- showTree v (Program s)
- runStateT (mapM typeCheckStm s) empty
- return ()
+ Bad s -> do putStrLn "\nParse Failed...\n"
+ putStrV v "Tokens:"
+ putStrV v $ show ts
+ putStrLn s
+ Ok tree -> do putStrLn "\nParse Successful!"
+ showTree v tree
+
+
showTree :: (Show a, Print a) => Int -> a -> IO ()
showTree v tree
--- /dev/null
+-- automatically generated by BNF Converter
+module Main where
+
+
+import IO ( stdin, hGetContents )
+import System ( getArgs, getProgName )
+
+import Lexsyntax
+import Parsyntax
+import Skelsyntax
+import Printsyntax
+import Abssyntax
+
+import Typecheck
+import Control.Monad.State
+import Data.Map as Map hiding (showTree)
+
+
+
+
+import ErrM
+
+type ParseFun a = [Token] -> Err a
+
+myLLexer = myLexer
+
+type Verbosity = Int
+
+putStrV :: Verbosity -> String -> IO ()
+putStrV v s = if v > 1 then putStrLn s else return ()
+
+runFile :: Verbosity -> ParseFun Stms -> FilePath -> IO ()
+runFile v p f = putStrLn f >> readFile f >>= run v p
+
+run :: Verbosity -> ParseFun Stms -> String -> IO ()
+run v p s = let ts = myLLexer s in case p ts of
+ Bad s -> do
+ putStrLn "\nParse Failed...\n"
+ putStrV v "Tokens:"
+ putStrV v $ show ts
+ putStrLn s
+ Ok (Program s) -> do
+ putStrLn "\nParse Successful!"
+ showTree v (Program s)
+ runStateT (mapM typeCheckStm s) empty
+ return ()
+
+showTree :: (Show a, Print a) => Int -> a -> IO ()
+showTree v tree
+ = do
+ putStrV v $ "\n[Abstract Syntax]\n\n" ++ show tree
+ putStrV v $ "\n[Linearized tree]\n\n" ++ printTree tree
+
+main :: IO ()
+main = do args <- getArgs
+ case args of
+ [] -> hGetContents stdin >>= run 2 pStms
+ "-s":fs -> mapM_ (runFile 0 pStms) fs
+ fs -> mapM_ (runFile 2 pStms) fs
+
+
+
+
+