Kaz Rietveld
Kaz Rietveld

Reputation: 11

SableCC expecting EOF

I seem to be having issues with SableCC generating lexing.grammar

This is what i run on sableCC

Package lexing ; // A Java package is produced for the
// generated scanner
Helpers
num = ['0'..'9']+; // A num is 1 or more decimal digits
letter = ['a'..'z'] | ['A'..'Z'] ;
// A letter is a single upper or
// lowercase character.
Tokens
number = num; // A number token is a whole number
ident = letter (letter | num)* ;
// An ident token is a letter followed by
// 0 or more letters and numbers.
arith_op = [ ['+' + '-' ] + ['*' + '/' ] ] ;
// Arithmetic operators
rel_op = ['<' + '>'] | '==' | '<=' | '>=' | '!=' ;
// Relational operators
paren = ['(' + ')']; // Parentheses
blank = (' ' | '\t' | 10 | '\n')+ ; // White space
unknown = [0..0xffff] ;
// Any single character which is not part
// of one of the above tokens.

This is the result

org.sablecc.sablecc.parser.ParserException: [21,1] expecting: EOF
        at org.sablecc.sablecc.parser.Parser.parse(Parser.java:1792)
        at org.sablecc.sablecc.SableCC.processGrammar(SableCC.java:203)
        at org.sablecc.sablecc.SableCC.processGrammar(SableCC.java:171)
        at org.sablecc.sablecc.SableCC.main(SableCC.java:137) 

Upvotes: 1

Views: 200

Answers (1)

testing_22
testing_22

Reputation: 2585

You can only have a short_comment if you put one empty line after it. If you use long_comments instead (/* ... */), there's no need for it.

The reason is that, according to the grammar that defines the SableCC 2.x input language, a short comment is defined as a consuming pattern of eol:

cr = 13;
lf = 10;
eol = cr lf | cr | lf;        // This takes care of different platforms
 
short_comment = '//' not_cr_lf* eol;

Since the last line has:

// of one of the above tokens.

It consumes the last (invisible) EOF token expected at the end of any .sable file, explaining the error.

Upvotes: 0

Related Questions