Solved

c-like comment in lex

Posted on 2009-04-03
4
754 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Desktop Application 3 54
Need a simple RegEx to search for two words 27 45
Name Space error VS2015 1 25
Code works but breaks when I add one section 4 20
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.
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.

829 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