Haskell漂亮打印

时间:2016-03-20 21:47:09

标签: haskell pretty-print

我想在一个漂亮的"中打印一棵树。格式。我正在使用Text.PrettyPrint.GenericPrettyText.PrettyPrint。我的树可以嵌入成对的整数,字符串,身份。

到目前为止我的代码:

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

我找到了一个例子:

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

工作得很好,但不适合我的树。如何使用我的树格式?我的树看起来像这样:

  

Ok(ProgBlock(Block1)(声明1(DeclarationVar_declaration(Var_declaration1(Ident&#34; x&#34;)(Array_dimensions1(ExprBint_term(Bint_termBint_factor(Bint_factorInt_expr(Int_exprInt_term(Int_termInt_factor(Int_factorInteger 2))))))Array_dimensions2)Type_int) )(声明1(DeclarationFun_declaration(Fun_declaration1(Ident&#34; exp&#34;)(Param_list1(Parameters1(Basic_declaration1(Ident&#34; b&#34;)Basic_array_dimensions2 Type_int)More_parameters2))Type_int(Fun_block1(声明1)(DeclarationVar_declaration(Var_declaration1) (Ident&#34; z&#34;)Array_dimensions2 Type_int))声明2)(Fun_body1(Prog_stmts1(Prog_stmt1(ExprBint_term(Bint_termBint_factor)(Bint_factor2(Int_exprInt_term(Int_termInt_factor(Int_factor6(Ident&#34; b&#34;)(Modifier_listArray_dimensions Array_dimensions2) )))Compare_op1(Int_exprInt_term(Int_termInt_factor(Int_factorInteger 0))))))(Prog_stmt4(Identifier1(Ident&#34; z&#34;)Array_dimensions2)(ExprBint_term(Bint_t) ermBint_factor(Bint_factorInt_expr(Int_exprInt_term(Int_termInt_factor(Int_factorInteger 1)))))))(Prog_stmt4(Identier1(Ident&#34; z&#34;)Array_dimensions2)(ExprBint_term(Bint_termBint_factor(Bint_factorInt_expr(Int_exprInt_term(Int_term1)(Int_termInt_factor(Int_factor6(Ident) &#34; x&#34;)(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&#34; exp&#34; )(Modifier_list1(Arguments1(ExprBint_term(Bint_termBint_factor(Bint_factorInt_expr(Int_expr1(Int_exprInt_term(Int_termInt_factor(Int_factor6(Ident&#34; b&#34;)(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&#34; z&#34;)(Modifier_list) Array_dimensions Array_dimensions2))))))))))声明2))(Program_body1(Prog_stmts1(Prog_stmt3(Identifier1(Ident&#34; x&#34;)(Array_dimensions1(ExprBint_term(Bint_termBint_factor)(Bint_factorInt_expr(Int_exprInt_term(Int_exInt_factor(Int_factorInteger 0) ))))))Array_dimensions2)))(Prog_stmts1(Prog_stmt3(Identifier1(Ident&#34; x&#34;)(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&#34; exp&#34;)(Modifier_list1(Arguments1(ExprBint_term)(Bint_termBint_factor(Bint_factorInt_expr)(Int_exprInt_term(Int_termInt_factor)(Int_factor6(Ident&#34) ; x&#34;)(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))))))

我得到以下

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))

树是以下语法的解析树:

prog - &gt;块

块 - &gt;声明program_body。

声明 - &gt;声明SEMICOLON声明              |

声明 - &gt; var_declaration              | fun_declaration。

var_declaration - &gt; VAR ID array_dimensions COLON类型。

type - &gt; INT       |真实       | BOOL。

array_dimensions - &gt; SLPAR expr SRPAR array_dimensions              |

fun_declaration - &gt; FUN ID param_list COLON类型
                                            CLPAR fun_block CRPAR。

fun_block - &gt;声明fun_body。

param_list - &gt; LPAR参数RPAR。

参数 - &gt; basic_declaration more_parameters             |

more_parameters - &gt; COMMA basic_declaration more_parameters             |

basic_declaration - &gt; ID basic_array_dimensions COLON类型。

basic_array_dimensions - &gt; SLPAR SRPAR basic_array_dimensions              |

program_body - &gt; BEGIN prog_stmts END。

fun_body - &gt; BEGIN prog_stmts RETURN expr SEMICOLON END。

prog_stmts - &gt; prog_stmt SEMICOLON prog_stmts             |。

prog_stmt - &gt; IF expr THEN prog_stmt ELSE prog_stmt            |什么是expr DO prog_stmt            | READ标识符            |标识符ASSIGN expr            | PRINT expr            | CLPAR阻止CRPAR。

标识符 - &gt; ID array_dimensions。

expr - &gt; expr OR bint_term        | bint_term。

bint_term - &gt; bint_term和bint_factor            | bint_factor。

bint_factor - &gt;不是bint_factor              | int_expr compare_op int_expr              | int_expr。

compare_op - &gt; EQUAL | LT | GT | LE | GE。

int_expr - &gt; int_expr addop int_term           | int_term。

addop - &gt; ADD | SUB。

int_term - &gt; int_term mulop int_factor           | int_factor。

mulop - &gt; MUL | DIV。

int_factor - &gt; 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 - &gt; LPAR参数RPAR            | array_dimensions。

参数 - &gt; expr more_arguments            |

more_arguments - &gt; COMMA expr more_arguments            |

2 个答案:

答案 0 :(得分:0)

如何只是替换&#39;(&#39;用\ n \ t \ t?这将打印出来。我该如何解决这个问题?

答案 1 :(得分:0)

此错误告诉您问题:

No instance for (Out (ErrM.Err Prog)) 

所以试试:

instance (Out a) => Out (ErrM.Err a)
instance Out Prog

如果这些类型未能为它们定义Generic,则需要编写类似

的内容
deriving instance Generic Prog

deriving instance (Generic a) => Generic (ErrM.Err a)