Skip to content

fandango convert drops ANTLR non-greedy quantifiers (*?, +?, ??) #871

Description

@Nuhiat-Arefin

When converting ANTLR grammars to .fan, fandango convert strips the non-greedy ? from EBNF suffixes and keeps only the greedy quantifier.

Converter code causing the issue:

def visitEbnfSuffix(self, ctx):
    suffix = ctx.getText() if ctx else ""
    if len(suffix) > 1:
        self.addNote(f"was '{suffix}'")
        suffix = suffix[0]
    return suffix

Because of this logic:

  • *? becomes *
  • +? becomes +
  • ?? becomes ?

Minimal Repro

Grammar (bug_lazy.g4):

grammar BugLazy;
start: BlockComment;
BlockComment: '/*' .*? '*/';

Command:

fandango convert -o bug_lazy.fan bug_lazy.g4

Converted output:

<start> ::= <BlockComment> 
<BlockComment> ::= '/*' r'.'* '*/'  # NOTE: was '*?'

This also affects real-world grammars. For example, in g_c.g4:

MultiLineMacro: '#' (~[\n]*? '\\' '\r'? '\n')+ ~[\n]+ -> channel(HIDDEN);

is converted to:

<MultiLineMacro> ::= '#' (r'[^\n]'* '\\' '\r'? '\n')+ r'[^\n]'+  # NOTE: was '*?'; was '-> channel(HIDDEN)'

This is not semantics-preserving. In ANTLR, .*? is lazy/non-greedy; after conversion, it silently becomes greedy r'.'*.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions