10.10.2013 Views

XML — Architecture, Tools, Techniques - Department of Computer ...

XML — Architecture, Tools, Techniques - Department of Computer ...

XML — Architecture, Tools, Techniques - Department of Computer ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

TraceN is a top-level nested class in Actor. The TraceN objects show their nonterminal name<br />

qualified with an index.<br />

Now we get new StringBuffer objects all the time and they can be returned as result <strong>of</strong><br />

reduce(). The trace shows that this trivially produces a syntax tree:<br />

$ java -classpath .:oops.jar oops.Compile -f Actor expr.ser Scanner<br />

GoalMakerFactory is Actor<br />

3*(4+5)<br />

...<br />

lines1 shift sum2<br />

sum [product [term 3] * [term ( [sum [product [term 4]] + [product [term 5]]] )]]<br />

Arithmetic Expressions in <strong>XML</strong><br />

The Element Construction Set contains classes to build a tree and render it in <strong>XML</strong>. Xml is an<br />

Actor for expr.ebnf. It uses these classes to render arithmetic expressions in <strong>XML</strong>:<br />

/** convert arithmetic expressions to <strong>XML</strong>.<br />

*/<br />

public class Xml extends Actor {<br />

%%<br />

lines: ( sum? "\n" )*;<br />

%<br />

return new GoalAdapter() {<br />

public void shift (Goal sender, Object value) {<br />

if (result == null) result = new Root("expr");<br />

((Root)result).add(value);<br />

}<br />

public Object reduce () {<br />

if (result != null)<br />

System.out.println(result);<br />

return null; // no serialized tree to output<br />

}<br />

};<br />

%<br />

sum: product ( "+" product | "-" product )*;<br />

%<br />

return new GoalAdapter() {<br />

public void shift (Goal sender, Object value) {<br />

if (result == null) result = value;<br />

else ((Element)result).add(value);<br />

}<br />

public void shift (Lit sender, Object value) {<br />

if (value.equals("+"))<br />

result = new Element("add", new Object[]{ result });<br />

else result = new Element("sub", new Object[]{ result });<br />

}<br />

};<br />

%<br />

51<br />

oops/Xml.actor

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!