Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lexer/parser ambiguity

Tags:

lexer

How does a lexer solve this ambiguity?

/*/*/

How is it that it doesn't just say, oh yeah, that's the begining of a multi-line comment, followed by another multi-line comment.

Wouldn't a greedy lexer just return the following tokens?

  • /*
  • /*
  • /

I'm in the midst of writing a shift-reduce parser for CSS and yet this simple comment thing is in my way. You can read this question if you wan't some more background information.

UPDATE

Sorry for leaving this out in the first place. I'm planning to add extensions to the CSS language in this form /* @ func ( args, ... ) */ but I don't want to confuse an editor which understands CSS but not this extension comment of mine. That's why the lexer just can't ignore comments.

like image 344
John Leidegren Avatar asked Apr 13 '10 23:04

John Leidegren


1 Answers

One way to do it is for the lexer to enter a different internal state on encountering the first /*. For example, flex calls these "start conditions" (matching C-style comments is one of the examples on that page).

like image 189
Matthew Slattery Avatar answered Sep 27 '22 18:09

Matthew Slattery