0

I'm having trouble recognising 'end of file' correctly with ANTLR. As soon there's no new line but just 'end of file' after the last character I get

line 37:1 missing NEWLINE at ''

Any ideas on how to deal correctly with end of file?

This is my grammer (for GCode):

grammar GCode;

gcode :
    start_end NEWLINE
    line (NEWLINE line)*
    start_end ( NEWLINE | EOF);

line : (
        comment |
        command WHITESPACE* COMMENT?
);

start_end : PERCENT;
comment : COMMENT;

command : (
    g00 |
    g01 |
    g02 |
    g03 |
    g21 |
    m2  |
    m3  |
    m5)  ;

g00 : G '00' WHITESPACE coordinates ;
g01 : G '01' WHITESPACE coordinates (speed)?;
g02 : G '02' WHITESPACE coordinates arc (speed)?;
g03 : G '03' WHITESPACE coordinates arc (speed)?;

g21 : G '21';

m2 : M '2';
m3 : M '3' (WHITESPACE 'S' DIGITS+)?;
m5 : M '5';

coordinates : (x | y | z)+; // jeder nur einmal, min 1, max 3
arc : i j;
speed : f;

f : F FLOAT (WHITESPACE)?;
i : I FLOAT (WHITESPACE)?;
j : J FLOAT (WHITESPACE)?;
x : X FLOAT (WHITESPACE)?;
y : Y FLOAT (WHITESPACE)?;
z : Z FLOAT (WHITESPACE)?;

fragment LOWERCASE  : [a-z] ;
fragment UPPERCASE  : [A-Z] ;
fragment DIGIT : [0-9];

NEWLINE : ('\r'? '\n' | '\r')+ ;
WHITESPACE : (' ' | '\t') ;
SPECIAL : [."':];
COMMENT : '(' .*? ')';
PERCENT : '%';
DIGITS : DIGIT+;
F : 'F';
G : 'G';
I : 'I';
J : 'J';
M : 'M';
X : 'X';
Y : 'Y';
Z : 'Z';
FLOAT: ('+'|'-')? DIGITS '.' DIGITS;

And this is the testfile:

%
(Header)
(Generated by gcodetools from Inkscape.)
(Using default header. To add your own header create file "header" in the output dir.)
M3
(Header end.)
G21 (All units in mm)

(Start cutting path id: path4827)
(Change tool to Cone cutter)

G00 Z5.000000
G00 X85.340320 Y22.949391

G01 Z-1.000000 F100.0(Penetrate)
G02 X82.451429 Y10.393260 Z-1.000000 I-28.731155 J0.000010 F400.000000
G02 X77.835955 Y7.504376 Z-1.000000 I-4.615471 J2.242548
G02 X73.220483 Y10.393262 Z-1.000000 I0.000001 J5.131434
G02 X70.331593 Y22.949391 Z-1.000000 I25.842269 J12.556139
G02 X73.220481 Y35.505528 Z-1.000000 I28.731173 J-0.000000
G02 X77.835955 Y38.394417 Z-1.000000 I4.615476 J-2.242547
G02 X80.361909 Y37.463849 Z-1.000000 I0.000002 J-3.893531
G02 X83.142342 Y33.870675 Z-1.000000 I-7.195292 J-8.440138
G02 X84.717221 Y29.203570 Z-1.000000 I-17.511307 J-8.508321
G02 X85.340320 Y22.949391 Z-1.000000 I-31.075686 J-6.254179
G01 X85.340320 Y22.949391 Z-1.000000
G00 Z5.000000

(End cutting path id: path4827)

(Footer)
M5
G00 X0.0000 Y0.0000
M2
(Using default footer. To add your own footer create file "footer" in the output dir.)
(end)
%
BetaRide
  • 16,207
  • 29
  • 99
  • 177

1 Answers1

2

Your grammar always requires a newline at the end, that's why you get the syntax error. Instead use:

gcode :
    start_end NEWLINE
    line (NEWLINE line)*
    start_end NEWLINE? EOF;

Btw. I find it odd that you handle whitespaces manually, instead of just skipping them. Is this really a requirement of the language?

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181