语法分析器显示成功使用flex和野牛 [英] Syntax analyser to show success using flex and bison

查看:247
本文介绍了语法分析器显示成功使用flex和野牛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让一个语法分析器识别一个有效的语句,并打印成功。但是,在制作lex和yacc文件后,我不断收到我的yacc文件中的错误,说:

I am trying to make a syntax analyzer that will recognize a valid statement and will print success upon doing so. However, after making the lex and yacc files, I keep getting errors in my yacc file which says:


在函数'yyparse'fofo。 y:在函数'yyparse'中:

fofo.y:13:5:error:stray'\223'in program

fofo.y:13:5:error: strady'\'in program

fofo.y:13:16:error:'n'undeclared(首次在此函数中使用)

fofo.y:13:16:注意:对于在
中出现的每个函数,每个未声明的标识符只报告一次。
fofo.y:13:18:error:expected')'before'Invalid'

fofo.y :13:18:错误:stray'\'in program

fofo.y:13:18:error:stray'\224'in program

In function 'yyparse'fofo.y: In function 'yyparse':
fofo.y:13:5: error: stray '\223' in program
fofo.y:13:5: error: stray '\' in program
fofo.y:13:16: error: 'n' undeclared (first use in this function)
fofo.y:13:16: note: each undeclared identifier is reported only once for each function it appears in
fofo.y:13:18: error: expected ')' before 'Invalid'
fofo.y:13:18: error: stray '\' in program
fofo.y:13:18: error: stray '\224' in program

这是我的yacc文件内容:

Here's my yacc file contents:

%{
#include <stdio.h>
%}

%start Stmt_list
%token Id Num Relop Addop Mulop Assignop Not

%%
Stmt_list   : Stmt ';' '\n' {printf ("\n Success. \n"); exit(0);}
        | Stmt_list Stmt ';' '\n'   {printf ("\n Success. \n"); exit(0);}
        | error '\n'    {printf ("\n Invalid. \n"); exit(1);}
        ;

Stmt    : Variable Assignop Expression
    ;

Variable    : Id 
        | Id '['Expression']'
        ;

Expression  : Simple_expression 
        | Simple_expression Relop Simple_expression
        ;

Simple_expression   : Term 
            | Simple_expression Addop Term
            ;

Term    : Factor 
    | Term Mulop Factor
    ;

Factor  : Id 
    | Num 
    | '('Expression')' 
    | Id '['Expression']' 
    | Not Factor
    ;

%%

#include"lex.yy.c"

int main()  
{   
    yyparse();  
    yylex();

}  

yyerror(char *s)  
{  
 printf("\nError\n");  
}  


推荐答案

文本中的非ASCII字符(可能来自粘贴Word文件中的文本),第13行,错误消息指示:

The errors come from having some non ASCII characters in the text (which probably come from pasting text from a Word file), on line 13, as the error message indicated:

        | error '\n'    {printf ("\n Invalid. \n"); exit(1);}
                                 ^              ^
                                 |              |
                                 `--------------`------------   The error is here!

请注意,引号字符不同于上面的行,应编辑为:

Note the quotation characters are different to the line above, and should be edited to be:

        | error '\n'    {printf ("\n Invalid. \n"); exit(1);}



我也在你的令牌周围添加了一些空白。例如,在这些行上:

I also added some white space around your tokens. For example, on these lines:

        | Id '['Expression']'
    | '('Expression')' 
    | Id '['Expression']'

,我改为:

        | Id '[' Expression ']'
    | '(' Expression ')' 
    | Id '[' Expression ']'

我也注意到你正在呼叫 C函数'exit',但没有正确声明。您的标题中需要以下行:

I also note you are calling the C function 'exit' but have not declared it properly. You need the following line in your header:

#include <stdlib.h>

它似乎为我构建好了。

这篇关于语法分析器显示成功使用flex和野牛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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