ANTLR 词法分析器不匹配标记

发布于 2024-08-28 05:14:17 字数 762 浏览 6 评论 0原文

我有一个简单的 ANTLR 语法,我已将其精简为最基本的内容来演示我遇到的这个问题。我正在使用 ANTLRworks 1.3.1。

grammar sample;

assignment  :  IDENT ':=' NUM ';' ;

IDENT       :  ('a'..'z')+ ;

NUM         :  ('0'..'9')+ ;

WS          :  (' '|'\n'|'\t'|'\r')+ {$channel=HIDDEN;} ;

显然,这个语句被语法接受:

x := 99;

但这也是:

x := @!$()()%99***;

ANTLRworks 解释器的输出:

ANTLR 解释器图
(来源:巴里在cs.sierracollege.edu)< /sub>

我做错了什么?甚至 ANTLR 附带的其他示例语法(例如 CMinus 语法)也表现出这种行为。

I have a simple ANTLR grammar, which I have stripped down to its bare essentials to demonstrate this problem I'm having. I am using ANTLRworks 1.3.1.

grammar sample;

assignment  :  IDENT ':=' NUM ';' ;

IDENT       :  ('a'..'z')+ ;

NUM         :  ('0'..'9')+ ;

WS          :  (' '|'\n'|'\t'|'\r')+ {$channel=HIDDEN;} ;

Obviously, this statement is accepted by the grammar:

x := 99;

But this one also is:

x := @!$()()%99***;

Output from the ANTLRworks Interpreter:

ANTLR Interpreter diagram
(source: barry at cs.sierracollege.edu)

What am I doing wrong? Even other sample grammars that come with ANTLR (such as the CMinus grammar) exhibit this behavior.

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

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

发布评论

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

评论(1

吹泡泡o 2024-09-04 05:14:17

如果您查看 ANTLRWorks IDE 的控制台,您会看到很多词法分析器错误。

在命令行上尝试:

grammar Sample;

@members {
  public static void main(String[] args) throws Exception {
    ANTLRStringStream in = new ANTLRStringStream("x := @!$()()\%99***;");
    SampleLexer lexer = new SampleLexer(in);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SampleParser parser = new SampleParser(tokens);
    parser.assignment();
  }
}

assignment  :  IDENT ':=' NUM ';' ;

IDENT       :  ('a'..'z')+ ;

NUM         :  ('0'..'9')+ ;

WS          :  (' '|'\n'|'\t'|'\r')+ {$channel=HIDDEN;} ;

然后:

// generate parser/lexer
java -cp antlr-3.2.jar org.antlr.Tool Sample.g

// compile
javac -cp antlr-3.2.jar *.java

// run Windows
java -cp .;antlr-3.2.jar SampleParser
// or run *nix/MacOS
java -cp .:antlr-3.2.jar SampleParser

将产生:

line 1:5 no viable alternative at character '@'
line 1:6 no viable alternative at character '!'
line 1:7 no viable alternative at character '

line 1:8 no viable alternative at character '('
line 1:9 no viable alternative at character ')'
line 1:10 no viable alternative at character '('
line 1:11 no viable alternative at character ')'
line 1:12 no viable alternative at character '%'
line 1:15 no viable alternative at character '*'
line 1:16 no viable alternative at character '*'
line 1:17 no viable alternative at character '*'

If you look at the console of your ANTLRWorks IDE, you'll see a lot of lexer errors.

Try it on the command line:

grammar Sample;

@members {
  public static void main(String[] args) throws Exception {
    ANTLRStringStream in = new ANTLRStringStream("x := @!$()()\%99***;");
    SampleLexer lexer = new SampleLexer(in);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SampleParser parser = new SampleParser(tokens);
    parser.assignment();
  }
}

assignment  :  IDENT ':=' NUM ';' ;

IDENT       :  ('a'..'z')+ ;

NUM         :  ('0'..'9')+ ;

WS          :  (' '|'\n'|'\t'|'\r')+ {$channel=HIDDEN;} ;

and then:

// generate parser/lexer
java -cp antlr-3.2.jar org.antlr.Tool Sample.g

// compile
javac -cp antlr-3.2.jar *.java

// run Windows
java -cp .;antlr-3.2.jar SampleParser
// or run *nix/MacOS
java -cp .:antlr-3.2.jar SampleParser

will produce:

line 1:5 no viable alternative at character '@'
line 1:6 no viable alternative at character '!'
line 1:7 no viable alternative at character '

line 1:8 no viable alternative at character '('
line 1:9 no viable alternative at character ')'
line 1:10 no viable alternative at character '('
line 1:11 no viable alternative at character ')'
line 1:12 no viable alternative at character '%'
line 1:15 no viable alternative at character '*'
line 1:16 no viable alternative at character '*'
line 1:17 no viable alternative at character '*'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文