Comments (6)
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.
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.
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.
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.
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.
Got it! Thx :-)
from dragon-book-exercise-answers.
Related Issues (20)
- good
- Exercise 3.4.1 (3)
- 1.6.1 answer maybe wrong?
- Exercise 6.1.2 (a)
- About Exercise set 2.6
- 编译原理
- Can someone explain me 2.2.1?
- 4.2.1的图片挂掉了
- Help!! A question to answer 2.2 HOT 2
- Exercise 3.7.1 3.7.2 HOT 1
- 4.4.1的第4题答案有问题 HOT 1
- Issue 2.4.md HOT 2
- is 7.2.3-2.gif ok?
- where are the answers of 3.2?
- 练习4.2.1 的第四问没有证明文法为何不具有二义性
- Exercise 2.2.2 d HOT 1
- (6.3.1) I guess values of offset value is not correct at some point
- Exercise 3.3.3, subsequences
- Issue in exercise 3.3.5 (part 4)
- Bug
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dragon-book-exercise-answers.