Search code examples
c#keywordextractgold-parser

Extracting keywords from GOLD-Parser CGT-File


i have defined a grammar with many rules, that uses many keywords. imagine it like this (just with more of these rules and more keywords):

<keyword> ::= 'public' | 'protected' | 'private'

the gold-parser-system is generating a compiled grammar table (CGT) file, which is used by the several engines, in my case the calitha-engine for c#.

in order to realize some syntax-highlighting for my sourcecode which i want to parse, i want to get all the keywords of a range of rules. how do i extract them?


Solution

  • Apart from generating "cgt" output file, Gold Builder has also XML output capability.

    If you feel comfortable with XML processing, I suggest you to extract the keywords using XML as one possible solution (See documentation).

    Here is a sample grammar XML file :

    <?GOLDParserTables version="1.0"?>
    <Tables>
            <Parameters>
                    <Parameter Name="Name" Value="(Untitled)"/>
                    <Parameter Name="Author" Value="(Unknown)"/>
                    <Parameter Name="Version" Value="(Not Specified)"/>
                    <Parameter Name="About" Value=""/>
                    <Parameter Name="Case Sensitive" Value="True"/>
                    <Parameter Name="Start Symbol" Value="8"/>
            </Parameters>
            <SymbolTable Count="12">
                    <Symbol Index="0" Name="EOF" Kind="3"/>
                    <Symbol Index="1" Name="Error" Kind="7"/>
                    <Symbol Index="2" Name="Whitespace" Kind="2"/>
                    <Symbol Index="3" Name="Comment End" Kind="5"/>
                    <Symbol Index="4" Name="Comment Line" Kind="6"/>
                    <Symbol Index="5" Name="Comment Start" Kind="4"/>
                    <Symbol Index="6" Name="Number" Kind="1"/>
                    <Symbol Index="7" Name="Word" Kind="1"/>
                    <Symbol Index="8" Name="Main" Kind="0"/>
                    <Symbol Index="9" Name="Number" Kind="0"/>
                    <Symbol Index="10" Name="Value" Kind="0"/>
                    <Symbol Index="11" Name="Word" Kind="0"/>
            </SymbolTable>
            <RuleTable Count="6">
                    <Rule Index="0" NonTerminalIndex="9" SymbolCount="1">
                            <RuleSymbol SymbolIndex="6"/>
                    </Rule>
                    <Rule Index="1" NonTerminalIndex="11" SymbolCount="1">
                            <RuleSymbol SymbolIndex="7"/>
                    </Rule>
                    <Rule Index="2" NonTerminalIndex="8" SymbolCount="2">
                            <RuleSymbol SymbolIndex="10"/>
                            <RuleSymbol SymbolIndex="8"/>
                    </Rule>
                    <Rule Index="3" NonTerminalIndex="8" SymbolCount="1">
                            <RuleSymbol SymbolIndex="10"/>
                    </Rule>
                    <Rule Index="4" NonTerminalIndex="10" SymbolCount="1">
                            <RuleSymbol SymbolIndex="11"/>
                    </Rule>
                    <Rule Index="5" NonTerminalIndex="10" SymbolCount="1">
                            <RuleSymbol SymbolIndex="9"/>
                    </Rule>
            </RuleTable>
            <CharSetTable Count="6">
                    <CharSet Index="0" Count="7">
                            <Char UnicodeIndex="9"/>
                            <Char UnicodeIndex="10"/>
                            <Char UnicodeIndex="11"/>
                            <Char UnicodeIndex="12"/>
                            <Char UnicodeIndex="13"/>
                            <Char UnicodeIndex="32"/>
                            <Char UnicodeIndex="160"/>
                    </CharSet>
                    <CharSet Index="1" Count="1">
                            <Char UnicodeIndex="42"/>
                    </CharSet>
                    <CharSet Index="2" Count="1">
                            <Char UnicodeIndex="59"/>
                    </CharSet>
                    <CharSet Index="3" Count="1">
                            <Char UnicodeIndex="47"/>
                    </CharSet>
                    <CharSet Index="4" Count="10">
                            <Char UnicodeIndex="48"/>
                            <Char UnicodeIndex="49"/>
                            <Char UnicodeIndex="50"/>
                            <Char UnicodeIndex="51"/>
                            <Char UnicodeIndex="52"/>
                            <Char UnicodeIndex="53"/>
                            <Char UnicodeIndex="54"/>
                            <Char UnicodeIndex="55"/>
                            <Char UnicodeIndex="56"/>
                            <Char UnicodeIndex="57"/>
                    </CharSet>
                    <CharSet Index="5" Count="52">
                            <Char UnicodeIndex="65"/>
                            <Char UnicodeIndex="66"/>
                            <Char UnicodeIndex="67"/>
                            <Char UnicodeIndex="68"/>
                            <Char UnicodeIndex="69"/>
                            <Char UnicodeIndex="70"/>
                            <Char UnicodeIndex="71"/>
                            <Char UnicodeIndex="72"/>
                            <Char UnicodeIndex="73"/>
                            <Char UnicodeIndex="74"/>
                            <Char UnicodeIndex="75"/>
                            <Char UnicodeIndex="76"/>
                            <Char UnicodeIndex="77"/>
                            <Char UnicodeIndex="78"/>
                            <Char UnicodeIndex="79"/>
                            <Char UnicodeIndex="80"/>
                            <Char UnicodeIndex="81"/>
                            <Char UnicodeIndex="82"/>
                            <Char UnicodeIndex="83"/>
                            <Char UnicodeIndex="84"/>
                            <Char UnicodeIndex="85"/>
                            <Char UnicodeIndex="86"/>
                            <Char UnicodeIndex="87"/>
                            <Char UnicodeIndex="88"/>
                            <Char UnicodeIndex="89"/>
                            <Char UnicodeIndex="90"/>
                            <Char UnicodeIndex="97"/>
                            <Char UnicodeIndex="98"/>
                            <Char UnicodeIndex="99"/>
                            <Char UnicodeIndex="100"/>
                            <Char UnicodeIndex="101"/>
                            <Char UnicodeIndex="102"/>
                            <Char UnicodeIndex="103"/>
                            <Char UnicodeIndex="104"/>
                            <Char UnicodeIndex="105"/>
                            <Char UnicodeIndex="106"/>
                            <Char UnicodeIndex="107"/>
                            <Char UnicodeIndex="108"/>
                            <Char UnicodeIndex="109"/>
                            <Char UnicodeIndex="110"/>
                            <Char UnicodeIndex="111"/>
                            <Char UnicodeIndex="112"/>
                            <Char UnicodeIndex="113"/>
                            <Char UnicodeIndex="114"/>
                            <Char UnicodeIndex="115"/>
                            <Char UnicodeIndex="116"/>
                            <Char UnicodeIndex="117"/>
                            <Char UnicodeIndex="118"/>
                            <Char UnicodeIndex="119"/>
                            <Char UnicodeIndex="120"/>
                            <Char UnicodeIndex="121"/>
                            <Char UnicodeIndex="122"/>
                    </CharSet>
            </CharSetTable>
            <DFATable Count="11" InitialState="0">
                    <DFAState Index="0" EdgeCount="6" AcceptSymbol="-1">
                            <DFAEdge CharSetIndex="0" Target="1"/>
                            <DFAEdge CharSetIndex="1" Target="2"/>
                            <DFAEdge CharSetIndex="2" Target="4"/>
                            <DFAEdge CharSetIndex="3" Target="5"/>
                            <DFAEdge CharSetIndex="4" Target="7"/>
                            <DFAEdge CharSetIndex="5" Target="8"/>
                    </DFAState>
                    <DFAState Index="1" EdgeCount="1" AcceptSymbol="2">
                            <DFAEdge CharSetIndex="0" Target="1"/>
                    </DFAState>
                    <DFAState Index="2" EdgeCount="1" AcceptSymbol="-1">
                            <DFAEdge CharSetIndex="3" Target="3"/>
                    </DFAState>
                    <DFAState Index="3" EdgeCount="0" AcceptSymbol="3">
                    </DFAState>
                    <DFAState Index="4" EdgeCount="0" AcceptSymbol="4">
                    </DFAState>
                    <DFAState Index="5" EdgeCount="1" AcceptSymbol="-1">
                            <DFAEdge CharSetIndex="1" Target="6"/>
                    </DFAState>
                    <DFAState Index="6" EdgeCount="0" AcceptSymbol="5">
                    </DFAState>
                    <DFAState Index="7" EdgeCount="1" AcceptSymbol="6">
                            <DFAEdge CharSetIndex="4" Target="7"/>
                    </DFAState>
                    <DFAState Index="8" EdgeCount="2" AcceptSymbol="-1">
                            <DFAEdge CharSetIndex="5" Target="9"/>
                            <DFAEdge CharSetIndex="4" Target="10"/>
                    </DFAState>
                    <DFAState Index="9" EdgeCount="2" AcceptSymbol="7">
                            <DFAEdge CharSetIndex="5" Target="9"/>
                            <DFAEdge CharSetIndex="4" Target="10"/>
                    </DFAState>
                    <DFAState Index="10" EdgeCount="2" AcceptSymbol="7">
                            <DFAEdge CharSetIndex="5" Target="9"/>
                            <DFAEdge CharSetIndex="4" Target="10"/>
                    </DFAState>
            </DFATable>
            <LALRTable Count="8"  InitialState="0">
                    <LALRState Index="0" ActionCount="6">
                            <LALRAction SymbolIndex="6" Action="1" Value="1"/>
                            <LALRAction SymbolIndex="7" Action="1" Value="2"/>
                            <LALRAction SymbolIndex="8" Action="3" Value="3"/>
                            <LALRAction SymbolIndex="9" Action="3" Value="4"/>
                            <LALRAction SymbolIndex="10" Action="3" Value="5"/>
                            <LALRAction SymbolIndex="11" Action="3" Value="6"/>
                    </LALRState>
                    <LALRState Index="1" ActionCount="3">
                            <LALRAction SymbolIndex="0" Action="2" Value="0"/>
                            <LALRAction SymbolIndex="6" Action="2" Value="0"/>
                            <LALRAction SymbolIndex="7" Action="2" Value="0"/>
                    </LALRState>
                    <LALRState Index="2" ActionCount="3">
                            <LALRAction SymbolIndex="0" Action="2" Value="1"/>
                            <LALRAction SymbolIndex="6" Action="2" Value="1"/>
                            <LALRAction SymbolIndex="7" Action="2" Value="1"/>
                    </LALRState>
                    <LALRState Index="3" ActionCount="1">
                            <LALRAction SymbolIndex="0" Action="4" Value="0"/>
                    </LALRState>
                    <LALRState Index="4" ActionCount="3">
                            <LALRAction SymbolIndex="0" Action="2" Value="5"/>
                            <LALRAction SymbolIndex="6" Action="2" Value="5"/>
                            <LALRAction SymbolIndex="7" Action="2" Value="5"/>
                    </LALRState>
                    <LALRState Index="5" ActionCount="7">
                            <LALRAction SymbolIndex="6" Action="1" Value="1"/>
                            <LALRAction SymbolIndex="7" Action="1" Value="2"/>
                            <LALRAction SymbolIndex="8" Action="3" Value="7"/>
                            <LALRAction SymbolIndex="9" Action="3" Value="4"/>
                            <LALRAction SymbolIndex="10" Action="3" Value="5"/>
                            <LALRAction SymbolIndex="11" Action="3" Value="6"/>
                            <LALRAction SymbolIndex="0" Action="2" Value="3"/>
                    </LALRState>
                    <LALRState Index="6" ActionCount="3">
                            <LALRAction SymbolIndex="0" Action="2" Value="4"/>
                            <LALRAction SymbolIndex="6" Action="2" Value="4"/>
                            <LALRAction SymbolIndex="7" Action="2" Value="4"/>
                    </LALRState>
                    <LALRState Index="7" ActionCount="1">
                            <LALRAction SymbolIndex="0" Action="2" Value="2"/>
                    </LALRState>
            </LALRTable>
    </Tables>
    

    XML as human readable format is self explanatory, you have access to all tables : Symbol, Rule, DFA and LALR and that's it.

    Of course, cooking a "pgt" file (program template) is the usual way to process "cgt" or even write some extract utility for the purpose on top of a Gold Engine.