Open mingodad opened 2 years ago
After playing a bit with xslt
I managed to create one based on html.xslt
to output an EBNF
understood by https://www.bottlecaps.de/rr/ui :
<?xml version='1.0'?>
<xsl:stylesheet
version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform' >
<xsl:output method='html' indent='yes'/>
<xsl:strip-space elements="*"/>
<xsl:output method="text" />
<xsl:template match='bnf'>
/*
Hyperlinked C++ BNF Grammar
By Alessio Marchetti
Version 3.6
Last updated: 10-Aug-2021
Purpose of this document
If you are a C++ developer then you sometimes need to have a look at the C++ BNF grammar. At least I do.
But reading a set of BNF rules is difficult since you have to jump from one rule to the next one, maybe pages away from it.
With this document I tried to make it easier to navigate among the BNF rules of the C++ language grammar; you can find here a hyperlinked graph whose nodes are the BNF productions of the C++ language grammar.
If you find errors or inconsistencies, please log an issue.
The BNF non-terminal for a C++ translation unit (translation-unit) is defined here.
The BNF non-terminal for a C++ preprocessing file (preprocessing-file) is defined here.
BNF Grammar Rules
*/
start_here ::= translation-unit // for easy navigation purpose
<xsl:apply-templates/>
</xsl:template>
<xsl:template match='section'>
<xsl:attribute name='name'><xsl:value-of select='@id'/></xsl:attribute>
//<xsl:value-of select='@id'/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match='rule'>
<xsl:attribute name='name'><xsl:value-of select='syntax'/></xsl:attribute>
<xsl:text>
</xsl:text><xsl:value-of select='syntax'/><xsl:text> ::=
	</xsl:text>
<xsl:choose>
<xsl:when test='@issue'>
<xsl:text>	// See </xsl:text>
<xsl:attribute name='href'>http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_toc.html</xsl:attribute>
<xsl:text>C++ Standard Core Language Issue</xsl:text>
<xsl:text> n. </xsl:text>
<xsl:value-of select='@issue'/>
</xsl:when>
<xsl:when test='@note and not(@issue)'>
<xsl:text>	// </xsl:text><xsl:value-of select='@note'/>
</xsl:when>
</xsl:choose>
<xsl:apply-templates/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match='syntax'/>
<xsl:template match='alternative'>
<xsl:if test="position() > 1">
<xsl:text>
	|</xsl:text>
</xsl:if>
<xsl:apply-templates/>
<xsl:if test='@issue'>
<xsl:text>	// See </xsl:text>
<xsl:attribute name='href'>http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_toc.html</xsl:attribute>
<xsl:text>C++ Standard Core Language Issue</xsl:text>
<xsl:text> n. </xsl:text>
<xsl:value-of select='@issue'/>
</xsl:if>
<xsl:if test="@note and not(@issue)">
<xsl:text>	// </xsl:text><xsl:value-of select='@note'/>
</xsl:if>
</xsl:template>
<xsl:template match='terminal'>
<xsl:variable name="quote">
<xsl:choose>
<xsl:when test="contains(., '"')">
<xsl:text>'</xsl:text>
</xsl:when>
<xsl:otherwise>"</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="@sentence='true'">
<xsl:text> </xsl:text><xsl:value-of select="$quote"/><xsl:value-of select='.'/><xsl:value-of select="$quote"/>
</xsl:when>
<xsl:otherwise>
<xsl:text> </xsl:text><xsl:value-of select="$quote"/><xsl:value-of select='.'/><xsl:value-of select="$quote"/><xsl:if test="@optional='true'">?</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match='non-terminal'>
<xsl:text> </xsl:text><xsl:value-of select='.'/><xsl:if test="@optional='true'">?</xsl:if>
</xsl:template>
</xsl:stylesheet>
Also for people looking for other related C++
grammars here are some I found:
Thanks @mingodad, I found the visual diagram quite useful!
Viewing https://alx71hub.github.io/hcb/ in a browser and selecting all and copying and pasting on an editor and manually fixing it to be an
EBNF
understood by https://www.bottlecaps.de/rr/ui we can have a nice navigable railroad diagram.Copy and paste the
EBNF
(manually incomplete translated) shown bellow on https://www.bottlecaps.de/rr/ui on the tabEdit Grammar
the click on the tabView Diagram
to see/download a navigable railroad diagram.