杰森语法问题,生成dparser产生的奇怪错误 [英] Issue with a Jison Grammar, Strange error from generate dparser

查看:111
本文介绍了杰森语法问题,生成dparser产生的奇怪错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的Jison语法,以便在开始更复杂的项目之前获得一些经验.我尝试了一个简单的语法,该语法是用逗号分隔的数字范围列表,其中范围的开始和结束值相同,以使用单个数字的简写形式.但是,在某些测试输入上运行生成的解析器时,出现错误,这对我来说没有多大意义.这是我想出的语法:

/* description: Parses end executes mathematical expressions. */

/* lexical grammar */
%lex
%%

\s+                   /* skip whitespace */
[0-9]+                {return 'NUMBER'}
"-"                   {return '-'}
","                   {return ','}
<<EOF>>               {return 'EOF'}
.                     {return 'INVALID'}

/lex

/* operator associations and precedence */

%start ranges

%% /* language grammar */

ranges
    : e EOF
        {return $1;}
    ;

e   :  rng { $$ = $1;}
    | e ',' e {alert('e,e');$$ = new Array(); $$.push($1); $$.push($3);}
    ;

rng
    : NUMBER '-' NUMBER
        {$$ = new Array(); var rng = {Start:$1, End: $3; }; $$.push(rng); }
    | NUMBER
        {$$ = new Array(); var rng = {Start:$1, End: $1; }; $$.push(rng);}
    ;

NUMBER: {$$ = Number(yytext);};

测试输入为:

5-10,12-16

输出为:

Parse error on line 1:
5-10,12-16
^
Expecting '-', 'EOF', ',', got '8'

如果将'a'放在前面,我会发现关于查找"INVALID"的预期错误,但是我在输入字符串中没有"8",所以我想知道这是否是内部状态吗?

我在以下位置使用在线解析器生成器: http://zaach.github.io/jison/尝试/

有什么想法?

解决方案

此产品使Jison感到困惑(它也使我感到困惑:)):

NUMBER: {$$ = Number(yytext);};

NUMBER应该是一个终端,但是上面的产生将其声明为一个空终端的非终端.因为它什么都不能匹配,所以它立即匹配,并且您的语法不允许两个连续的NUMBER.因此是错误.

此外,尽管我认为Jison的默认设置将解决此问题,但是您的语法是模棱两可的.不过,最好还是明确一点,因为这很容易.您的规则:

e   : rng 
    | e ',' e

未指定,的关联"方式:换句话说,应将rng , rng , rng视为e , rng还是rng , e.第一个可能对您更好,因此您应该明确地编写它:

e   :  rng
    |  e ',' rng

以上内容的一大优点是,您无需在第二个产品中创建新的阵列;您只需将$3推到$1的末尾,然后将$$设置为$1.

I am writing a simple Jison grammar in order to get some experience before starting a more complex project. I tried a simple grammar which is a comma separated list of numeric ranges, with ranges where the beginning and ending values were the same to use a single number shorthand. However, when running the generated parser on some test input I get an error which doe snot make alot of sense to me. Here is the grammar i came up with:

/* description: Parses end executes mathematical expressions. */

/* lexical grammar */
%lex
%%

\s+                   /* skip whitespace */
[0-9]+                {return 'NUMBER'}
"-"                   {return '-'}
","                   {return ','}
<<EOF>>               {return 'EOF'}
.                     {return 'INVALID'}

/lex

/* operator associations and precedence */

%start ranges

%% /* language grammar */

ranges
    : e EOF
        {return $1;}
    ;

e   :  rng { $$ = $1;}
    | e ',' e {alert('e,e');$$ = new Array(); $$.push($1); $$.push($3);}
    ;

rng
    : NUMBER '-' NUMBER
        {$$ = new Array(); var rng = {Start:$1, End: $3; }; $$.push(rng); }
    | NUMBER
        {$$ = new Array(); var rng = {Start:$1, End: $1; }; $$.push(rng);}
    ;

NUMBER: {$$ = Number(yytext);};

The Test input is this:

5-10,12-16

The output is:

Parse error on line 1:
5-10,12-16
^
Expecting '-', 'EOF', ',', got '8'

If it put an 'a' at the front i get and expected error about finding "INVALID" but i dont have an "8" in the input string so i wondering if this is an internal state?

I am using the online parser generator at: http://zaach.github.io/jison/try/

thoughts?

解决方案

This production is confusing Jison (and it confused me, too :) ):

NUMBER: {$$ = Number(yytext);};

NUMBER is supposed to be a terminal, but the above production declares it as a non-terminal with an empty body. Since it can match nothing, it immediately matches, and your grammar doesn't allow two consecutive NUMBERs. Hence the error.

Also, your grammar is ambiguous, although I suppose Jison's default will solve the issue. It would be better to be explicit, though, since it's easy. Your rule:

e   : rng 
    | e ',' e

does not specify how , "associates": in other words, whether rng , rng , rng should be considered as e , rng or rng , e. The first one is probably better for you, so you should write it explicitly:

e   :  rng
    |  e ',' rng

One big advantage of the above is that you don't need to create a new array in the second production; you can just push $3 onto the end of $1 and set $$ to $1.

这篇关于杰森语法问题,生成dparser产生的奇怪错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆