ANTLRworks 从语法创建解释器

发布于 2025-01-02 10:16:13 字数 2155 浏览 1 评论 0原文

嘿,我有一个简单的问题。我正在使用 ANTLRworks 根据一组语法创建 Java 解释器。我本来打算用手写出来,但后来意识到我不必这样做,因为 Antlrworks。我收到此错误,但

Tg:9:23: label ID因同名令牌冲突

ANTLRworks是从语法创建解释器时的方法吗?你们都看到我的代码中有任何错误吗?

我正在尝试将 ID 设为 az 中的一个字母,并且不区分大小写。并在每个词位之间留有空格。谢谢

grammar T;

programs : ID WS compound_statement;

statement:       
if_statement|assignment_statement|while_statement|print_statement|compound_statement;

compound_statement: 'begin' statement_list 'end';

statement_list: statement|statement WS statement_list;  

if_statement:    'if' '(' boolean_expression ')' 'then' statement 'else' statement;

while_statement:    'while' boolean_expression 'do' statement;

assignment_statement:   ID = arithmetic_expression;

print_statement:    'print' ID;

boolean_expression: operand relative_op operand;

operand :   ID |INT;

relative_op:    '<'|'<='|'>'|'>='|'=='|'/=';

arithmetic_expression:  operand|operand WS arithmetic_op WS operand;

arithmetic_op:  '+'|'-'|'*'|'/';


ID  :   ('a'..'z'|'A'..'Z'|'_').
;

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

,这是语法

<program> → program id <compound_statement>

<statement> → <if_statement> | <assignment_statement> | <while_statement> |     
<print_statement> | <compound_statement>

<compound_statement> → begin <statement_list> end

<statement_list> → <statement> | <statement> ; <statement_list>

<if_statement> → if <boolean_expression> then <statement> else <statement>

<while_statement> → while <boolean_expression> do <statement>

<assignment_statement> -> id := <arithmetic_expression>

<print_statement> → print id

<boolean_expression> → <operand> <relative_op> <operand>

<operand> → id | constant

<relative_op> → < | <= | > | >= | = | /=

<arithmetic_expression> → <operand> | <operand> <arithmetic_op> <operand>

<arithmetic_op> → + | - | * | / 

Hey I have a quick question. I am using ANTLRworks to create an interpreter in Java from a set of grammar. I was going to write it out by hand but then realized I didn't have to because of antlrworks. I am getting this error though

T.g:9:23: label ID conflicts with token with same name

Is ANTLRworks the way to go when creating a interpreter from grammar. And do y'all see any error in my code?

I am trying to make ID one letter from a-z and not case sensitive. and to have white space in between every lexeme. THANK YOU

grammar T;

programs : ID WS compound_statement;

statement:       
if_statement|assignment_statement|while_statement|print_statement|compound_statement;

compound_statement: 'begin' statement_list 'end';

statement_list: statement|statement WS statement_list;  

if_statement:    'if' '(' boolean_expression ')' 'then' statement 'else' statement;

while_statement:    'while' boolean_expression 'do' statement;

assignment_statement:   ID = arithmetic_expression;

print_statement:    'print' ID;

boolean_expression: operand relative_op operand;

operand :   ID |INT;

relative_op:    '<'|'<='|'>'|'>='|'=='|'/=';

arithmetic_expression:  operand|operand WS arithmetic_op WS operand;

arithmetic_op:  '+'|'-'|'*'|'/';


ID  :   ('a'..'z'|'A'..'Z'|'_').
;

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

and here is the grammar

<program> → program id <compound_statement>

<statement> → <if_statement> | <assignment_statement> | <while_statement> |     
<print_statement> | <compound_statement>

<compound_statement> → begin <statement_list> end

<statement_list> → <statement> | <statement> ; <statement_list>

<if_statement> → if <boolean_expression> then <statement> else <statement>

<while_statement> → while <boolean_expression> do <statement>

<assignment_statement> -> id := <arithmetic_expression>

<print_statement> → print id

<boolean_expression> → <operand> <relative_op> <operand>

<operand> → id | constant

<relative_op> → < | <= | > | >= | = | /=

<arithmetic_expression> → <operand> | <operand> <arithmetic_op> <operand>

<arithmetic_op> → + | - | * | / 

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

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

发布评论

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

评论(2

稍尽春風 2025-01-09 10:16:13

从语法创建解释器时,ANTLR 是可行的方法吗?

不可以。ANTLRWorks

只能用于编写语法并可能测试其输入是否正确(通过其调试器或解释器)。它不能用于为您为其编写语法的语言创建解释器。 ANTLRWorks 只是一个精美的文本编辑器,仅此而已。

你们看到我的代码中有什么错误吗?

正如 Treebranch 所示:您在 = 登录处没有引号:

assignment_statement:   ID = arithmetic_expression;

使 ANTLR“认为”您想要将标签 ID 分配给解析器规则 算术表达式,这是非法的:您不能拥有一个同时也是规则名称的标签名称(ID,在您的情况下)。

Is ANTLRworks the way to go when creating a interpreter from grammar.

No.

ANTLRWorks can only be used to write your grammar and possibly test to see if it input properly (through its debugger or interpreter). It cannot be used to create an interpreter for the language you've written the grammar for. ANTLRWorks is just a fancy text-editor, nothing more.

And do y'all see any error in my code?

As indicated by Treebranch: you didn't have quotes around the = sign in:

assignment_statement:   ID = arithmetic_expression;

making ANTLR "think" you wanted to assign the label ID to the parser rule arithmetic_expression, which is illegal: you can't have a label-name that is also the name of a rule (ID, in your case).

小霸王臭丫头 2025-01-09 10:16:13

您的代码中可能存在一些问题:

我认为您希望您的 ID 标记有一个 + 正则表达式,以便它的长度可以为 1 或更大,如下所示:

ID  :   ('a'..'z'|'A'..'Z'|'_')+
;

它看起来也像您缺少引号引起来的= 符号:

assignment_statement:   ID '=' arithmetic_expression;

编辑

关于您的左递归问题:由于正则表达式功能,ANTLR 非常强大。虽然 EBNF(就像您所介绍的那样)可能在表达事物的方式上受到限制,但 ANTLR 可以用来以一种更简单的方式表达某些语法规则。例如,如果您希望在您的compound_statement 中有一个statement_list,只需使用带有闭包(*) 的statement 规则即可。就像这样:

compound_statement: 'begin' statement* 'end';

突然间,您可以删除不必要的规则,例如statement_list。

Some possible issues in your code:

I think you want your ID tag to have a + regex so that it can be of length 1 or more, like so:

ID  :   ('a'..'z'|'A'..'Z'|'_')+
;

It also looks like you are missing quotes around your = sign:

assignment_statement:   ID '=' arithmetic_expression;

EDIT

Regarding your left recursion issue: ANTLR is very powerful because of the regex functionality. While an EBNF (like the one you have presented) may be limited in the way it can express things, ANTLR can be used to express certain grammar rules in a much simpler way. For instance, if you want to have a statement_list in your compound_statement, just use your statement rule with closure (*). Like so:

compound_statement: 'begin' statement* 'end';

Suddently, you can remove unnecessary rules like statement_list.

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