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

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_arc_tangent_combo(p)[source]

arc_tangent_combo : ARC_TANGENT expression DIVIDED_BY expression

p_binary_operation(p)[source]

binary_operation : binary_operation1 | binary_operation2 | binary_operation3

p_binary_operation1(p)[source]

binary_operation1 : POWER

p_binary_operation2(p)[source]

binary_operation2 : DIVIDED_BY | MODULO | TIMES

p_binary_operation3(p)[source]

binary_operation3 : AND | EXCLUSIVE_OR | MINUS | NON_EXCLUSIVE_OR | PLUS

p_comment(p)[source]

comment : ordinary_comment

p_error(p)[source]
p_expression(p)[source]

expression : LEFT_BRACKET inner_expression RIGHT_BRACKET

p_inner_expression(p)[source]

inner_expression : real_value | inner_expression binary_operation real_value

p_line(p)[source]

line : DIVIDED_BY line_right | line_right

p_line_content(p)[source]

line_content : segments

p_line_number(p)[source]

line_number : N POSITIVE_INTEGER | N POSITIVE_REAL

p_line_right(p)[source]

line_right : line_content | line_content EOF_COMMENT

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_mid_line_word(p)[source]

mid_line_word : mid_line_letter real_value

p_numbered_line(p)[source]

line_content : line_number segments

p_ordinary_comment(p)[source]

ordinary_comment : INLINE_COMMENT

p_ordinary_unary_combo(p)[source]

ordinary_unary_combo : ordinary_unary_operation expression

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_index(p)[source]

parameter_index : real_value

p_parameter_setting(p)[source]

parameter_setting : PARAMETER_SIGN parameter_index EQUAL_SIGN real_value

p_parameter_value(p)[source]

parameter_value : PARAMETER_SIGN parameter_index

p_real_value(p)[source]

real_value : POSITIVE_INTEGER | POSITIVE_REAL | REAL | expression | parameter_value | unary_combo

p_segment(p)[source]

segment : mid_line_word | comment | parameter_setting

p_segments(p)[source]

segments : segment | segments segment

p_unary_combo(p)[source]

unary_combo : ordinary_unary_combo | arc_tangent_combo