Reputation: 131
I want to print a tree in a "pretty" format. I am using Text.PrettyPrint.GenericPretty and Text.PrettyPrint. My tree can be nested with pairs of ints, strings, identities.
My code so far:
{-# LANGUAGE DeriveGeneric #-}
module Main where
import System.IO ( stdin, hGetContents )
import System.Environment ( getArgs, getProgName )
import System.Exit ( exitFailure, exitSuccess )
import LexNav
import ParNav
import SkelNav
import AbsNav
import System.Environment
import Language.LBNF.Compiletime
import qualified Language.LBNF.Runtime as L hiding (Ok, Bad)
import Text.PrettyPrint.GenericPretty
import Text.PrettyPrint
main = pp tree1
main = do
args <- getArgs
conts <- readFile (args !! 0)
let tokens = myLexer conts
pTree = pProg tokens
let tree = transProg pTree
putStrLn (show tokens)
putStrLn "----- \n\n ---- next -----"
putStrLn (show (pp pTree))
I Found an example:
import Text.PrettyPrint.GenericPretty
data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Generic)
instance (Out a) => Out (Tree a)
tree1 :: Tree Int
tree1 = Node (Node (Leaf 333333) (Leaf (-555555)))(Node (Node(Node(Leaf 888888)
(Leaf 57575757))(Leaf (-14141414)))(Leaf 7777777))
main = pp tree1
Works fine, but not with my tree. How can I get it working with my tree format? My tree looks like this:
Ok (ProgBlock (Block1 (Declarations1 (DeclarationVar_declaration (Var_declaration1 (Ident "x") (Array_dimensions1 (ExprBint_term (Bint_termBint_factor (Bint_factorInt_expr (Int_exprInt_term (Int_termInt_factor (Int_factorInteger 2)))))) Array_dimensions2) Type_int)) (Declarations1 (DeclarationFun_declaration (Fun_declaration1 (Ident "exp") (Param_list1 (Parameters1 (Basic_declaration1 (Ident "b") Basic_array_dimensions2 Type_int) More_parameters2)) Type_int (Fun_block1 (Declarations1 (DeclarationVar_declaration (Var_declaration1 (Ident "z") Array_dimensions2 Type_int)) Declarations2) (Fun_body1 (Prog_stmts1 (Prog_stmt1 (ExprBint_term (Bint_termBint_factor (Bint_factor2 (Int_exprInt_term (Int_termInt_factor (Int_factor6 (Ident "b") (Modifier_listArray_dimensions Array_dimensions2)))) Compare_op1 (Int_exprInt_term (Int_termInt_factor (Int_factorInteger 0)))))) (Prog_stmt4 (Identifier1 (Ident "z") Array_dimensions2) (ExprBint_term (Bint_termBint_factor (Bint_factorInt_expr (Int_exprInt_term (Int_termInt_factor (Int_factorInteger 1))))))) (Prog_stmt4 (Identifier1 (Ident "z") Array_dimensions2) (ExprBint_term (Bint_termBint_factor (Bint_factorInt_expr (Int_exprInt_term (Int_term1 (Int_termInt_factor (Int_factor6 (Ident "x") (Modifier_listArray_dimensions (Array_dimensions1 (ExprBint_term (Bint_termBint_factor (Bint_factorInt_expr (Int_exprInt_term (Int_termInt_factor (Int_factorInteger 1)))))) Array_dimensions2)))) Mulop1 (Int_factor6 (Ident "exp") (Modifier_list1 (Arguments1 (ExprBint_term (Bint_termBint_factor (Bint_factorInt_expr (Int_expr1 (Int_exprInt_term (Int_termInt_factor (Int_factor6 (Ident "b") (Modifier_listArray_dimensions Array_dimensions2)))) Addop2 (Int_termInt_factor (Int_factorInteger 1)))))) More_arguments2)))))))))) Prog_stmts2) (ExprBint_term (Bint_termBint_factor (Bint_factorInt_expr (Int_exprInt_term (Int_termInt_factor (Int_factor6 (Ident "z") (Modifier_listArray_dimensions Array_dimensions2))))))))))) Declarations2)) (Program_body1 (Prog_stmts1 (Prog_stmt3 (Identifier1 (Ident "x") (Array_dimensions1 (ExprBint_term (Bint_termBint_factor (Bint_factorInt_expr (Int_exprInt_term (Int_termInt_factor (Int_factorInteger 0)))))) Array_dimensions2))) (Prog_stmts1 (Prog_stmt3 (Identifier1 (Ident "x") (Array_dimensions1 (ExprBint_term (Bint_termBint_factor (Bint_factorInt_expr (Int_exprInt_term (Int_termInt_factor (Int_factorInteger 1)))))) Array_dimensions2))) (Prog_stmts1 (Prog_stmt5 (ExprBint_term (Bint_termBint_factor (Bint_factorInt_expr (Int_exprInt_term (Int_termInt_factor (Int_factor6 (Ident "exp") (Modifier_list1 (Arguments1 (ExprBint_term (Bint_termBint_factor (Bint_factorInt_expr (Int_exprInt_term (Int_termInt_factor (Int_factor6 (Ident "x") (Modifier_listArray_dimensions (Array_dimensions1 (ExprBint_term (Bint_termBint_factor (Bint_factorInt_expr (Int_exprInt_term (Int_termInt_factor (Int_factorInteger 0)))))) Array_dimensions2)))))))) More_arguments2))))))))) Prog_stmts2))))))
I get the following
Error:
> No instance for (Show (IO ())) arising from a use of ‘show’
In the first argument of ‘putStrLn’, namely ‘(show (pp pTree))’
In a stmt of a 'do' block: putStrLn (show (pp pTree))
In the expression:
do { args <- getArgs;
conts <- readFile (args !! 0);
let tokens = myLexer conts
pTree = pProg tokens;
putStrLn (show tokens);
.... }
> TestNav.hs:33:21:
No instance for (Out (ErrM.Err Prog)) arising from a use of ‘pp’
In the first argument of ‘show’, namely ‘(pp pTree)’
In the first argument of ‘putStrLn’, namely ‘(show (pp pTree))’
In a stmt of a 'do' block: putStrLn (show (pp pTree))
The tree is a parse tree of the following grammar:
prog -> block
block -> declarations program_body.
declarations -> declaration SEMICOLON declarations |.
declaration -> var_declaration | fun_declaration.
var_declaration -> VAR ID array_dimensions COLON type.
type -> INT | REAL | BOOL.
array_dimensions -> SLPAR expr SRPAR array_dimensions |.
fun_declaration -> FUN ID param_list COLON type
CLPAR fun_block CRPAR.
fun_block -> declarations fun_body.
param_list -> LPAR parameters RPAR.
parameters -> basic_declaration more_parameters |.
more_parameters -> COMMA basic_declaration more_parameters |.
basic_declaration -> ID basic_array_dimensions COLON type.
basic_array_dimensions -> SLPAR SRPAR basic_array_dimensions |.
program_body -> BEGIN prog_stmts END.
fun_body -> BEGIN prog_stmts RETURN expr SEMICOLON END.
prog_stmts -> prog_stmt SEMICOLON prog_stmts |.
prog_stmt -> IF expr THEN prog_stmt ELSE prog_stmt | WHILE expr DO prog_stmt | READ identifier | identifier ASSIGN expr | PRINT expr | CLPAR block CRPAR.
identifier -> ID array_dimensions.
expr -> expr OR bint_term | bint_term.
bint_term -> bint_term AND bint_factor | bint_factor.
bint_factor -> NOT bint_factor | int_expr compare_op int_expr | int_expr.
compare_op -> EQUAL | LT | GT | LE |GE.
int_expr -> int_expr addop int_term | int_term.
addop -> ADD | SUB.
int_term -> int_term mulop int_factor | int_factor.
mulop -> MUL | DIV.
int_factor -> LPAR expr RPAR | SIZE LPAR ID basic_array_dimensions RPAR | FLOAT LPAR expr RPAR | FLOOR LPAR expr RPAR | CEIL LPAR expr RPAR | ID modifier_list | IVAL | RVAL | BVAL | SUB int_factor.
modifier_list -> LPAR arguments RPAR | array_dimensions.
arguments -> expr more_arguments |.
more_arguments -> COMMA expr more_arguments |.
Upvotes: 1
Views: 820
Reputation: 38901
This error tells you the problem:
No instance for (Out (ErrM.Err Prog))
So try:
instance (Out a) => Out (ErrM.Err a)
instance Out Prog
And if those types fail to have Generic
defined for them, you need to write something like
deriving instance Generic Prog
and
deriving instance (Generic a) => Generic (ErrM.Err a)
Upvotes: 1
Reputation: 131
How about just replacing '(' with \n\t\t? That will pretty print it. How do I approach this?
Upvotes: 0