# Are parentheses allowed within a G-code comment?

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.

• BTW ... Welcome to 3D Printing! :o) – Pᴀᴜʟsᴛᴇʀ2 Jul 17 '18 at 0:38
• Why not just use start-of-line " ; " and avoid all risk? – Carl Witthoft Jul 17 '18 at 15:25
• 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 Jul 17 '18 at 15:26
• 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 Jul 17 '18 at 16:59

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.

• This sounds even more weird to me. There are no comments about this behaviour for example in RepRap reprap.org/wiki/G-code#Comments – Jan Vorisek Jul 16 '18 at 19:33
• @JanVorisek I just noticed it is slightly different in newer versions of Marlin. Still, parentheses-style comments aren't supported. – Tom van der Zanden Jul 16 '18 at 19:57
• 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 Jul 16 '18 at 20:02

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. 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