2016-03-20 7 views
1

Ich möchte einen Baum in einem "schönen" Format drucken. Ich verwende Text.PrettyPrint.GenericPretty und Text.PrettyPrint. Mein Baum kann mit Paaren von Ints, Strings und Identitäten verschachtelt werden.Haskell pretty printing

Mein Code so weit:

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

ich ein Beispiel gefunden:

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 

funktioniert gut, aber nicht mit meinem Baum. Wie kann ich mit meinem Baumformat arbeiten? Mein Baum sieht wie folgt aus:

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_declaration1 (Ident "exp") (Param_list1 (Parameter1 (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)))) Co mpare_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 (Argumente1 (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_t ermBint_factor (Bint_factorInt_expr (Int_exprInt_term (Int_termInt_factor (Int_factor6 (Ident "z") (Modifier_listArray_dimensions Array_dimensions2)))))))))) Deklarationen2)) (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))))))

ich die

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

der Baum ist ein Parse-Baum der folgenden Grammatik:

prog -> Block

blockieren -> Erklärungen program_body.

deklarationen -> deklaration SEMICOLON deklarationen |.

Deklaration -> var_declaration | fun_declaration.

var_declaration -> VARID array_dimensions COLON-Typ.

Typ -> INT | REAL | BOOL.

Array_Abmessungen -> SLPAR expr SRPAR array_dimensions |.

fun_declaration -> FUN ID param_list COLON Typ
CLPAR fun_block CRPAR.

fun_block -> Deklarationen fun_body.

param_list -> LPAR Parameter RPAR.

parameter -> basic_declaration more_parameters |.

more_parameters -> COMMA basic_declaration more_parameters |.

basic_declaration -> ID basic_array_dimensions COLON-Typ.

basic_array_dimensions -> SLPAR SRPAR basic_array_dimensions |.

program_body -> BEGIN prog_stmts ENDE.

fun_body -> BEGIN prog_stmts RETURN Ausdruck SEMICOLON END.

prog_stmts -> prog_stmt SEMICOLON prog_stmts |.

prog_stmt -> IF expr DANN prog_stmt ELSE prog_stmt | WHILE expr DO prog_stmt | READ-Kennung | Bezeichner ASSIGN expr | DRUCKEN Ausdruck | CLPAR Block CRPAR.

Kennung -> ID array_dimensions.

Ausdruck -> Ausdruck OR bint_term | bint_term

bint_term -> bint_term UND bint_factor | Bint_Faktor.

bint_factor -> NICHT 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_faktor | int_faktor.

mulop -> MUL | DIV.

int_factor -> ausdr LPAR OPG | SIZE LPAR ID basic_array_dimensions OPG | FLOAT LPAR ausdr OPG | FLOOR LPAR ausdr OPG | CEIL LPAR ausdr OPG | ID modifier_list | rval | RWert | BVAL | SUB int_factor.

modifier_list -> LPAR OPG Argumente | array_dimensions.

Argumente -> ausdr more_arguments |.

more_arguments -> COMMA ausdr more_arguments |.

+0

Was ist Ihr Baum-Format, und welche Fehler bekommen Sie? – sclv

+0

@sclv den Baum am unteren Rand der Frage, und ich fügte dort den Fehler. – 2D3D4D

+0

Sie haben mir die konkrete Bezeichnung für den Baum Da bist du ziemlich Druck. Ich mag die Deklaration des Baumdatentypen selbst. Das heißt das Äquivalent von 'Daten Baum a = Leaf | Knoten (Baum a) (Baum a) Ableitung (Generic) 'Baum für Ihre Art. – sclv

Antwort

0

Wie wäre es nur Ersetzen ‚(mit \ n \ t \ t Das wird es ziemlich drucken Wie das ich nähere

0

Dieser Fehler sagt Ihnen das Problem?.?

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

So versuchen:

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

und wenn diese Arten to fail haben Generic für ‚em definiert, müssen Sie so etwas wie

schreiben
deriving instance Generic Prog 

und

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