Macro and Argument Substitution Via Macro

Hi Forum ,

I was going through LRM-2017 , Section 22.5.1 ::


//  CODE I

 `define HI Hello
 `define LO `"`HI, world`"
 
 `define H(x) `"Hello, `x `"
 
 initial begin

   $display(`"`HI, world`");
   $display(`LO);
   $display(`H(world));
   $display(`H(HI));

 end


[Q1] Am I correct to expect the Output as follow ::

 **Hello, world
 Hello, world
 Hello, `world 
 Hello, Hello**

[Q2] For the 3rd line , should it be an error ( Since world isn't actually a Macro** ) OR Should it Simply display it as a string i.e **world

  I then have CODE II which is copy from LRM

   //  CODE II
`ifdef LRM
  `define msg(x,y) `"x: `\`"y`\`" `"
`else
  `define msg(x,y) `"x: \\\"y\\\" `"
`endif
 
 initial begin

   $display(`"ABD"); // [1] Why Error , If I start with `" Can't I end it with " ??
   $display(`"\"`");
   $display(`msg(left side,right side));

 end

    

[Q3] I see a Compilation error with 1st $display(). If I begin using " , is it Compulsory to end it with " , can’t I end it with " ?

[Q4] Is there a difference between ::


     (a) $display(`"\"`"); 
     (b) $display(`"`\`"`"); 
    
 Won't both Give Same Output ( i.e Quotation Mark ) :: **"**   

[Q5] Similarly is there a difference between ::


      (a)  `define msg(x,y) `"x: `\`"y`\`" `"
      (b)  `define msg(x,y) `"x: \\\"y\\\" `"
     

Thanks .

In reply to TC_2017:

Your first problem is the characters
**`` `" `**
only have meeting within the body of a `define macro. Your first $display should be a syntax error, and explains a lot of your other errors.

If within your define macro you start a quote with **"**, you can end it with either "** or **"**, but it seems more balenced to use **".

In reply to dave_59:

Hi Dave ,

So " Should be used within body of define macro rather than $display() .

Any thoughts on [Q2] ?

Should it be treated as argument substitution ( As world macro i.e Compilation Error ) OR Simply as an argument ( world )

In reply to TC_2017:

Let me just say that LRM is plagued with lots of unresolved issues surrounding macros. But the current LRM is clear in that `" is only defined for use within the body of a text macro. If it were allowed, there would have to be some mention of it the BNF and section 5.9 String literals in the 1800-2017. Some tools do allow it with some different functionality, so you’ll have to ask each tool vendor the semantics how their enhancements have been implemented.