Git Product home page Git Product logo

Comments (4)

jon-hanson avatar jon-hanson commented on July 26, 2024

Hi,

I suspect the problem is that if your parser operates directly on the chars (as opposed to a token stream), then this grammar is not LL(1). When the parser encouters a "*" within the comment, it needs to look an extra char ahead to verify that "/" is the following char and therefore we are at the comment end. This makes it an LL(2) grammar, which this parser doesn't support.

One workardound is to first convert the char stream to a token stream, such that "/" and "/" become single tokens (e.g. COMMENT_START and COMMENT_END).

Some parsers get around this restriction by allowing backtracking. The problem with this is when the parser encounters an actual syntax error it then backtracks to see if there exists another route, meaning you then lose the location at which the error occurred, and the resultant error message is usually meaningless.

I'll have a play around with this to see whether there are any other solutions.

from funcj.

fponticelli avatar fponticelli commented on July 26, 2024

I got a hacky solution working but it has flaws:

public static final Parser<Chr, String> blockComment() {
        final String[] prev = {""};
        return string("/*")
                .andR(satisfy("closing block comment", c -> {
                    if (c.equals('/') && prev[0] == "*") {
                        return false;
                    } else {
                        prev[0] = String.valueOf(c);
                        return true;
                    }
                }).many())
                .map(Chr:: listToString)
                .map(s -> s.substring(0, s.length() - 2));
    }

The main issue is that you have to reconstruct the parser every time it is used.

from funcj.

jon-hanson avatar jon-hanson commented on July 26, 2024

Does look somewhat hacky, but if it works...

I'll close this issue for now.

from funcj.

fponticelli avatar fponticelli commented on July 26, 2024

It is totally hacky, I am not proud of it :)

To your original comment, I think I agree ... the performance hit might be minimal and the greater flexibility might be worth it.

from funcj.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.