Sablecc期望EOF

发布于 2025-01-24 14:39:53 字数 1203 浏览 3 评论 0原文

我似乎在sablecc生成lexing.grammar

这是我在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.

这就是结果

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) 

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) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋竹姑娘 2025-01-31 14:39:54

如果将一条空线放在后,则只能拥有short_comment。如果您使用 long_comments 而不是(/ * ... */),则无需它。

原因是,根据 grammar

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

//上述令牌之一。

它消耗了最后(不可见的)
.sable文件,解释了错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文