Commit 19b41ef8 authored by Administrator's avatar Administrator
Browse files

documented new facilities

parent 769a8afd
......@@ -334,6 +334,8 @@ No new or modified functionality, only new version number.
\input{Recovery} % Error recovery
\input{SyntaxTree} % Syntax tree
\input{Features} % Miscellaneous features
\input{Deploying} % Deploying
......
%HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
\section{Automatic syntax tree generation \label{synTree}}
%HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
We have also included automated generation of parse trees into mouse. It is
realized via a synthetically generated Semantics class, in which a subtree
is generated for every successfully matched right-hand side of a production.
You can arrange the automatic generation of parse trees via the parameter \tx{{-t}}:
\begin{Verbatim}[samepage=true,xleftmargin=15mm,baselinestretch=0.8]
java -jar Mouse-1.7.jar -G Java.1.8.peg -P JavaParser -S JavaSemantics -t
\end{Verbatim}
The parser calls \tx{{\$TREE\$}} prefixed semantic actions for each production:
\small
\begin{Verbatim}[frame=single,framesep=2mm,samepage=true,xleftmargin=15mm,xrightmargin=15mm,baselinestretch=0.8]
//=====================================================================
// Identifier = !Keyword Letter LetterOrDigit* Spacing {Identifier} ;
//=====================================================================
void $TREE$Identifier(){
LinkedList<XMLElement> elems = new LinkedList<>();
for (int i=0;i<rhsSize();i++) {
if (rhs(i).get()==null) continue;
if (rhs(i).isTerm()){
Phrase p= rhs(i);
elems.add(new XMLElement.Terminal(
new Location(...), // left location
"terminal",rhs(i).text(),
new Location(...) // right location
));
}
else
elems.add((XMLElement)rhs(i).get());
}
if (elems.size()==0)
lhs().put(new XMLElement.Terminal(
new Location(...),
"Identifier",lhs().text(),
new Location(...))
);
else
lhs().put(new XMLElement.NonTerminal("Identifier",0,elems.toArray()));
}
\end{Verbatim}
\normalsize
However, you can still override the automatically generated actions by custom semantic actions
via the usual curly bracketed labels. However, since you might want to regenerated the semantics
class due to the synthetic actions at some point when Your grammar changes, the custom user-defined
actions are now outsourced to the default methods of an interface, with the postfix \tx{{Usercode}}. There, you can still deposit your own action:
\small
\begin{Verbatim}[frame=single,framesep=2mm,samepage=true,xleftmargin=15mm,xrightmargin=15mm,baselinestretch=0.8]
public interface JavaSemanticsUsercode {
Phrase lhs();
Phrase rhs(int i);
//-------------------------------------------------------------------
// Identifier = !Keyword Letter LetterOrDigit* Spacing {Identifier}
//-------------------------------------------------------------------
default void Identifier()
{
XMLElement x = (XMLElement)lhs().get();
lhs().put(new XMLElement.Terminal(
new Location(),
"Identifier",
lhs().text().trim(),
new Location()
));
}
}
\end{Verbatim}
\normalsize
The result of the whole operation is a DOM parse tree. You can e.g. generate XML from that and
process it with the usual XSL transformations:
\small
\begin{Verbatim}[frame=single,framesep=2mm,samepage=true,xleftmargin=15mm,xrightmargin=15mm,baselinestretch=0.8]
boolean ok = parser.parse(src);
if (ok) {
JavaSemantics sem = parser.semantics();
XMLElement elem = (XMLElement)sem.rule.rhs(0).get();
// Dump DOM into XML-String via output stream
XMLOutputFactory outFactory = XMLOutputFactory.newInstance();
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLStreamWriter sw = outFactory.createXMLStreamWriter(out);
XMLElement.dump(null, sw, elem);
// Dump XML String to a file
PrintWriter pw = new PrintWriter("output.xml");
String s = new String(out.toByteArray());
pw.print(s);
pw.flush();
pw.close();
}
\end{Verbatim}
\normalsize
Alternatively, there are several \tx{{java\_cup.runtime}} classes like \tx{{SyntaxTreeDFS}}, \tx{{SyntaxTreeTransform}} and
\tx{{SyntaxTreeXPath}} which help you working with the parse tree by offering tools like XPath-style queries ,Visitors for a DFS through
the parser tree and default transformations like collapsing chains of unary parse tree branches to a single branch.
You can find all these topics covered in example 11.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment