2016-03-20 11 views
1

ツリーを「きれいな」形式で印刷したいと思います。私はText.PrettyPrint.GenericPrettyText.PrettyPrintを使用しています。私のツリーは、int、strings、identityのペアでネストすることができます。Haskell pretty printing

これまでの私のコード:

{-# 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(ブロック1(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)))))):私のツリーはこのようになりますType_int(Fun_block1(宣言1)(宣言Var_宣言(Var_declaration1(Ident "z")))を宣言すると、 Array_dimensions2 Type_int))))))))))Fun_body1(Prog_stmts1(Prog_stmt1(ExprBint_term(Bint_termBint_factor(Bint_termBint_factor(int_exprInt_term(Int_termInt_factor(Int_factor6(Ident "b")) (Prog_stmt4(Identifier1(Ident "z")Array_dimensions2)(ExprBint_term(Bint_termBint_factor(Bint_factorInteger 1)))))(Prog_stmt4(Ident_1) (の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))))) (Int_termInt_factor(Int_termInt_factor(Int_termInt_factor(Int_termInt_factor(Int_termInt_factor(Int_termInt_factor));))AddUp2(Int_termInt_factor(Int_termInt_factor() Prog_stmts2)(ExprBint_term(Bint_t(int));)))))))))))))))) (配列番号2))))))))宣言2))(Prog_stmt1(Prog_stmt3(Identifier1(Ident "x")(Array_dimensions1(ExprBint_term(Int_exprInt_expr(Int_termInt_expr) (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))))))

私が取得次の文法の構文解析ツリー

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は - >宣言program_body - >

ブロックをブロックします。

宣言 - >宣言SEMICOLON宣言 |。

宣言 - > var_declaration | fun_declaration。

var_declaration - > VAR ID array_dimensions COLONタイプです。

タイプ - > INT | REAL | BOOL。

array_dimensions - > SLPAR expr SRPAR array_dimensions |。

fun_declaration - > FUN ID param_list COLONタイプ
CLPAR fun_block CRPAR。

fun_block - >宣言fun_body。

param_list - > LPARパラメータRPAR。

パラメータ - > basic_declaration more_parameters |。

more_parameters - > COMMA basic_declaration more_parameters |。

basic_declaration - > ID basic_array_dimensions COLONタイプ。

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識別子 |識別子ASSIGN expr | PRINT expr | CLPARはCRPARをブロックします。

識別子 - > ID配列_次元。

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 |サブ。

int_term - > int_term mulop int_factor | int_factor。

mulop - > MUL | DIV。

int_factor - > exprのLPAR OPG | SIZE LPARのIDのbasic_array_dimensions OPG | FLOAT LPAR exprの OPG | FLOORのLPAR exprの OPG | CEIL LPAR exprの OPG | IDのmodifier_list | RVAL | RVAL | BVAL | SUB int_factor。

modifier_list - > LPAR OPG引数| array_dimensions。

引数 - > exprのmore_arguments |。

more_arguments - > COMMA exprのmore_arguments |。

+0

何あなたのツリー形式であり、あなたはどのようなエラーが出るのですか? – sclv

+0

は、質問の下にあるツリーを@sclv、と私はそこにエラーを追加しました。 – 2D3D4D

+0

あなたは私にあなたがかなり印刷している木のための具体的な用語を与えてくれました。私は、ツリーのデータ型自体の宣言をします。すなわち 'データツリーA =リーフの同等|あなたの種類のための(ジェネリック) 'ツリーを導出ノード(ツリーA)(ツリーA)。 – sclv

答えて

0

方法だけ\ nに(」交換について\ T \ Tこれはかなり私が近づいんどのようにそれを印刷します。この

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)