Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c-like comment in lex

Posted on 2009-04-03
4
Medium Priority
?
772 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 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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 while-loops in the C programming language.

721 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