Skip to content

Commit

Permalink
better DCG generation; generic intermediate format for semi-parsed gr…
Browse files Browse the repository at this point in the history
…ammar

git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@716 ab42f6e0-554d-0410-b580-99e487e6eeb2
  • Loading branch information
grammarware committed Nov 19, 2009
1 parent 49f454a commit 18546c3
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 52 deletions.
50 changes: 50 additions & 0 deletions shared/xsd/rgf.xsd
@@ -0,0 +1,50 @@
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:rgf="http://planet-sl.org/rgf"
targetNamespace="http://planet-sl.org/rgf">

<xsd:annotation>
<xsd:documentation>
Raw Grammar Format
</xsd:documentation>
</xsd:annotation>

<xsd:element name="grammar">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="entry" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="key" type="xsd:string"/>
<xsd:element name="rhs" maxOccurs="unbounded">
<xsd:complexType>
<xsd:group ref="rgf:symbol" maxOccurs="unbounded"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:group name="symbol">
<xsd:choice>
<xsd:element name="nonterminal" type="xsd:string"/>
<xsd:element name="terminal" type="xsd:string"/>

<xsd:element name="meta-repetition" type="xsd:string"/>
<xsd:element name="meta-repetition-start" type="xsd:string"/>
<xsd:element name="meta-repetition-end" type="xsd:string"/>

<xsd:element name="meta-choice" nillable="true"/>
<xsd:element name="meta-group-start" nillable="true"/>
<xsd:element name="meta-group-end" nillable="true"/>
<xsd:element name="meta-optionality" nillable="true"/>
<xsd:element name="meta-optional-start" nillable="true"/>
<xsd:element name="meta-optional-end" nillable="true"/>
</xsd:choice>
</xsd:group>

</xsd:schema>

136 changes: 84 additions & 52 deletions shared/xsl/edd2dcg.xslt
@@ -1,25 +1,13 @@
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:edd="http://planet-sl.org/edd"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
xmlns:edd="http://planet-sl.org/edd">

<xsl:output
method="text"
encoding="UTF-8"
omit-xml-declaration="yes"
/>

<!--
module Main
exports context-free start-symbols
<xsl:apply-templates select="./root" />
context-free sorts
<xsl:apply-templates select="./bgf:production" />
context-free syntax
<xsl:apply-templates select="./bgf:production" />
-->

<xsl:template match="/edd:dialect">
<xsl:text>grammar(g(Ps)) --> productions(Ps).
productions([H|T]) --> production(H), productions(T).
Expand All @@ -41,9 +29,7 @@ symbol(nt(N)) --> name(N).
<xsl:if test="production/separator">
<!-- separator symbol -->
<xsl:call-template name="symbol2definition">
<xsl:with-param name="predicate">
<xsl:text>separatorsymbol</xsl:text>
</xsl:with-param>
<xsl:with-param name="predicate" select="'separatorsymbol'"/>
<xsl:with-param name="symbol" select="production/separator"/>
</xsl:call-template>
<xsl:text>alternatives([A|T]) --> symbols(A), separatorsymbol, alternatives(T).</xsl:text>
Expand All @@ -53,30 +39,22 @@ alternatives([S]) --> symbols(S), terminatorsymbol.
</xsl:text>
<!-- defining symbol -->
<xsl:call-template name="symbol2definition">
<xsl:with-param name="predicate">
<xsl:text>definingsymbol</xsl:text>
</xsl:with-param>
<xsl:with-param name="predicate" select="'definingsymbol'"/>
<xsl:with-param name="symbol" select="production/defining"/>
</xsl:call-template>
<!-- terminator symbol -->
<xsl:call-template name="symbol2definition">
<xsl:with-param name="predicate">
<xsl:text>terminatorsymbol</xsl:text>
</xsl:with-param>
<xsl:with-param name="predicate" select="'terminatorsymbol'"/>
<xsl:with-param name="symbol" select="production/terminator"/>
</xsl:call-template>
<!-- grouping symbols -->
<xsl:if test="fragment/group">
<xsl:call-template name="symbol2definition">
<xsl:with-param name="predicate">
<xsl:text>groupstart</xsl:text>
</xsl:with-param>
<xsl:with-param name="predicate" select="'groupstart'"/>
<xsl:with-param name="symbol" select="fragment/group/start"/>
</xsl:call-template>
<xsl:call-template name="symbol2definition">
<xsl:with-param name="predicate">
<xsl:text>groupend</xsl:text>
</xsl:with-param>
<xsl:with-param name="predicate" select="'groupend'"/>
<xsl:with-param name="symbol" select="fragment/group/end"/>
</xsl:call-template>
<xsl:text>
Expand All @@ -87,17 +65,13 @@ symbolchoices([S]) --> symbols(S).
</xsl:if>
<xsl:if test="fragment/choice-symbol">
<xsl:call-template name="symbol2definition">
<xsl:with-param name="predicate">
<xsl:text>choicesymbol</xsl:text>
</xsl:with-param>
<xsl:with-param name="predicate" select="'choicesymbol'"/>
<xsl:with-param name="symbol" select="fragment/choice-symbol"/>
</xsl:call-template>
</xsl:if>
<xsl:if test="optionality/symbol">
<xsl:call-template name="symbol2definition">
<xsl:with-param name="predicate">
<xsl:text>optionalitysymbol</xsl:text>
</xsl:with-param>
<xsl:with-param name="predicate" select="'optionalitysymbol'"/>
<xsl:with-param name="symbol" select="optionality/symbol"/>
</xsl:call-template>
<xsl:text>symbol(opt(nt(N))) --> name(N), optionalitysymbol.
Expand All @@ -106,29 +80,31 @@ symbol(opt(br(S))) --> groupstart, symbolchoices(S), groupend, optionalitysymbol
</xsl:if>
<!-- not implemented: optionality pair -->
<!-- repetitions -->
<xsl:text>
symbol(r(R,nt(N))) --> name(N), repetition(R).
symbol(r(R,br(S))) --> groupstart, symbolchoices(S), groupend, repetition(R).
</xsl:text>
<xsl:for-each select="repetition">
<xsl:call-template name="symbol2definition">
<xsl:with-param name="predicate">
<xsl:text>repetition</xsl:text>
<xsl:value-of select="name"/>
</xsl:with-param>
<xsl:text>repetition(</xsl:text>
<xsl:value-of select="name"/>
<xsl:text>) --> </xsl:text>
<xsl:call-template name="transformSymbol">
<xsl:with-param name="symbol" select="markup/symbol"/>
</xsl:call-template>
<xsl:text>
symbol(</xsl:text>
<xsl:value-of select="name"/>
<xsl:text>(nt(N))) --> name(N), repetition</xsl:text>
<xsl:value-of select="name"/>
<xsl:text>.
symbol(</xsl:text>
<xsl:value-of select="name"/>
<xsl:text>(br(S))) --> groupstart, symbolchoices(S), groupend, repetition</xsl:text>
<xsl:value-of select="name"/>
<xsl:text>.
</xsl:text>
</xsl:for-each>
<!-- terminals -->
<xsl:if test="terminal">
<!-- double quote (34) hard coded; single quote is 39 -->
<xsl:text>symbol(t(Y)) --> terminalstart, string(V), {\+ member(34, V), string_to_list(Y,V)}, terminalend.
terminalstart --> [34].
terminalend --> [34].
</xsl:text>
</xsl:if>
<!-- everything else -->
<xsl:text>
newline --> [10].
<![CDATA[newline --> [10].
layout --> [0' ], layout.
layout --> [0' ], layout.
layout --> [].
Expand All @@ -138,9 +114,65 @@ string([H|T1],[H|T2],X) :- string(T1,T2,X).
name(V) --> layout, letter(H), letters(T), { atom_chars(V,[H|T]) }.
letters([H|T]) --> letter(H), letters(T).
letters([]) --> [].
<![CDATA[letter(H,[H|T],T) :- H >= 0'a, H =< 0'z.
letter(H,[H|T],T) :- H >= 0'A, H =< 0'Z.]]>
letter(H,[H|T],T) :- H >= 0'a, H =< 0'z.
letter(H,[H|T],T) :- H >= 0'A, H =< 0'Z.
letter(0'-,[0'-|T],T).
% Pretty-printing a flat-token-grammar
writegrammar(g(Ps)) :-
write('<?xml version="1.0" encoding="UTF-8"?>'),nl,
write('<rgf:grammar xmlns:rgf="http://planet-sl.org/rgf">'), nl,
writeproductions(Ps),
write('</rgf:grammar>').
writeproductions([H|T]) :- writeproduction(H), writeproductions(T).
writeproductions([]).
writeproduction(p(NT,As)) :-
write('<entry>'), nl,
write('<key>'),
write(NT),
write('</key>'), nl,
writealternatives(As),
write('</entry>'), nl.
writesymbols([H|T]) :- writesymbol(H), writesymbols(T).
writesymbols([]).
writesymbol(nt(N)) :- writent(N).
writesymbol(t(V)) :- writet(V).
writealternatives([A|T]) :-
write('<rhs>'), nl,
writesymbols(A),
write('</rhs>'), nl,
writealternatives(T).
writealternatives([]).
writegroupstart :- write('<meta-group-start/>'), nl.
writegroupend :- write('<meta-group-end/>'), nl.
writesymbol(br(S)) :- writegroupstart, writesymbolchoices(S), writegroupend.
writesymbolchoices([H1,H2|T]) :- writesymbols(H1), writechoicesymbol, writesymbolchoices([H2|T]).
writesymbolchoices([S]) :- writesymbols(S).
writechoicesymbol :- write('<meta-choice/>'), nl.
writeoptionalitysymbol :- write('<meta-optionality/>'), nl.
writesymbol(opt(nt(N))) :- writent(N), writeoptionalitysymbol.
writesymbol(opt(br(S))) :- writegroupstart, writesymbolchoices(S), writegroupend, writeoptionalitysymbol.
writesymbol(r(R,nt(N))) :- writent(N), writerepetition(R).
writesymbol(r(R,br(S))) :- writegroupstart, writesymbolchoices(S), writegroupend, writerepetition(R).
writerepetition(R) :- write('<meta-repetition>'), write(R), write('</meta-repetition>'), nl.
writenewline :- nl.
writestring(X) :- write('<text>'), write(X), write('</text>'), nl.
writent(X) :- write('<nonterminal>'), write(X), write('</nonterminal>'), nl.
writet(X) :- write('<terminal>'), write(X), write('</terminal>'), nl.
% The main wrapper thing
main(File) :- parseFile(File,grammar,S), writegrammar(S).
parseFile(File,P,R) :-
open(File,read,Stream,[]),
read_stream_to_codes(Stream, Contents),
close(Stream),
apply(P,[R,Contents,Rest]),
eof(Rest,_).
eof([],[]).
eof([0' |T],R) :- eof(T,R).
eof([10|T],R) :- eof(T,R).]]>
</xsl:text>
</xsl:template>

Expand Down

0 comments on commit 18546c3

Please sign in to comment.