Solved

c-like comment in lex

Posted on 2009-04-03
4
767 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to useā€¦
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

623 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