Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I know next to no VHDL, so let's say you want to replace all single line comments in a (Java) source file with multi-line comments:</p> <pre><code>//foo </code></pre> <p>should become:</p> <pre><code>/* foo */ </code></pre> <p>You need to let the lexer match single line comments, of course. But you should also make sure it recognizes multi-line comments because you don't want <code>//bar</code> to be recognized as a single line comment in:</p> <pre><code>/* //bar */ </code></pre> <p>The same goes for string literals:</p> <pre><code>String s = "no // comment"; </code></pre> <p>Finally, you should create some sort of catch-all rule in the lexer that will match any character.</p> <p>A quick demo:</p> <pre><code>grammar T; parse : (t=. {System.out.print($t.text);})* EOF ; Str : '"' ('\\' . | ~('\\' | '"'))* '"' ; MLComment : '/*' .* '*/' ; SLComment : '//' ~('\r' | '\n')* { setText("/* " + getText().substring(2) + " */"); } ; Any : . // fall through rule, matches any character ; </code></pre> <p>If you now parse input like this:</p> <pre><code>//comment 1 class Foo { //comment 2 /* * not // a comment */ String s = "not // a // comment"; //comment 3 } </code></pre> <p>the following will be printed to your console:</p> <pre><code>/* comment 1 */ class Foo { /* comment 2 */ /* * not // a comment */ String s = "not // a // comment"; /* comment 3 */ } </code></pre> <p>Note that this is just a quick demo: a string literal in Java could contain Unicode escapes, which my demo doesn't support, and my demo also does not handle char-literals (the char literal <code>char c = '"';</code> would break it). All of these things are quite easy to fix, of course.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload