Parser State#

Before you can understand and use G-code commands you have to understand the concept of parser state.

The parser state is a table that remembers the last used command parameters so future commands can use them by default. These parameters are the unit of measurement (millimeters or inches), relative or absolute positions, movement speed (AKA “feed rate”), and more.

 Deep Dive: Parser state and movement commands

Every movement command contains coordinate values like X, Y or Z. These values can be interpreted differently depending on the current parser state. It stores things like:

  • Units of measurement - millimeters or inches. G21 means millimeters and G20 means inches
  • Relative to the current location or absolute. G91 means relative and G90 means absolute
  • How fast to move - so-called “feed rate” F
  • How fast the spindle rotates - so-called “speed rate” S
  • Current move mode - G0 for rapid move, G1 for feed move, etc

You can include all that information in every movement command

G1 G21 G91 X10 F2000

It means “move the X axis 10mm from the current position at rate 2000mm/min”.

Or you can include only partial information

G1 X10

Or even go as short as

X10

The rest of the parameters are taken from the current parser state. Any parameters you use in your last command will be stored in the state and become the defaults for the next command.

For example

G1 G90 G21 X10 F500 - moves X to absolute position 10 at speed 500
G1 X20 - moves X to position 20. assumes G90, G21 and F500 from before
Y50 - moves Y to position 50. assumes G1, G90, G21 and F500 from before

You can view the current state with the $G command. It will print something like

[GC:G0 G54 G17 G21 G90 G94 M5 M9 T0 F500 S0]

Note: When you are writing individual commands on the console, it’s best to provide as many parameters as you can to avoid surprises in case the current state is not what you need. Otherwise the machine can move in unexpected and dangerous ways.

When you are writing a longer list of commands, like a multi-line macro or a full G-code program, then you can set the state at the start and from then on use shorter commands.

Modifying the parser state#

These G-codes change the parser state. They can be used individually or as part of movement commands. In either case, the parser state will be updated with the new settings, which will become the defaults for future commands.

Units mode (G20, G21)#

G20 selects the current units to be inches, and G21 selects millimeters. The unit determines how to interpret distances, absolute positions and feed rates.

G1 G20 G91 X10 F10 - move X 10 inches to the right from the current position at speed 10in/min
G1 G21 G90 X10 F10 - move X to position 10mm in the work coordinate system at speed 10mm/min

Absolute or relative (G90, G91)#

G90 is for absolute moves and G91 is for relative moves from the current position.

G1 G20 G91 X10 - move X 10 inches to the right from the current position
G1 G21 G90 X10 - move X to position 10mm in the work coordinate system

Feeds and speeds (F, S)#

F sets the feed rate for movement commands. The units depend on the current unit state.

F1000 - selects feed rate of 1000mm/min (assuming the current unit is millimeters)
G20 F100 - selects feed rate of 100in/min

S works similarly for selecting the spindle speed in revolutions per minute. It is rarely used on its own. Most often it is part of the M3 command that turns on the spindle.

S12000 - sets the speed to 12000 RPM
M3 - turns on the spindle using the current rate
M3 S6000 - turns on the spindle at 6000 RPM

Note: If the spindle is not running, the S command will not turn it on.
If the spindle is already running, the S command will change the speed.

Discussion#

Comment Form is loading comments...

  © 2026 Ivo Beltchev cnc@ibeltchev.com