GPR is a small, MIT license, C++11 G-code parser. It is intended to provide the lowest level of parsing for G-code, breaking up raw G-code text into blocks consisting of comments and word-address style commands.
What is G-code?
G-code is the universal language of machine tools. It is used to control everything from mills and lathes to EDM machines and 3D printers.
Though the syntax and semantics of g-code are officially defined by ISO 6983 it is not really a single language. Different machine vendors implement different subsets of the standard and different non-standard extensions for their own equipment.
What everybody agrees on is that G-code programs consist of blocks, which are really just lines of text. Each block consists of:
- An optional block delete character '/'
- An optional line number
- Any number of words, parameter settings and comments
- An end of line marker (carriage return)
G-code emitted by modern CAM systems usually consists mostly of comments and words. For example the G-code program below has 4 blocks: 1 comment, and three movement commands.
(*** Toolpath 1 ***) G0 X0.0 Y0.0 Z0.0 G1 X1.0 F23.0 G1 Z-1.0 F10.0
Since the interpretation of G-code commands varies from vendor to vendor this parser does not make any attempt to interpret G-code commands. It simply parses G-code and returns the parsed blocks.
G-code Dialects Supported
This parser has been tested on 5 different G-code samples which are in the gcode_samples folder
- RepRap: A common G-code dialect used by 3D printers like the monoprice select mini, the sample was produced by Cura.
- LinuxCNC: The sample in the github repo was produced for a 3 axis mill by an experimental CAM system from Stanford.
- Vectric: Generated by VCarve Pro for a CAMASTER router
- HAAS: Taken from a machinists forum post.
- Mazak: Produced by Mazak's Mazatrol to G-code converter.
cmake . make -j ./all-tests
If you use this parser in your own project I would really like to hear from you about it. Let me know what you liked and what could be better, and if you like this parser please give the repo a star!