简体   繁体   English

Antlr语法错误:预期WS的输入'p'不匹配

[英]Antlr Syntax error: mismatched input 'p' expecting WS

I have the following grammar: 我有以下语法:

grammar myProject;

program: WS EOF myRules;
WS: [ \t\r\n]+ -> skip;
myRules: myRule+;
myRule: SELECTOR OPEN declarations CLOSE;
declarations: declaration+;
declaration: PROPERTY EQ value ENDSYMBOL;
value: INT | STRING | COLOR;

SELECTOR : (('#'CHAR+)|('.'CHAR+)|CHAR+);
PROPERTY : [A-z-]+;

STRING : '"' .*? '"';
INT : [0-9]+ ;
COLOR : '#' [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F];
CHAR: [A-z];

EQ : ':' ;
OPEN : '{';
CLOSE : '}';
ENDSYMBOL : ';' ;

Now my input is this: 现在我的输入是这样的:

p {
    color: #054593;
    width: 100px;
}

Now, when i parse this i get the following error: 现在,当我解析此错误时,出现以下错误:

Syntax error: mismatched input 'p' expecting WS 语法错误:预期WS的输入'p'不匹配

I have red many questions here on stack and googled a lot already, but I simpeley can't find a anwser. 我在这里有很多红色问题,已经在Google上搜索了很多,但辛普利(Simpeley)找不到答案。 What am I doing wrong in my grammar? 我的语法做错了什么? Why does the program needs a WS and how do I fix this. 该程序为什么需要WS,以及如何解决此问题。 Many, many thanks in advance! 非常感谢!

The very first thing you do in your grammar is demand WS: 语法中要做的第一件事是需求WS:

program: WS EOF myRules;

So, lacking any whitespace characters, your parse fails. 因此,缺少任何空白字符,您的解析就会失败。 I'd simply suggest: 我只是建议:

program: myRule*;

since you're discarding whitespace already with the skip option. 因为您已经使用skip选项丢弃了空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM