parsingcompiler-constructionbisonyacc

How can I make a yacc rule that recognizes a comment in C?


I'm trying to make a rule in yacc that recognizes comments. For this, I have defined one token for the comments of only 1 line an another one for the multi line ones; in the lex file:

comentario              [//]([^"])* [\n]
comentarioMulti         [/*]([^"])*[*/]

And also a rule for it in the yacc file :

comentario:                     COMENTARIO
                                COMENTARIMULTILINEA
                                ;

But it gives me this error:

 syntax error en la línea 26
 COMENTARIO MacBook-Air-de-administrador:Sintactico administrador$ 

I have also tried by putting the \n without the [] and some other options, but I get the same error every time.


Solution

  • When a regex doesn't work as expected, I recommend to test it piece by piece to see if they do what is expected. For example, if you'd checked what [//] does, you would find out that it matches / instead of //. (There are also regex visualizers online that can help.)

    Let's go over problems with your code: