ANTLRworks 从语法创建解释器
嘿,我有一个简单的问题。我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不可以。ANTLRWorks
只能用于编写语法并可能测试其输入是否正确(通过其调试器或解释器)。它不能用于为您为其编写语法的语言创建解释器。 ANTLRWorks 只是一个精美的文本编辑器,仅此而已。
正如 Treebranch 所示:您在
=
登录处没有引号:使 ANTLR“认为”您想要将标签
ID
分配给解析器规则算术表达式
,这是非法的:您不能拥有一个同时也是规则名称的标签名称(ID
,在您的情况下)。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.
As indicated by Treebranch: you didn't have quotes around the
=
sign in:making ANTLR "think" you wanted to assign the label
ID
to the parser rulearithmetic_expression
, which is illegal: you can't have a label-name that is also the name of a rule (ID
, in your case).您的代码中可能存在一些问题:
我认为您希望您的 ID 标记有一个
+
正则表达式,以便它的长度可以为 1 或更大,如下所示:它看起来也像您缺少引号引起来的
=
符号:编辑
关于您的左递归问题:由于正则表达式功能,ANTLR 非常强大。虽然 EBNF(就像您所介绍的那样)可能在表达事物的方式上受到限制,但 ANTLR 可以用来以一种更简单的方式表达某些语法规则。例如,如果您希望在您的compound_statement 中有一个statement_list,只需使用带有闭包(
*
) 的statement 规则即可。就像这样:突然间,您可以删除不必要的规则,例如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:It also looks like you are missing quotes around your
=
sign: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:Suddently, you can remove unnecessary rules like statement_list.