[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Sablecc-user] Grammar Visualize



I think i missed this list from the reply.
Why do we have 2 lists anyway? Just close one of them for posts.
--- Begin Message ---
I don't know exactly what you mean but here's my take on the
issue. It builds on the alternative output I wrote for sablecc,
and more specifically the generated XML.

Here's a XSL stylesheet that generates input for the
dot tool.

My sablecc addon:
http://www.mare.ee/indrek/sablecc/

The dot tool (came with my Linux distro):
http://www.research.att.com/sw/tools/graphviz/

As example I've attached the XSL stylesheet, an example
grammar, outputed dot file and generated graph.

Regards,
Indrek


On Wednesday 05 November 2003 05:57, Welson Sun wrote:
> Hi all,
>
> How can visualize the grammar specified in SableCC?
>
> I would like to write a grammar in SableCC and then analyze the grammar and
> translate it into some graph representation? Is there a way to do this?
>
>
> Welson
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<!--
  This XSL file produces output from SableCC generated XML files.
  It creates input for the dot tool to vizialize a grammar.
  See http://www.sablecc.org/ for sablecc and
  http://www.research.att.com/sw/tools/graphviz/ for the dot tool.
-->

  <xsl:output method="text"/>

<xsl:template match="parser">
digraph mygraph {
    <xsl:for-each select="productions/production">
      <xsl:value-of select="concat(@ename, ' [shape=diamond,label=&quot;', @name, '&quot;]', '&#10;')"/>
      <xsl:for-each select="alt">
        <xsl:value-of select="concat(@ename, ' [shape=box,label=&quot;', ../@name, '.', @name, '&quot;]', '&#10;')"/>
        <xsl:value-of select="concat(../@ename, ' -> ', @ename, '&#10;')"/>

        <xsl:for-each select="elem">
          <xsl:if test="@is_token">
            <xsl:value-of select="concat(../@ename, ' -> ', @etype, ' [label=&quot;', @name, @modifier, '&quot;]', '&#10;')"/>
            <xsl:value-of select="concat(@etype, ' [shape=ellipse,label=&quot;', @type, '&quot;]', '&#10;')"/>
          </xsl:if>

          <xsl:if test="not(@is_token)">
            <xsl:value-of select="concat(../@ename, ' -> ', @etype, ' [label=&quot;', @name, @modifier, '&quot;]', '&#10;')"/>
          </xsl:if>
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
}
</xsl:template>
</xsl:stylesheet>

Package expression;

Helpers

    digit = ['0' .. '9'];
    tab = 9;
    cr = 13;
    lf = 10;
    eol = cr lf | cr | lf;

    blank = (' ' | tab | eol)+;

Tokens
    l_par = '(';
    r_par = ')';
    plus = '+';
    minus = '-';
    mult = '*';
    div = '/';
    semi = ';';

    blank = blank;
    number = digit+;

    one = 'one';
    two = 'two';
    three = 'three';

    random = 'random_digit';


Ignored Tokens

    blank;

Productions

    grammar           = exp_list                    {-> New grammar ([exp_list.exp])}
                      ;

    exp_list          {-> exp*} =
                        {list}    exp_list separator exp {-> [exp_list.exp exp.exp] }
                      | {single}  exp               {-> [exp.exp] }
                      ;

    exp               {-> exp} =  
                        {plus}    exp plus factor   {-> New exp.plus (exp.exp, factor.exp) }
                      | {minus}   exp minus factor  {-> New exp.minus (exp.exp, factor.exp) }
                      | {factor}  factor            {-> factor.exp }
                      ;

    factor            {-> exp} =
                        {mult}    factor mult term  {-> New exp.mult (factor.exp, term.exp) }
                      | {div}     factor div term   {-> New exp.div (factor.exp, term.exp) }
                      | {term}    term              {-> term.exp }
                      ;

    term              {-> exp} =
                        {number}  number            {-> New exp.number(number) }
                      | {exp}     l_par exp r_par   {-> exp.exp }
                      | {textual} textual+          {-> New exp.textual ([textual]) }
                      | {random_x2} random_x2       {-> New exp.random_x2 (random_x2.ran1, random_x2.ran2) }
                      ;

    textual           =
                        {t1}      one
                      | {t2}      two
                      | {t3}      three
                      ;

    random_x2         {-> [ran1]:random [ran2]:random} =
                        [ran1]:random [ran2]:random {-> ran1 ran2 }
                      ;

    separator {-> }  =
                        {semicolon} semi {-> }
                      ;
                      

Abstract Syntax Tree

    grammar           = exp*
                      ;

    exp               =
                        {plus}    [l]:exp  [r]:exp |
                        {minus}   [l]:exp  [r]:exp |
                        {div}     [l]:exp  [r]:exp |
                        {mult}    [l]:exp  [r]:exp |
                        {textual} textual* |
                        {random_x2} [r1]:random [r2]:random |
                        {number}  number
                      ;

    textual            =
                        {t1}      one
                      | {t2}      two
                      | {t3}      three
                      ;

digraph mygraph {
    PGrammar [shape=diamond,label="grammar"]
AGrammar [shape=box,label="grammar.grammar"]
PGrammar -> AGrammar
AGrammar -> PExp [label="exp*"]
PExp [shape=diamond,label="exp"]
APlusExp [shape=box,label="exp.plus"]
PExp -> APlusExp
APlusExp -> PExp [label="l"]
APlusExp -> PExp [label="r"]
AMinusExp [shape=box,label="exp.minus"]
PExp -> AMinusExp
AMinusExp -> PExp [label="l"]
AMinusExp -> PExp [label="r"]
ADivExp [shape=box,label="exp.div"]
PExp -> ADivExp
ADivExp -> PExp [label="l"]
ADivExp -> PExp [label="r"]
AMultExp [shape=box,label="exp.mult"]
PExp -> AMultExp
AMultExp -> PExp [label="l"]
AMultExp -> PExp [label="r"]
ATextualExp [shape=box,label="exp.textual"]
PExp -> ATextualExp
ATextualExp -> PTextual [label="textual*"]
ARandomX2Exp [shape=box,label="exp.random_x2"]
PExp -> ARandomX2Exp
ARandomX2Exp -> TRandom [label="r1"]
TRandom [shape=ellipse,label="random"]
ARandomX2Exp -> TRandom [label="r2"]
TRandom [shape=ellipse,label="random"]
ANumberExp [shape=box,label="exp.number"]
PExp -> ANumberExp
ANumberExp -> TNumber [label="number"]
TNumber [shape=ellipse,label="number"]
PTextual [shape=diamond,label="textual"]
AT1Textual [shape=box,label="textual.t1"]
PTextual -> AT1Textual
AT1Textual -> TOne [label="one"]
TOne [shape=ellipse,label="one"]
AT2Textual [shape=box,label="textual.t2"]
PTextual -> AT2Textual
AT2Textual -> TTwo [label="two"]
TTwo [shape=ellipse,label="two"]
AT3Textual [shape=box,label="textual.t3"]
PTextual -> AT3Textual
AT3Textual -> TThree [label="three"]
TThree [shape=ellipse,label="three"]

}

Attachment: out.png
Description: PNG image


--- End Message ---