从 GOLD-Parser CGT 文件中提取关键字

发布于 2024-12-07 16:32:57 字数 281 浏览 1 评论 0原文

我定义了一个包含许多规则的语法,它使用了许多关键字。想象一下这样(只是有更多的规则和更多的关键字):

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

gold-parser-system 正在生成一个编译的语法表(CGT)文件,该文件由多个引擎使用,在我的例子中是 c# 的 calitha-engine 。

为了实现我想要解析的源代码的语法突出显示,我想获取一系列规则的所有关键字。我如何提取它们?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↙温凉少女 2024-12-14 16:32:57

除了生成“cgt”输出文件外,Gold Builder 还具有 XML 输出功能。

如果您对 XML 处理感到满意,我建议您使用 XML 提取关键字作为一种可能的解决方案(请参阅文档)。

下面是一个示例语法 XML 文件:

<?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 作为人类可读格式是不言自明的,您可以访问所有表:符号、规则、DFA 和 LALR,仅此而已。

当然,烹饪“pgt”文件(程序模板)是处理“cgt”的常用方法,甚至在黄金引擎之上为此目的编写一些提取实用程序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文