Solved

c-like comment in lex

Posted on 2009-04-03
4
728 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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Good point, mrjoltcola.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now