0

I would like to use lark to generate a parser for C# 6.

I found a C# 6 antlr grammar but it does not work out-of-the-box with lark.

Is there anybody who could tell me how to reformat the g4 grammar to something I can feed lark with?

It seems that the format between what expect lark and g4 are kinda different.

from lark import Lark

filename = 'grammar.bnf'

with open(filename,'r') as file:
    output = file.read()

parser = Lark(output, start='compilation_unit')

grammar.bnf contains the C# 6 grammar I mentioned above, with the starting point being compilation_unit.

Excerpt from the original bnf file:

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]

Note that I removed everything before the entry point compilation_unit:

parser grammar CSharpParser;

options { tokenVocab=CSharpLexer; }
Natalie Perret
  • 8,013
  • 12
  • 66
  • 129
  • Why do you think you can process ANTLR grammars with Lark? Lark handles BNF, not ANTLR’s flavored BNF-like syntax. Besides that, I see that Lark handles LR grammars, while ANTLR does LL grammars. In short: you can’t do this with Lark. – Bart Kiers Mar 05 '19 at 08:00
  • @BartKiers that was my point thought there was a way to convert the antlr bnf to lark ebnf variant, they look similar. Thanks for your answer, guess I need to stick to antlr then =/ – Natalie Perret Mar 05 '19 at 08:03
  • 1
    AFAIK, there is no way to easily/automatically convert ANTLR's LL grammar to a generic (Lark) (E)BNF LR variant. Yes, I'd stick with ANTLR. – Bart Kiers Mar 05 '19 at 08:29
  • 2
    You can actually use Lark to parse anything that ANTLR can parse. You just have to convert the syntax. – Erez Mar 07 '19 at 14:51

0 Answers0