Commit 4c3ce704 authored by petter's avatar petter

major progress at the XML-parse tree

git-svn-id: https://www2.in.tum.de/repos/cup/develop@39 f8377d4f-c7f8-0310-b030-a31f13008fac
parent f9cc1289
......@@ -1120,9 +1120,14 @@ public class emit {
nested+=",new XMLElement.Terminal("+label+"xleft,\""+label+"\","+label+","+label+"xright)";
}
// determine the variant:
int variant=0;
for (int i=0;i<proditeration;i++)
if (production.find(i).lhs().equals(prod.lhs())) variant++;
out.println(" RESULT = new XMLElement.NonTerminal(\""+
prod.lhs().the_symbol().name()+"\","+
prod.index()+nested+");");
variant+nested+");");
/* Create the code that assigns the left and right values of
......
......@@ -115,6 +115,9 @@ public class ComplexSymbolFactory implements SymbolFactory{
if (xleft==null || xright==null) return "Symbol: "+name;
return "Symbol: "+name+" ("+xleft+" - "+xright+")";
}
public String getName(){
return name;
}
public ComplexSymbol(String name, int id, int state) {
super(id,state);
this.name=name;
......
package java_cup.runtime;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class ScannerBuffer implements Scanner {
private Scanner inner;
private List<Symbol> buffer = new LinkedList<Symbol>();
/**
* Wraps around a custom scanner and stores all so far produced tokens in a buffer
* @param inner the scanner to buffer
*/
public ScannerBuffer(Scanner inner){
this.inner=inner;
}
/**
* Read-Only access to the buffered Symbols
* @return an unmodifiable Version of the buffer
*/
public List<Symbol> getBuffered() {
return Collections.unmodifiableList(buffer);
}
@Override
public Symbol next_token() throws Exception {
Symbol buffered = inner.next_token();
buffer.add(buffered);
return buffered;
}
}
......@@ -7,13 +7,61 @@ import java.util.LinkedList;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import java_cup.runtime.ComplexSymbolFactory.ComplexSymbol;
import java_cup.runtime.ComplexSymbolFactory.Location;
public abstract class XMLElement {
public static void dump(XMLStreamWriter writer, XMLElement elem) throws XMLStreamException {
public static void dump(XMLStreamWriter writer, XMLElement elem, String ... blacklist) throws XMLStreamException {
dump(null,writer,elem,blacklist);
}
public static void dump(ScannerBuffer buffer, XMLStreamWriter writer, XMLElement elem, String ... blacklist) throws XMLStreamException {
writer.writeStartDocument();
writer.writeProcessingInstruction("xml-stylesheet","href=\"tree.xsl\" type=\"text/xsl\"");
writer.writeStartElement("document");
if (blacklist.length>0) {
writer.writeStartElement("blacklist");
for (String s: blacklist){
writer.writeStartElement("symbol");
writer.writeCharacters(s);
writer.writeEndElement();
}
writer.writeEndElement();
}
writer.writeStartElement("parsetree");
elem.dump(writer);
writer.writeEndElement();
if (buffer!=null){
writer.writeStartElement("tokensequence");
for (Symbol s:buffer.getBuffered()){
writer.writeStartElement("token");
if (s instanceof ComplexSymbol){
ComplexSymbol cs = (ComplexSymbol)s;
if (cs.value!=null){
writer.writeStartElement("token");
writer.writeAttribute("name",cs.getName());
writer.writeAttribute("left",cs.getLeft()+"");
writer.writeAttribute("right",cs.getRight()+"");
writer.writeCharacters(cs.value+"");
writer.writeEndElement();
} else
{
writer.writeStartElement("keyword");
writer.writeAttribute("left",cs.getLeft()+"");
writer.writeAttribute("right",cs.getRight()+"");
writer.writeCharacters(cs.getName()+"");
writer.writeEndElement();
}
}
else
if (s instanceof Symbol) writer.writeCharacters(s.toString());
writer.writeEndElement();
}
writer.writeEndElement();
}
writer.writeEndElement();
writer.writeEndDocument();
writer.flush();
writer.close();
......
/* Minijava Grammar */
import java_cup.runtime.ComplexSymbolFactory;
import java_cup.runtime.ScannerBuffer;
import java_cup.runtime.XMLElement;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
......@@ -10,11 +11,12 @@ parser code {:
}
public static void main(String[] args) throws Exception {
ComplexSymbolFactory csf = new ComplexSymbolFactory();
Parser p = new Parser(new Lexer(new BufferedReader(new FileReader(args[0])),csf),csf);
ScannerBuffer lexer = new ScannerBuffer(new Lexer(new BufferedReader(new FileReader(args[0])),csf));
Parser p = new Parser(lexer,csf);
XMLElement e = (XMLElement)p.parse().value;
XMLOutputFactory outFactory = XMLOutputFactory.newInstance();
XMLStreamWriter sw = outFactory.createXMLStreamWriter(new FileOutputStream(args[1]));
XMLElement.dump(sw,e);
XMLElement.dump(lexer,sw,e,"expr","stmt");
}
:};
......
body { font-family: sans-serif; font-size: 80%; background-color: #EAEAD9; color: black }
.connector { font-family: monospace; }
.name { color: navy; background-color: white; text-decoration: underline; font-weight: bold;
padding-top: 0px; padding-bottom: 1px; padding-left: 3px; padding-right: 3px }
.altname { color: navy; text-decoration: underline }
.uri { color: #444; font-style: italic }
.value { color: #040; background-color: #CCC; font-weight: bold }
.escape { color: #620; font-family: monospace }
.root { color: yellow; background-color: black }
.element { color: yellow; background-color: navy }
.namespace { color: yellow; background-color: #333 }
.attribute { color: yellow; background-color: #040 }
.text { color: yellow; background-color: #400 }
.pi { color: yellow; background-color: #044 }
.comment { color: yellow; background-color: #303 }
.root,.element,.attribute,.namespace,.text,.comment,.pi
{ font-weight: bold;
padding-top: 0px; padding-bottom: 1px; padding-left: 3px; padding-right: 3px }
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="no"/>
<xsl:variable name="apos">'</xsl:variable>
<xsl:template match="/" mode='rendered'>
<html>
<head>
<title>Parse-Tree</title>
<link type="text/css" rel="stylesheet" href="tree-view.css"/>
</head>
<body>
<h3>Parse-Tree</h3>
<xsl:apply-templates select="." mode="render"/>
</body>
</html>
</xsl:template>
<xsl:template match="/" mode="render">
<xsl:apply-templates mode="render"/>
</xsl:template>
<xsl:template match="*" mode="render">
<xsl:call-template name="ascii-art-hierarchy"/>
<br/>
<xsl:call-template name="ascii-art-hierarchy"/>
<span class='connector'>___</span>
<span class="element"><xsl:value-of select="local-name()"/></span>
<xsl:text>&#160;</xsl:text>
<br/>
<xsl:apply-templates select="@*" mode="render"/>
<xsl:apply-templates mode="render"/>
</xsl:template>
<xsl:template match="@*" mode="render">
<xsl:call-template name="ascii-art-hierarchy"/>
<span class='connector'>&#160;&#160;</span>
<span class='connector'>\___</span>
<xsl:text>&#160;</xsl:text>
<span class="name">
<xsl:value-of select="local-name()"/>
</span>
<xsl:text> = </xsl:text>
<span class="value">
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="translate(.,' ','&#160;')"/>
</xsl:call-template>
</span>
<br/>
</xsl:template>
<xsl:template match="text()" mode="render">
<xsl:call-template name="ascii-art-hierarchy"/>
<br/>
<xsl:call-template name="ascii-art-hierarchy"/>
<span class='connector'>___</span>
<xsl:text>&#160;</xsl:text>
<span class="value">
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="translate(.,' ','&#160;')"/>
</xsl:call-template>
</span>
<br/>
</xsl:template>
<xsl:template match="comment()" mode="render" />
<xsl:template match="processing-instruction()" mode="render" />
<xsl:template name="ascii-art-hierarchy">
<xsl:for-each select="ancestor::*">
<xsl:choose>
<xsl:when test="following-sibling::node()">
<span class='connector'>&#160;&#160;</span>|<span class='connector'>&#160;&#160;</span>
<xsl:text>&#160;</xsl:text>
</xsl:when>
<xsl:otherwise>
<span class='connector'>&#160;&#160;&#160;&#160;</span>
<span class='connector'>&#160;&#160;</span>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:choose>
<xsl:when test="parent::node() and ../child::node()">
<span class='connector'>&#160;&#160;</span>
<xsl:text>|</xsl:text>
</xsl:when>
<xsl:otherwise>
<span class='connector'>&#160;&#160;&#160;</span>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- recursive template to escape linefeeds, tabs -->
<xsl:template name="escape-ws">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '&#xA;')">
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="substring-before($text, '&#xA;')"/>
</xsl:call-template>
<span class="escape">\n</span>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="substring-after($text, '&#xA;')"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($text, '&#x9;')">
<xsl:value-of select="substring-before($text, '&#x9;')"/>
<span class="escape">\t</span>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="substring-after($text, '&#x9;')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise><xsl:value-of select="$text"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
>
<xsl:output method="xml" indent="yes"/>
<xsl:include href="tree-view.xsl"/>
<xsl:template match="blacklist" mode="flatten" />
<xsl:template match="document" mode="flatten">
<xsl:apply-templates mode="flatten" select="parsetree"/>
</xsl:template>
<xsl:template match="nonterminal" mode="flatten">
<xsl:variable name="temp" select="@id" />
<xsl:choose>
<!-- collapses degenerated trees like lists, conserving the blacklist subtrees-->
<xsl:when test="../@id = @id and count(/document/blacklist[1]/symbol[text() = $temp])=0">
<xsl:apply-templates mode="flatten"/>
</xsl:when>
<!-- collapses unary productions -->
<xsl:when test="count(*)=1 and count(/document/blacklist[1]/symbol[text() = $temp])=0">
<xsl:apply-templates mode="flatten"/>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{@id}" >
<xsl:attribute name="variant"><xsl:value-of select="@variant" /></xsl:attribute>
<xsl:apply-templates mode="flatten"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="terminal" mode="flatten">
<xsl:element name="{@id}">
<xsl:apply-templates mode="flatten"/>
</xsl:element>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="flatten">
<xsl:apply-templates mode="flatten"/>
</xsl:variable>
<xsl:variable name="rendered">
<xsl:apply-templates mode="rendered" select="exsl:node-set($flatten)"/>
</xsl:variable>
<xsl:copy-of select="$rendered" />
</xsl:template>
</xsl:stylesheet>
\ No newline at end of file
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