9.1.1.5.6. Parser¶
Module to implement a RS-274 G-code parser.
Usage:
parser = GcodeParser()
ast_line = parser.parse(gcode_line)
ast_program = parser.parse_lines(gcode_lines)
Implementation
The parser is generated automatically from the grammar defined in this class using the generator PLY which implement a LALR(1) parser similar to the tools lex and yacc.
The parser construct an abstract syntax tree (AST) during the parsing.
User can subclass this parser to support a derived G-code flavour.
For references, see
- The NIST RS274NGC Interpreter — Version 3 — Appendix E. Production Rules for the RS274/NGC Language
- http://linuxcnc.org/docs/2.7/html/gcode/overview.html
-
exception
PythonicGcodeMachine.Gcode.Rs274.Parser.
GcodeParserError
[source]¶ Bases:
ValueError
-
class
PythonicGcodeMachine.Gcode.Rs274.Parser.
GcodeParser
(machine=None)[source]¶ Bases:
PythonicGcodeMachine.Gcode.Rs274.Parser.GcodeParserMixin
,PythonicGcodeMachine.Gcode.Rs274.Parser.GcodeGrammarMixin
-
class
PythonicGcodeMachine.Gcode.Rs274.Parser.
GcodeParserMixin
(machine=None)[source]¶ Bases:
object
Mixin to implement a RS-274 G-code parser
-
machine
¶
-
parse
(line)[source]¶ Parse a G-code line.
Return a
PythonicGcodeMachine.Gcode.Rs274.Ast.Line
instance.
-
parse_lines
(lines)[source]¶ Parse a G-code lines
Return a
PythonicGcodeMachine.Gcode.Rs274.Ast.Program
instance.
-
-
class
PythonicGcodeMachine.Gcode.Rs274.Parser.
GcodeGrammarMixin
[source]¶ Bases:
object
Mixin to implement the grammar.
Production Language for RS-274
The symbols in the productions are mostly standard syntax notation. Meanings of the symbols are:
=
The symbol on the left of the equal sign is equivalent to the expression on the right+
followed by|
or.
end of production (a production may have several lines)[]
zero or one of the expression inside square brackets may occur{}
zero to many of the expression inside curly braces may occur()
exactly one of the expression inside parentheses must occur
The productions are:
- arc_tangent_combo = arc_tangent + expression + divided_by + expression .
- binary_operation = binary_operation1 | binary_operation2 | binary_operation3 .
- binary_operation1 = power .
- binary_operation2 = divided_by | modulo | times .
- binary_operation3 = and | exclusive_or | minus | non_exclusive_or | plus .
- comment = message | ordinary_comment .
- expression = left_bracket + real_value + { binary_operation + real_value } + right_bracket .
- line = [block_delete] + [line_number] + {segment} + end_of_line .
- line_number = letter_n + digit + [digit] + [digit] + [digit] + [digit] .
- message = left_parenthesis + {white_space} + letter_m + {white_space} + letter_s + {white_space} + letter_g + {white_space} + comma + {comment_character} + right_parenthesis .
- mid_line_letter = letter_a | letter_b | letter_c| letter_d | letter_f | letter_g | letter_h | letter_i | letter_j | letter_k | letter_l | letter_m | letter_p | letter_q | letter_r | letter_s | letter_t | letter_x | letter_y | letter_z .
- mid_line_word = mid_line_letter + real_value .
- ordinary_comment = left_parenthesis + {comment_character} + right_parenthesis .
- ordinary_unary_combo = ordinary_unary_operation + expression .
- ordinary_unary_operation = absolute_value | arc_cosine | arc_sine | cosine | e_raised_to | fix_down | fix_up | natural_log_of | round | sine | square_root | tangent .
- parameter_index = real_value .
- parameter_setting = parameter_sign + parameter_index + equal_sign + real_value .
- parameter_value = parameter_sign + parameter_index .
- real_number = [ plus | minus ] + (( digit + { digit } + [decimal_point] + {digit}) | ( decimal_point + digit + {digit})) .
- real_value = real_number | expression | parameter_value | unary_combo .
- segment = mid_line_word | comment | parameter_setting .
- unary_combo = ordinary_unary_combo | arc_tangent_combo .
-
p_binary_operation
(p)[source]¶ binary_operation : binary_operation1 | binary_operation2 | binary_operation3
-
p_binary_operation3
(p)[source]¶ binary_operation3 : AND | EXCLUSIVE_OR | MINUS | NON_EXCLUSIVE_OR | PLUS
-
p_inner_expression
(p)[source]¶ inner_expression : real_value | inner_expression binary_operation real_value
-
p_mid_line_letter
(p)[source]¶ mid_line_letter : A | B | C | D | F | G | H | I | J | K | L | M | P | Q | R | S | T | X | Y | Z
-
p_ordinary_unary_operation
(p)[source]¶ ordinary_unary_operation : ABSOLUTE_VALUE | ARC_COSINE | ARC_SINE | COSINE | E_RAISED_TO | FIX_DOWN | FIX_UP | NATURAL_LOG_OF | ROUND | SINE | SQUARE_ROOT | TANGENT
-
p_parameter_setting
(p)[source]¶ parameter_setting : PARAMETER_SIGN parameter_index EQUAL_SIGN real_value