Skip to content

Token types from internal rules end up in parser #302

Open

Description

@andreasabel

In a grammar like

Parsable.  Exp ::= Integer ;
Internal.  Exp ::= String;

The definition of String should not end up in the lexer, and no parser for String should be generated.

Here is an example where parsing actually fails, similar to #204:

Main.              Prg ::= Ident     ;
internal IId.      Prg ::= Id        ;

token Id letter+ ;  -- This overlaps with Ident, but should not confuse the parser.

The generated (Haskell) parser does not accept input foo but should, since this is an Ident and Id is only used internally. (Dead code should not affect the semantics.)

Activity

  1. added
    internal rules
    lexer
    parser
    on Oct 9, 2020
  2. added this to the 2.8.4 milestone on Oct 9, 2020
  3. self-assigned this
    on Oct 9, 2020
  4. modified the milestones: 2.8.4, 3.0 on Oct 9, 2020
  5. added a commit that references this issue on Oct 9, 2020
  6. fonfalleh commented on Nov 1, 2020

    @fonfalleh

    Hi,
    I've been playing around writing a parser for Lilypond and I've been having an issue that I think relates to this issue.
    In particular, the following should be equivalent, but I could only parse the latter: c_\ff, c _\ff.
    I believe parsing fails because c_ is a valid Ident, however I am not using any Idents in my bnfc rules.

    I managed to work around this by removing removing the references to identifier character, or $i in (Haskell backend) the generated Lex.x file.

    Will this be solved by this issue, or could it be a separate issue?
    Kind regards,
    Jacob

  7. andreasabel commented on Nov 2, 2020

    @andreasabel
    MemberAuthor

    Hello @fonfalleh, thanks for he report.

    Atm BNFC does not correctly support grammars that do not have a notion of identifier. This is because keywords are first parsed (more precisely: lexed) as identifiers, and then looked up in a keyword dictionary. For instance, keyword "if" matches also the regex for an Ident and is lexed as that and then turned into a keyword token.

    A fix would be to include keywords in the generated lexer definition as their separate rules. However, this blows up the generated lexer significantly. (This might even exceed the limits on the automaton size in e.g. in ocamllex.) Thus, in practice, one lexes keywords as identifiers.

    I think allowing the redefinition of Ident (#197) would solve your problem.

  8. fonfalleh commented on Nov 10, 2020

    @fonfalleh

    Thanks!
    I make use of token for variable declarations and references, but they can only contain letters, so I didn't use Ident for that in particular.

    Does this mean that everything that can be lexed must match an Ident? Not sure if I follow.

  9. andreasabel commented on Nov 11, 2020

    @andreasabel
    MemberAuthor

    Does this mean that everything that can be lexed must match an Ident? Not sure if I follow.

    No, but BNFC puts a regex for Ident in the lexer definition, whether you use it or not. This means that e.g. c_\ff will be lexed as Ident("c_") \ff rather than c _\ff because of the longest match rule (the match "c_" is longer than the match "c" and thus takes precedence). This explains the problems you are experiencing with your generated lexer.

    Your problem would be fixed if Ident was not included in the lexer definition, but then the lexer has handle keywords differently.

  10. fonfalleh commented on Nov 11, 2020

    @fonfalleh

    I see, thank you!

  11. jasper-e commented on Sep 3, 2023

    @jasper-e

    No, but BNFC puts a regex for Ident in the lexer definition, whether you use it or not. This means that e.g. c_\ff will be lexed as Ident("c_") \ff rather than c _\ff because of the longest match rule (the match "c_" is longer than the match "c" and thus takes precedence). This explains the problems you are experiencing with your generated lexer.

    Your problem would be fixed if Ident was not included in the lexer definition, but then the lexer has handle keywords differently.

    Let me ask for a command line option to omit auto-including Ident.
    I do not want to have this as a default behavior, just would like to be able to have the flag in my Makefile when I need it...
    Since long I used workarounds (diff/patch) to omit this but every now and then after an upgrade my patchfile gets broken...

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

Metadata

Metadata

Assignees

Labels

buginternal ruleslexerparser

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions