2012-08-02 12:25:38 (CET)

Format of a line

Format of a Line

A permissible line of input RS274/NGC code consists of the following, in order, with the restriction that there is a maximum (currently 256) to the number of characters allowed on a line.

  • An optional line number.

  • Any number of words, parameter settings, and comments.

Any input not explicitly allowed is illegal and will cause the Interpreter to signal an error.

Spaces and tabs are allowed anywhere on a line of code and do not change the meaning of the line, except inside comments. This makes some strange-looking input legal. The line "g0x +0. 12 34y 7" is equivalent to "g0 x+0.1234 y7", for example.

Blank lines are allowed in the input. They are to be ignored.

Input is case insensitive.


Line Number

A line number is the letter N followed by an integer (with no sign) between 0 and 99999 written with no more than five digits (000009 is not OK, for example). Line numbers may be repeated or used out of order, although normal practice is to avoid such usage.

Line numbers may also be skipped, and that is normal practice. A line number is not required to be used, but must be in the proper place if used.


A word is a letter other than N followed by a real value.

Words may begin with any of the letters shown in Table 3-2. The table includes N for completeness, even though, as defined above, line numbers are not words. Several letters (I, J, K, L, P, and R) may have different meanings in different contexts.

letter meaning
D Tool radius compensation number
F Feed rate
G General function (see Table 3-4)
H Tool length offset index
I X-axis offset for arcs X offset in G87 canned cycle
J Y-axis offset for arcs Y offset in G87 canned cycle
K Z-axis offset for arcs Z offset in G87 canned cycle
L number of repetitions in canned cycles key used with G10
M miscellaneous function (see Table 3-6)
N line number
P dwell time in canned cycles dwell time with G4 key used with G10
Q feed increment in G83 canned cycle
R arc radius, clear_z distance in canned cycle
S spindle speed
T tool selection
X X-axis of machine

Y-axis of machine


Z-axis of machine


A-axis of machine


A-axis of machine


C-axis of machine



A real value is some collection of characters that can be processed to come up with a number. A real value may be an explicit number (such as 341 or -0.8807), a parameter value, an expression, or a unary operation value. Definitions of these follow immediately. Processing characters to come up with a number is called "evaluating". An explicit number evaluates to itself.






The following rules are used for (explicit) numbers. In these rules a digit is a single character between 0 and 9.

  • A number consists of (1) an optional plus or minus sign, followed by (2) zero to many digits, followed, possibly, by (3) one decimal point, followed by (4) zero to many digits - provided that there is at least one digit somewhere in the number.

  • There are two kinds of numbers: integers and decimals. An integer does not have a decimal point in it; a decimal does.

  • Numbers may have any number of digits, subject to the limitation on line length. Only about seventeen significant figures will be retained, however (enough for all known applications).

  • A non-zero number with no sign as the first character is assumed to be positive.


Notice that initial (before the decimal point and the first non-zero digit) and trailing (after the decimal point and the last non-zero digit) zeros are allowed but not required.

A number written with initial or trailing zeros will have the same value when it is read as if the extra zeros were not there.


Numbers used for specific purposes in RS274/NGC are often restricted to some finite set of values or some to some range of values. In many uses, decimal numbers must be close to integers; this includes the values of indexes (for parameters and carousel slot numbers, for example), M codes, and G codes multiplied by ten. A decimal number which is supposed be close to an integer is considered close enough if it is within 0.0001 of an integer. 



parameter value


A parameter value is the pound character # followed by a real value. The real value must evaluate to an integer between 1 and 5399. The integer is a parameter number, and the value of the parameter value is whatever number is stored in the numbered parameter.

The # character takes precedence over other operations, so that, for example, "#1+2" means the number found by adding 2 to the value of parameter 1, not the value found in parameter 3. Of course, #[1+2] does mean the value found in parameter 3. The # character may be repeated; for example ##2 means the value of the parameter whose index is the (integer) value of parameter 2.



Expressions and Binary Operations  

   An expression is a set of characters starting with a left bracket [ and ending with a balancing right bracket ]. In between the brackets are numbers, parameter values, mathematical operations, and other expressions. An expression may be evaluated to produce a number. The expressions on a line are evaluated when the line is read, before

anything on the line is executed. An example of an expression is [ 1 + acos[0] - [#3 ** [4.0/2]]].


Binary operations appear only inside expressions. Nine binary operations are defined. There are four basic mathematical operations: addition (+), subtraction (-), multiplication (*), and division (/). There are three logical operations: non-exclusive or (OR), exclusive or (XOR), and logical and (AND). The eighth operation is the modulus operation (MOD). The ninth operation is the "power" operation (**) of raising the number on the left of the operation to the power on the right.


The binary operations are divided into three groups. The first group is: power. The second group is: multiplication, division, and modulus. The third group is: addition, subtraction, logical non-exclusive or, logical exclusive or, and logical and. If operations are strung together (for example in the expression [2.0 / 3 * 1.5 - 5.5 / 11.0]), operations in the first group are to be performed before operations in the second group and operations in the second group before operations in the third group. If an expression contains more than one operation from the same group (such as the first / and * in the example), the operation on the left is performed first. Thus, the example is equivalent to: [((2.0 / 3) * 1.5) - (5.5 / 11.0)] , which simplifies to [1.0 - 0.5] , which is 0.5.


The logical operations and modulus are to be performed on any real numbers, not just on integers. The number zero is equivalent to logical false, and any non-zero number is equivalent to logical true. 


Unary Operation Value  


A unary operation value is either "ATAN" followed by one expression divided by another expression (for example "ATAN[2]/[1+3]") or any other unary operation name followed by an expression (for example "SIN[90]"). The unary operations are: ABS (absolute value), ACOS (arc cosine), ASIN (arc sine), ATAN (arc tangent), COS (cosine), EXP (e raised to the given power), FIX (round down), FUP (round up), LN (natural logarithm), ROUND (round to the nearest whole number), SIN (sine), SQRT (square root), and TAN (tangent). Arguments to unary operations which take angle measures (COS, SIN, and TAN) are in degrees. Values returned by unary operations which return angle measures (ACOS, ASIN, and ATAN) are also in degrees.


The FIX operation rounds towards the left (less positive or more negative) on a number line, so that FIX[2.8] =2 and FIX[-2.8] = -3, for example. The FUP operation rounds towards the right (more positive or less negative) on a number line; FUP[2.8] = 3 and FUP[-2.8] = -2, for example. 



parameter setting


A parameter setting is the following four items one after the other: (1) a pound character # , (2) a real value which evaluates to an integer between 1 and 5399, (3) an equal sign

= , and (4) a real value. For example "#3 = 15" is a parameter setting meaning "set parameter 3 to 15."


A parameter setting does not take effect until after all parameter values on the same line have been found. For example, if parameter 3 has been previously set to 15 and the line "#3=6 G1 x#3" is interpreted, a straight move to a point where x equals 15 will occur and the value of parameter 3 will be 6. 



Comments and Messages

   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’s an example of a line containing a comment: "G80 M5 (stop motion)". Comments do not cause a machining center to do anything.


A comment contains a message if "MSG," appears after the left parenthesis and before any other printing characters. Variants of "MSG," which include white space and lower case characters are allowed. The rest of the characters before the right parenthesis are considered to be a message. Messages should be displayed on the message display device. Comments not containing messages need not be displayed there. 



item repeats


A line may have any number of G words, but two G words from the same modal group (see Section 3.4) may not appear on the same line.


A line may have zero to four M words. Two M words from the same modal group may

not appear on the same line.


For all other legal letters, a line may have only one word beginning with that letter.


If a parameter setting of the same parameter is repeated on a line, "#3=15 #3=6", for example, only the last setting will take effect. It is silly, but not illegal, to set the same parameter twice on the same line.


If more than one comment appears on a line, only the last one will be used; each of the other comments will be read and its format will be checked, but it will be ignored thereafter. It is expected that putting more than one comment on a line will be very rare. 



Item order  


The three types of item whose order may vary on a line (as given at the beginning of this

section) are word, parameter setting, and comment. Imagine that these three types of item are divided into three groups by type.


The first group (the words) may be reordered in any way without changing the meaning of the line.


If the second group (the parameter settings) is reordered, there will be no change in the meaning of the line unless the same parameter is set more than once. In this case, only the last setting of the parameter will take effect. For example, after the line "#3=15 #3=6" has been interpreted, the value of parameter 3 will be 6. If the order is reversed to "#3=6 #3=15" and the line is interpreted, the value of parameter 3 will be 15.


If the third group (the comments) contains more than one comment and is reordered, only the last comment will be used.


If each group is kept in order or reordered without changing the meaning of the line, then the three groups may be interleaved in any way without changing the meaning of the line. For example, the line "g40 g1 #3=15 (foo) #4=-7.0" has five items and means exactly the same thing in any of the 120 possible orders (such as "#4=-7.0 g1 #3=15 g40 (foo)") for the five items.



Commands and Machine Modes


In RS274/NGC, many commands cause a machining center to change from one mode to another, and the mode stays active until some other command changes it implicitly or explicitly. Such commands are called "modal". For example, if coolant is turned on, it stays on until it is explicitly turned off. The G codes for motion are also modal. If a G1 (straight move) command is given on one line, for example, it will be executed again on

the next line if one or more axis words are available on the line, unless an explicit command is given on that next line using the axis words or cancelling motion.


"Non-modal" codes have effect only on the lines on which they occur. For example, G4 (dwell) is non-modal.