Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

c-like comment in lex

Posted on 2009-04-03
4
Medium Priority
?
777 Views
Last Modified: 2012-05-06
Hello,

How can i declare the c-like comments in my lexer?
i.e. single line comment  =  /* comment */

I did that :

\/\* . *\/\  line++;

but i get :

error: syntax error before . token
error: stray \ in program
error: stray \ in program

Thanks in advance for any help !
0
Comment
Question by:unknown_
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 24057854
The errors you get is because of extra \'s you have. The \ is used as an escape character, but there has to be something to escape after it. Notice that the error message says exactly that !

Note also that C-style comments are not single-line comments - they can span multiple lines !

C-style comments are usually handled a bit differently. See for example how they did it in this lex specification for ANSI C :

        http://www.lysator.liu.se/c/ANSI-C-grammar-l.html
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24061065
FYI I have disabled multi-line comments in my own compilers just for simplicity. I stick with the // and #

But its not too difficult, it helps if you make use of lexer states. If I were you (unknown_) I would stick with single line comments and move on to properly finishing your compiler project. I'm just saying this because I and Infinity have an idea of where you are due to helping you with it in several areas (which we are happy to continue with), and I feel like you may be distracting yourself from the importance of building a proper grammar and code generator.

You can always add C comments later.

That said, its up to you, so if you don't agree, Infinity's link should help.

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24062164
Good point, mrjoltcola.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question