GithubHelp home page GithubHelp logo

Infinite loop about pegjs HOT 8 CLOSED

pegjs avatar pegjs commented on May 28, 2024
Infinite loop

from pegjs.

Comments (8)

s3u avatar s3u commented on May 28, 2024

It actually is an error in the grammar.

start = scheme
scheme = ALPHA ( ALPHA / DIGIT / "+" / "-" / "." )* 
ALPHA = [a-zA-Z]
DIGIT = [0-9]

Would be nice to catch this.

from pegjs.

 avatar commented on May 28, 2024

I believe the error happens whenever you put non-terminal with asterisk within another non-terminal, that has asterisk after the other non-terminal as well (as shown in s3u comments). I think this fact can make a detection of this issue quite easy, although I think it is not necessary to consider this an error (I think a parser can be created for such a grammar). Fortunately, I believe this issue can be obeyed (that you can alternate your grammar in a way when it will not use such as rules), so the quick and dirty fix is just to make this issue visible, so the user knows what to avoid.

from pegjs.

tolmasky avatar tolmasky commented on May 28, 2024

You don't seem to need to need the double kleene situation to get this, and I believe infinite loop is actually "correct behavior".The reduction of this error seems to be:

start = .* (!.)*

What's happening is that you are using a kleene operator on an expression that expects zero length results. As such, yes, their are always infinite of these at the end of the file. The rule ALPHA = [a-zA-Z]* reduces to this as well because at the end of the file it matches zero-length versions of this indefinitely.

from pegjs.

tolmasky avatar tolmasky commented on May 28, 2024

Just to highlight that there are many ways to get this, the following also generates an infinite loop:

start = .* (!.)+

This also serves to show that its difficult to define "how many" matches should be allowed in these cases. If it was just "*" that produced this then there may be a tempting (but I believe still wrong) argument that it should just somehow know to not match at all.

from pegjs.

dmajda avatar dmajda commented on May 28, 2024

It would be nice to have a warning when generating a parser with a possibility to enter infinite loop on some inputs. However I am not 100% sure all such cases can be caught statically, and presence of actions/predicates would complicate things even if they can. I'll have to think about this more deeply.

But it's certainly not a priority now.

from pegjs.

brettz9 avatar brettz9 commented on May 28, 2024

Is there any way support could be built in to deal with recursion beyond even exiting upon it? I tried adapting the ABNF grammar, but am running into a loop: https://github.com/brettz9/js2peg/blob/rfc5234/examples/rfc5234.pegjs

from pegjs.

brettz9 avatar brettz9 commented on May 28, 2024

Turns out it was needed in my example, as I had a mistake...

from pegjs.

Phrogz avatar Phrogz commented on May 28, 2024

FWIW infinite loops are biting me hard write now in the tool I'm writing for editing and evaluating PEGs live. Certain transitory grammars produce a parser that then hangs the browser, killing your editing session (and losing recent unsaved work).

Edit: I just spent several hours moving to Web Workers so that I can detect and kill a parsing that is stuck in some endless recursion/loop.

from pegjs.

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.