我想使用 lark 为 C# 6 生成解析器。
我找到了一个C# 6 antlr 语法,但它不适用于开箱即用的云雀。
有没有人可以告诉我如何将 g4 语法重新格式化为我可以喂百灵的东西?
看来 lark 和 g4 的格式有点不一样。
from lark import Lark
filename = 'grammar.bnf'
with open(filename,'r') as file:
output = file.read()
parser = Lark(output, start='compilation_unit')
grammar.bnf
包含我上面提到的 C# 6 语法,起点是compilation_unit
.
摘自原始 bnf 文件:
parser grammar CSharpParser;
options { tokenVocab=CSharpLexer; }
// entry point
compilation_unit
: BYTE_ORDER_MARK? extern_alias_directives? using_directives?
global_attribute_section* namespace_member_declarations? EOF
;
//B.2 Syntactic grammar
//B.2.1 Basic concepts
namespace_or_type_name
: (identifier type_argument_list? | qualified_alias_member) ('.' identifier type_argument_list?)*
;
[Rest of the file]
请注意,我删除了入口点之前的所有内容compilation_unit
:
parser grammar CSharpParser;
options { tokenVocab=CSharpLexer; }