Commit a7af5f4f authored by Dr. Michael Petter's avatar Dr. Michael Petter

completed the example project

parent c6eed6ee
#!/bin/bash
# clean up directory
rm *.class
rm -r JavaParser.java JavaSemantics.java
# generate Semantics file and Parser
java -jar ../../build/Mouse-1.7.jar -G Java.1.8.peg -P JavaParser -S JavaSemantics -t
# compile everything
javac -cp ../../lib/java-cup-11b-runtime.jar:../../build/Mouse-1.7.jar:. Compiler.java
# call the final parser and generate a Compiler.ast.html
java -cp ../../lib/java-cup-11b-runtime.jar:../../build/Mouse-1.7.jar:. Compiler Compiler.java Compiler
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"
encoding="UTF-8"
indent="no"/>
<xsl:variable name="apos">'</xsl:variable>
<xsl:template match="/">
<html>
<head>
<meta charset="UTF-8" />
<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" version="1.0" encoding="UTF-8" />
<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>
<xsl:when test="count(*)=3 and contains($temp,'expr') and *[contains(@id,'expr')]">
<xsl:apply-templates mode="flatten"/>
</xsl:when>
<!-- 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(*)=3 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:copy-of select="$flatten" />
</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