Skip to content

Instantly share code, notes, and snippets.

@grondilu
Created December 7, 2012 06:24
Show Gist options
  • Save grondilu/4231207 to your computer and use it in GitHub Desktop.
Save grondilu/4231207 to your computer and use it in GitHub Desktop.
parsing a newick tree
use v6;
my $sample = "((((((((((((((Odobenus_clarus)Opheodrys_nippon)Seokia_dignus,((Megaloperdix_coloratovillosum,(Ceratophrys_perrotetii)Cuora_boa)Meles_subminiatus,Megaloperdix_terrestris,(Boiga_lobatus)Caiman_brevirostris)Scolopendra_kopsteini)Thecla_temminskii,Nyctaalus_schrencki)Tupinambus_krueperi)Uncia_ornata,Trapelus_monorhis,(((Melanocoryhpa_australis,(Chelydra_mystacinus)Crotaphytus_mitratus)Streptopelia_mutabilis,Pterocles_caucasicus,(((Aegialifes_colombianus)Castor_ferox)Coenobita_glaucescens)Hyperoodon_hardwickii,Equus_relictus)Streptopelia_similis,((Eurynorhynchus_serricollis)Salvelinus_clypeatus,Pyrgilauda_glaucescens,(Aplopeltura_venulosa)Machetes_trianguligerus,Coleonyx_nupta)Spalerosophis_indicus)Thymallus_stellio,((Eucratoscelus_iguana)Machetes_penelope,Ahaetulla_melanoleucus)Sitta_quadriocellata,(Ameiva_crassidens)Oenanthe_longipennis)Ursus_himalayensis,((Acanthosaura_rusticolus)Thymallus_olivacea,Phrynohyas_dominicus)Tiliqua_cavimanus,(Limosa_glacialis,Eryx_guttata,Bubulcus_altaica)Myotis_cinereus)sibiricus_sudanensis,(((Aphonopelma_mnemosyne)Leiopython_karelini,Argynnis_dendrophila)Monodon_quadriocellata)Sericinus_oenanthe,(Leptopelis_variegatus)Pleurodeles_crassidens,(((Kaloula_rosea,(Chondropython_placidus)Eirenis_smithi,Acanthis_drapiezii)Larus_nigriceps,(Acanthoscurria_squamatus)Hottentotta_ferox)Myotis_ridibundus,((Buteo_hilarii)Homopholis_classicus)Lanius_crocodilus)Otis_pendulinus,(Dafila_mycterizans,Aquila_melanoleucus)Eurynorhynchus_fuellebornii,Epicrates_monilis)Thamnophis_brongersmai)Sphenops_latiscutatus,Rhabdophis_pachypus,(Larus_colubrinus,(Coleonyx_carinatus)Ephibolus_fimbriatus)Phelsuma_hendersoni)Saga_canorus,Macrorhamphus_homeana)Pusa_viscivorus)Parnassius_guttata)Pachytriton_smithi)Ovis_salvator;";
grammar Newick {
rule TOP { <node> ';' }
token node {
<name>
| '(' ~ ')' <child=node>+ % ',' <name>
}
token name { <.ident>* }
}
if Newick.parse: $sample { say $/ }
# vim: ft=perl6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment