14
Aug

Javascript Comments And Keywords Solution – Programming Languages


For this problem, we’re going to write a lexer that’s able to go through and create tokens for each of the JavaScript keywords and also ignore JavaScript comments, which are specified with little stars for long comments, just a slash and a star–this is a multi-line comment– or just a slash, slash which comments everything for the rest of the line. So we’ve got 2 types of comments to handle. We did this 1 in class, but this one you’re going to have to use some state variable like we did with HTML in class. So let’s see how that’s done. So here I have the solution. The first thing, which we gave you, was the long list of tokens– the names and what they should match. So if I scroll down, I have them all defined. It’s a little tedious, but it’s what you have to do, and it’s only a 1-time thing you have to do it. The only thing you really have to keep in mind is you have to escape characters that are particular to a regular expression. So freezing means you escape the parenthesis. We need to escape the pipes, plus, star– things that have meaning in a regular expression. So the other major part of this problem was handling the comments. We have the end-of-line comment, which we did in lecture, and that’s almost exactly the same. If you see 2 slashes, no matter what follows you simply pass by it. But what we’re going to have to handle, just like we did with HTML, is if we’re in a block comment–a multi-line comment, which is indicated by the slash, a star, blah, blah, blah, blah, and another star and a slash, we assign our states. We have a state called comment, and it’s exclusive when you’re in a comment, and you’re not in anything else. A comment begins with the slash and the star, which is indicated right here. You have to escape both of them since they both have meaning. And then we begin the comment. We’re done when we see the other star and then the slash. We increment our line count based on how many things we’ve passed by, and then we go back to the initial state. We need to remember an error for when something goes wrong, and then down here, I’ve defined what we want to ignore for the comment state, and it’s the same exact stuff that we ignore when we’re tokenizing the rest of it.

Tags: , , , , , , , ,

There are no comments yet

Why not be the first

Leave a Reply

Your email address will not be published. Required fields are marked *