/** * JavaCC template file created by SF JavaCC plugin 1.5.17+ wizard for JavaCC 1.5.0+ */ options { JDK_VERSION = "1.5"; static = false; } PARSER_BEGIN(ParseRythmicTree) package javacc.client; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.ArrayList; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; public class ParseRythmicTree { public static void make(String s) throws ParseException { ParseRythmicTree parser = new ParseRythmicTree(s); try { System.out.println(parser.start()); } catch (Exception e) { System.out.println("NOK."); System.out.println(e.getMessage()); } catch (Error e) { System.out.println("Oops."); System.out.println(e.getMessage()); } } } PARSER_END(ParseRythmicTree) SKIP : {" "} TOKEN : { } TOKEN : { } TOKEN : { } TOKEN : { } TOKEN : { < END : "\n" > } RythmicTree start(): { RythmicTree rt; } {rt = exp()< EOF > { return rt; } } RythmicTree exp(): { Token t; RythmicTree rt; Fraction ratio = new Fraction(0,1); ArrayList children = null; } { ( ( < LPAR > { children = new ArrayList(); } ratio = makeRatio() ( rt = exp() { children.add(rt); } )+ < RPAR > ) | ratio = makeRatio()) { return new RythmicTree(ratio,children); } } Fraction makeRatio() : { Fraction f; Token t; } { ( t = < NUMBER > | t = < FRAC >) { if(t.kind == NUMBER) f = new Fraction(Integer.parseInt(t.image), 1); else f = new Fraction(Integer.parseInt(t.image.split("/")[0]), Integer.parseInt(t.image.split("/")[1])); return f; } }