"Güzel" bir biçimde bir ağaç yazdırmak istiyorum. Text.PrettyPrint.GenericPretty ve Text.PrettyPrint kullanıyorum. Ağacım, ints çiftleri, dizeler, kimlikler ile iç içe olabilir. Bugüne kadarHaskell güzel yazdırma
Kodum:
{-# 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))
Ben bir örnek Bulunan:
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
benim ağacı Çalışıyor, ama. Ağaç formatımla nasıl çalışabilirim? Benim ağaç şöyle görünür:
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 (Parametreler1 (Basic_declaration1 (Ident "b") Basic_array_dimensions2 Type_int) Daha fazla_parameters2)) Type_int (Fun_block1 (Declarations1 (DeclarationVar_declaration) (Var_declaration1 (Ident "z")) Array_dimensions2 Type_int)) Beyanlar2) (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 (Tanımlayıcı1 (Ident "z") Array_dimensions2) (ExprBint_term (Bint_termBint_factor (Bint_factorInt_expr (Int_exprInt_term (Int_termInt_factor (Int_factorInteger 1))))))) (Prog_stmt4 (Tanımlayıcı1) (Tanıtma "z") Array_dimensions2) (ExprBint_term (Bint_termBint_factor (Bint_factorInt_expr (Int_exprInt_term (Int_term1 (Int_termInt_factor (Int_factor6 (Tanıtma "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 (Kimlik "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 (Kimlik "z") (Modifier_listArray_dimensions Array_dimensions2))))))))))) Bildirimler2)) (Program_body1 (Prog_stmts1 (Prog_stmt3 (Tanımlayıcı1 (Kimlik "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 (Tanımlayıcı1 (Tanıtma "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 (Tanıtma "exp") (Modifier_list1 (Arguments1 (ExprBint_term (Bint_termBint_factor (Bint_factorInt_expr (Int_exprInt_term (Int_termInt_factor (Int_factor6 (Tanıtma "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))))))
Elde aşağıdaki dilbilgisi bir ayrıştırma ağacı
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))
ağaç şudur:
prog -> beyanlar program_body ->
blok blok.Açıklamalar -> bildirim SEMICOLON bildirimleri |
beyan -> var_declaration | fun_declaration.
var_declaration -> VAR ID array_dimensions COLON türü.
type -> INT | GERÇEK | BOOL.
array_dimensions -> SLPAR expr SRPAR array_dimensions |. > FUN İD param_list kolon tipi
CLPAR fun_block CRPAR -
fun_declaration.
fun_block -> bildirimler fun_body.
param_list -> LPAR parametreleri.
parametreleri -> basic_declaration more_parameters |.
more_parameters -> COMMA basic_declaration daha fazlası için_parameters |.
basic_declaration -> Kimlik basic_array_dimensions COLON türü.
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 tanımlayıcısı | tanımlayıcı ASSIGN expr | PRINT expr | CLPAR blok CRPAR.
tanımlayıcı -> ID dizisi_dimensions.
expr -> expr VEYA bint_term | bint_term.
bint_term -> bint_term VE 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 | ALT.
int_term -> int_term mulop int_factor | int_factor.
mulop -> MUL | DIV.
int_factor -> expr LPAR OPG | BOYUT LPAR Kimlik basic_array_dimensions OPG | FLOAT LPAR İfade OPG | ZEMİN LPAR İfade OPG | CEIL LPAR İfade OPG | Kimlik modifier_list | RVal | Rval | bval | SUB int_factor.
modifier_list -> LPAR OPG argümanlar | array_dimensions.
argümanlar -> expr more_arguments |.
more_arguments -> COMMA expr more_arguments |.
Ne ağaç biçimidir ve ne hata alıyorum? – sclv
sorunun altındaki ağacını @sclv ve orada hata eklendi. – 2D3D4D
Sen ağaç için Me beton terimini Verilen ettik oldukça baskı konum. Veri tipinin kendisinin beyanını istiyorum. Yani veriler Ağacı a = Leaf 'eşdeğer | Düğüm (Ağaç a) (Tree) (Generic) sizin türlü 'ağaç çıkartılması. Herhangi bir durumda – sclv