Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

对标准bnf-file的parse #2

Open
acodercc opened this issue Oct 24, 2014 · 0 comments
Open

对标准bnf-file的parse #2

acodercc opened this issue Oct 24, 2014 · 0 comments
Assignees

Comments

@acodercc
Copy link
Owner

要把bnf解析为jsbison能使用的json格式的上下文无关文法。。

如:

%start expr
%%
expr
    : expr '+' term
        {
            this.$$ = $1 + $2;
        }
    | term
        {
            this.$$ = $1;
        }
    ;

term
    : term '*' factoy
        {
            this.$$ = $1 * $2;
        }
    | factoy
        {
            this.$$ = $1;
        }
    ;

factoy
    : NUMBER
        {
            this.$$ = parseInt($1, 10);
        }
    |   '(' expr ')'
        {
            this.$$ = $2;
        }
    ;

转换为:

{
        tokens: '+ * ( ) NUMBER',
        start: 'expr',
        bnf: {
            'expr': {
                'expr + term': 'this.$$ = $1 + $3',
                'term': 'this.$$ = $1'
            },
            'term': {
                'term * factor': 'this.$$ = $1 * $3',
                'factor': 'this.$$ = $1'
            },
            'factor': {
                'NUMBER': 'this.$$ = parseInt($1, 10)',
                '( expr )': 'this.$$ = $2'
            }
        }
}

jsbison的bnf2cfg,所能解析的bnf文件中支持的功能:

功能 完成度
声明token: %token ok
声明结合性与优先级 %left %right %nonassoc ok
声明开始符号: %start ok
未声明开始符号时,自动使用首个定义的产生式的lhs为开始符号 不支持,必须定义%start
应用产生式归约为非终结符时的语义动作 ok
语义动作中引用各符号值 $1 $2 $n ok
语义动作中定义归约后的非终结符的值 this.$$ ok
@acodercc acodercc self-assigned this Oct 27, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant