如何解决歧义
我有一个语法:
grammar Test;
s : ID OP (NUMBER | ID);
ID : [a-z]+ ;
NUMBER : '.'? [0-9]+ ;
OP : '/.' | '/' ;
WS : [ \t\r\n]+ -> skip ;
像x/.123
这样的表达式可以解析为(s x /. 123)
或(s x / .123)
.有了上面的语法,我得到了第一个变种.
An expression like x/.123
can either be parsed as (s x /. 123)
, or as (s x / .123)
. With the grammar above I get the first variant.
有没有办法获得两个解析树?有没有一种方法可以控制它的解析方式?说,如果/.
后面有一个数字,那么我发出/
,否则我在树中发出/.
.
Is there a way to get both parse trees? Is there a way to control how it is parsed? Say, if there is a number after the /.
then I emit the /
otherwise I emit /.
in the tree.
我是ANTLR的新手.
I am new to ANTLR.
x/.123之类的表达式可以解析为(s x/.123)或解析为(s x/.123)
An expression like x/.123 can either be parsed as (s x /. 123), or as (s x / .123)
我不确定.在ReplaceAll页面(*)的可能出现的问题"段落中,据说句号绑定到数字的强度比斜线更强",因此/.123
将始终被解释为数字.123
的除法运算.接下来,为了避免此问题,如果您希望将其理解为替代内容,则必须在/.
运算符和数字之间的输入中插入一个空格.
I'm not sure. In the ReplaceAll page(*), Possible Issues paragraph, it is said that "Periods bind to numbers more strongly than to slash", so that /.123
will always be interpreted as a division operation by the number .123
. Next it is said that to avoid this issue, a space must be inserted in the input between the /.
operator and the number, if you want it to be understood as a replacement.
所以只有一棵可能的解析树(否则Wolfram解析器将如何决定如何解释该语句?).
So there is only one possible parse tree (otherwise how could the Wolfram parser decide how to interpret the statement ?).
ANTLR4词法分析器和解析器都很贪婪.这意味着词法分析器(解析器)会尝试在匹配规则时读取尽可能多的输入字符(令牌).使用OP规则OP : '/.' | '/' ;
,词法分析器将始终将输入/.
与替代项/.
匹配(即使规则为OP : '/' | '/.' ;
).这意味着没有歧义,您也没有机会将输入解释为OP =/和NUMBER = .123.
ANTLR4 lexer and parser are greedy. It means that the lexer (parser) tries to read as much input characters (tokens) that it can while matching a rule. With your OP rule OP : '/.' | '/' ;
the lexer will always match the input /.
to the /.
alternative (even if the rule is OP : '/' | '/.' ;
). This means there is no ambiguity and you have no chance the input to be interpreted as OP=/ and NUMBER=.123.
基于我对ANTLR的少量了解,我发现除了将ReplaceAll运算符拆分为两个令牌外,没有其他解决方案.
Given my small experience with ANTLR, I have found no other solution than to split the ReplaceAll operator into two tokens.
语法Question.g4:
Grammar Question.g4 :
grammar Question;
/* Parse Wolfram ReplaceAll. */
question
@init {System.out.println("Question last update 0851");}
: s+ EOF
;
s : division
| replace_all
;
division
: expr '/' NUMBER
{System.out.println("found division " + $expr.text + " by " + $NUMBER.text);}
;
replace_all
: expr '/' '.' replacement
{System.out.println("found ReplaceAll " + $expr.text + " with " + $replacement.text);}
;
expr
: ID
| '"' ID '"'
| NUMBER
| '{' expr ( ',' expr )* '}'
;
replacement
: expr '->' expr
| '{' replacement ( ',' replacement )* '}'
;
ID : [a-z]+ ;
NUMBER : '.'? [0-9]+ ;
WS : [ \t\r\n]+ -> skip ;
输入文件t.text:
Input file t.text :
x/.123
x/.x -> 1
{x, y}/.{x -> 1, y -> 2}
{0, 1}/.0 -> "zero"
{0, 1}/. 0 -> "zero"
执行:
$ export CLASSPATH=".:/usr/local/lib/antlr-4.6-complete.jar"
$ alias a4='java -jar /usr/local/lib/antlr-4.6-complete.jar'
$ alias grun='java org.antlr.v4.gui.TestRig'
$ a4 Question.g4
$ javac Q*.java
$ grun Question question -tokens -diagnostics t.text
[@0,0:0='x',<ID>,1:0]
[@1,1:1='/',<'/'>,1:1]
[@2,2:5='.123',<NUMBER>,1:2]
[@3,7:7='x',<ID>,2:0]
[@4,8:8='/',<'/'>,2:1]
[@5,9:9='.',<'.'>,2:2]
[@6,10:10='x',<ID>,2:3]
[@7,12:13='->',<'->'>,2:5]
[@8,15:15='1',<NUMBER>,2:8]
[@9,17:17='{',<'{'>,3:0]
...
[@29,47:47='}',<'}'>,4:5]
[@30,48:48='/',<'/'>,4:6]
[@31,49:50='.0',<NUMBER>,4:7]
...
[@40,67:67='}',<'}'>,5:5]
[@41,68:68='/',<'/'>,5:6]
[@42,69:69='.',<'.'>,5:7]
[@43,71:71='0',<NUMBER>,5:9]
...
[@48,83:82='<EOF>',<EOF>,6:0]
Question last update 0851
found division x by .123
found ReplaceAll x with x->1
found ReplaceAll {x,y} with {x->1,y->2}
found division {0,1} by .0
line 4:10 extraneous input '->' expecting {<EOF>, '"', '{', ID, NUMBER}
found ReplaceAll {0,1} with 0->"zero"
输入x/.123
是模糊的,直到斜线为止.然后,解析器有两个选择:除法规则中的/ NUMBER
或replace_all规则中的/ . expr
.我认为NUMBER吸收了输入,因此不再有歧义.
The input x/.123
is ambiguous until the slash. Then the parser has two choices : / NUMBER
in the division rule or / . expr
in the replace_all rule. I think that NUMBER absorbs the input and so there is no more ambiguity.
(*),该链接是昨天消失的注释,即 Wolfram语言&系统,全部替换
(*) the link was yesterday in a comment that has disappeared, i.e. Wolfram Language & System, ReplaceAll