gpt4 book ai didi

c# - 从 GOLD-Parser CGT-File 中提取关键词

转载 作者:太空狗 更新时间:2023-10-30 01:25:08 29 4
gpt4 key购买 nike

我已经定义了一个有很多规则的语法,它使用了很多关键字。想象一下(只是有更多的规则和更多的关键字):

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

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

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

最佳答案

除了生成“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 作为人类可读格式是不言自明的,您可以访问所有表格:Symbol、Rule、DFA 和 LALR,仅此而已。

当然,制作“pgt”文件(程序模板)是处理“cgt”的常用方法,甚至为此目的在 Gold Engine 上编写一些提取实用程序。

关于c# - 从 GOLD-Parser CGT-File 中提取关键词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7619842/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com