<p>It looks like comments inside parentheses are not allowed in numerous parsers (ie. Marlin). This seems to be true for most of the 3D printers. Classical CNC milling machines use parentheses without problems.</p>
<p>It should work on Prusa printers as stated in their <a href="https://reprap.org/wiki/G-code#Comments" rel="nofollow noreferrer">Wiki</a>. Unfortunately there are no words about nesting of the comments.</p>
<p>I have however found a comment on cnczone.com forums regarding the nesting of comments within parentheses.</p>
<blockquote>
<p>Printable characters and white space inside parentheses is a comment. A left parenthesis always starts a comment. The comment ends at the first right parenthesis found thereafter.</p>
<p>Once a left parenthesis is placed on a line, a matching right parenthesis must appear before the end of the line.</p>
<p>Comments may not be nested; it is an error if a left parenthesis is found after the start of a comment and before the end of the comment.</p>
<p>Here is an example of a line containing a comment: <code>G80 M5 (stop motion)</code></p>
</blockquote>
<p>Source: <a href="https://www.cnczone.com/forums/g-code-programing/103434-coments-g-code.html#post763510" rel="nofollow noreferrer">cnczone.com</a></p>

CC BY-SA 4.0
2018-07-16T20:13:26.167Z
2018-07-16T20:13:26.167Z
2020-06-18T08:29:41.667Z
Jan Vorisek
<p>This is not universally valid G-code, and how it is handled depends on the implementation. You can use this style of comment on <em>some</em> machines, but not all.</p>

<p>The way parsing used to be implemented in Marlin (a very common 3D printer firmware), it would work fine unless the comment string included a X, Y, Z, E or F character. The parser simply looks for the first occurrence of X/Y/Z/E/F and then tries to parse the bit of text appearing after that character into a number. If the string cannot be parsed as a number, it defaults to 0 instead. For example,</p>

<pre><code> G0 (Some comment containing the character Y) Y10 Z-5
</code></pre>

<p>would be interpreted as <code>G0 Y0 Z-5</code> and not as <code>G0 Y10 Z-5</code>, because ") " (the string appearing after the first occurrence of "Y") does not parse to any valid number. Your example happens to work fine because the comment string doesn't contain any special characters.</p>

<p>Marlin does support end-of-line comments, which should start with a semicolon and continue until the end of the line.</p>

<p>This is how it used to work in older Marlin versions. Newer Marlin versions have a more advanced parser, but it still would not play well with these parentheses-style comments. It is best to avoid them, as compatibility is not guaranteed.</p>

CC BY-SA 4.0
2018-07-16T19:33:28.427Z
This sounds even more weird to me. There are no comments about this behaviour for example in RepRap https://reprap.org/wiki/G-code#Comments
Jan Vorisek
CC BY-SA 4.0
2018-07-16T19:57:58.417Z
@JanVorisek I just noticed it is slightly different in newer versions of Marlin. Still, parentheses-style comments aren't supported.
Tom van der Zanden
CC BY-SA 4.0
2018-07-16T20:02:04.783Z
You are right. Just checked few parsers including Marlin. I have came to conclusion that most of the 3D printer firmwares parse only semicolons as comments. Looks like parentheses are/were? prefered way in CNC Milling machines. I'll stick to the semicolon as I care about 3D printing mostly. Thanks for your time anyways :-)
Jan Vorisek
CC BY-SA 4.0
2018-07-16T19:26:10.193Z
2018-07-16T19:55:08.223Z
2018-07-16T19:55:08.223Z
Tom van der Zanden
<p>I am wondering if this piece of G-code is valid:</p>

<pre><code>G0 (Some comment (Its G0 command)) Y10 Z-5
</code></pre>

<p>I have tested this on my Chinese CNC machine and it strips out the comment and works flawlessly.</p>

<p>The machine processes this as <code>G0Y10Z-5</code> which seems like the correct approach to me.</p>

<p>I have however never seen such comment in real CNC practise.
It would be nice if anyone is able to test it out on their CNC/3D Printer.</p>

<p>Many G-code simulators on the internet fail to process such a line in their parser so it makes me confused. I haven't found anything about it on RepRap Wiki or even Google.</p>

CC BY-SA 4.0
2018-07-17T15:25:20.127Z
Why not just use  start-of-line " ; "  and avoid all risk?
Carl Witthoft
CC BY-SA 4.0
2018-07-17T15:26:46.233Z
For a nominal fee :-) ,  I'll write a regex that pulls all  "( * )"  out and replaces with a new line  " ; * "     Let the  sed vs awk flame wars begin.
Carl Witthoft
CC BY-SA 4.0
2018-07-17T16:59:22.810Z
You are right Carl. I was just caring for thirdparty code that I might need to handle someday. I personally use only ; comments. :-)
Jan Vorisek
<g-code><cnc>
Are parentheses allowed within a G-code comment?
Jan Vorisek
# Are parentheses allowed within a G-code comment?

(Asked by: username_0 on 2018-07-16)

I am wondering if this piece of G-code is valid:
G0 (Some comment (Its G0 command)) Y10 Z-5

I have tested this on my Chinese CNC machine and it strips out the comment and works flawlessly.
The machine processes this as G0Y10Z-5 which seems like the correct approach to me.
I have however never seen such comment in real CNC practise.
It would be nice if anyone is able to test it out on their CNC/3D Printer.
Many G-code simulators on the internet fail to process such a line in their parser so it makes me confused. I haven't found anything about it on RepRap Wiki or even Google.

**Tags:** g-code, cnc
> **Comments:**
> **username_1**: Why not just use  start-of-line " ; "  and avoid all risk?  
> **username_1**: For a nominal fee :-) ,  I'll write a regex that pulls all  "( * )"  out and replaces with a new line  " ; * "     Let the  sed vs awk flame wars begin.  
> **username_0**: You are right Carl. I was just caring for thirdparty code that I might need to handle someday. I personally use only ; comments. :-)  

---

## 2 answers

### Answer by username_0 on 2018-07-16. Score: 4 | Accepted answer

It looks like comments inside parentheses are not allowed in numerous parsers (ie. Marlin). This seems to be true for most of the 3D printers. Classical CNC milling machines use parentheses without problems.

It should work on Prusa printers as stated in their [Wiki](https://reprap.org/wiki/G-code#Comments). Unfortunately there are no words about nesting of the comments.

I have however found a comment on cnczone.com forums regarding the nesting of comments within parentheses.

> Printable characters and white space inside parentheses is a comment. A left parenthesis always starts a comment. The comment ends at the first right parenthesis found thereafter.
>
> Once a left parenthesis is placed on a line, a matching right parenthesis must appear before the end of the line.
>
> Comments may not be nested; it is an error if a left parenthesis is found after the start of a comment and before the end of the comment.
>
> Here is an example of a line containing a comment: `G80 M5 (stop motion)`

Source: [cnczone.com](https://www.cnczone.com/forums/g-code-programing/103434-coments-g-code.html#post763510)


---

### Answer by username_2 on 2018-07-16. Score: 12

This is not universally valid G-code, and how it is handled depends on the implementation. You can use this style of comment on some machines, but not all.
The way parsing used to be implemented in Marlin (a very common 3D printer firmware), it would work fine unless the comment string included a X, Y, Z, E or F character. The parser simply looks for the first occurrence of X/Y/Z/E/F and then tries to parse the bit of text appearing after that character into a number. If the string cannot be parsed as a number, it defaults to 0 instead. For example,
 G0 (Some comment containing the character Y) Y10 Z-5

would be interpreted as G0 Y0 Z-5 and not as G0 Y10 Z-5, because ") " (the string appearing after the first occurrence of "Y") does not parse to any valid number. Your example happens to work fine because the comment string doesn't contain any special characters.
Marlin does support end-of-line comments, which should start with a semicolon and continue until the end of the line.
This is how it used to work in older Marlin versions. Newer Marlin versions have a more advanced parser, but it still would not play well with these parentheses-style comments. It is best to avoid them, as compatibility is not guaranteed.

> **Comments:**
> **username_0**: This sounds even more weird to me. There are no comments about this behaviour for example in RepRap https://reprap.org/wiki/G-code#Comments  
> **username_2**: @JanVorisek I just noticed it is slightly different in newer versions of Marlin. Still, parentheses-style comments aren't supported.  
> **username_0**: You are right. Just checked few parsers including Marlin. I have came to conclusion that most of the 3D printer firmwares parse only semicolons as comments. Looks like parentheses are/were? prefered way in CNC Milling machines. I'll stick to the semicolon as I care about 3D printing mostly. Thanks for your time anyways :-)  

---

