Giter Site home page Giter Site logo

Comments (6)

fool2fish avatar fool2fish commented on July 20, 2024

From the question:

without an intervening */, unless it is inside double-quotes

so, this means:

\/\*([^*"]*|".*"|\*+[^/])*\*\/
             ^^^^
             any comment in quotes is regarded as normal text

from dragon-book-exercise-answers.

misaka-10032 avatar misaka-10032 commented on July 20, 2024

That's what I mean. That RegExp can handle /"xxx"/ , but cannot handle /"xx/ or "/xx/" . In fact, the first and second should be regarded as comments, but the third shouldn't, right?

from dragon-book-exercise-answers.

fool2fish avatar fool2fish commented on July 20, 2024

This answer do implies that: quotes in comment must be closed. Follow your thought, a stricter answer could be (in JavaScript,and you can ignore the space):

\/\*  (  [^*"]*  |  ".*"  |  "[^"]*?(?=\*\/)  |  \*+[^/]  )  *\*\/

I hope you don't intend to ask me: how about /*"*/\"*/, an such like.

Actually, in practice the comment rule is often more simple:

\/\*[\s\S]*?\*\/

But the third is another matter, it should be considered as a string rather than a comment. We can get some lex rules below:

\/\/.*$              {return COMMENT}
\/\*[\s\S]*?\*\/   {return COMMENT}
"(\\"|[^"])*"       {return STRING}

from dragon-book-exercise-answers.

misaka-10032 avatar misaka-10032 commented on July 20, 2024

I would prefer \/\*[\s\S]*?\*\/ :D. I get your solution for "comments in quotes" then. Actually, what I want to ask is, if it is beyond the ability of regExp to describe the pattern "comments not in quotes" in a single line? And we can only resort to other approaches to solve the problem?

p.s. I am a beginner, and your patient explanation does mean a lot of help to me. Thank you very much. :-)

from dragon-book-exercise-answers.

fool2fish avatar fool2fish commented on July 20, 2024

It is completely normal that a lex analyser comprise several lex rules(similar to regExp, and there is an rule example, plus a hand made lex analyser example). Thus, if you want to recognize both comment and string, you will have to use two(or three) rules. But if you only want to recognize comment, then "/*...*/" is illegal.

I'm not sure if I explained clearly, you'd better try to write a lex analyser by hand that only recognize comment and string, it may give you some stimulation.

from dragon-book-exercise-answers.

misaka-10032 avatar misaka-10032 commented on July 20, 2024

Got it! Thx :-)

from dragon-book-exercise-answers.

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.