为什么使用 flex 和 yacc 编写的程序中出现语法错误?

发布于 2024-08-11 22:07:09 字数 2665 浏览 2 评论 0原文

我编写了一个应该识别简单语法的程序。当我输入我认为有效的语句时,我收到错误。具体来说,如果我输入

int a;

整数b;

它不起作用。当我输入 int a; 后节目回响;由于某种原因。然后当我输入 int b; 时我收到语法错误。

lex 文件:

%{
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include "y.tab.h"

%}

else ELSE
if IF
int INT|int
return RETURN
void VOID
while WHILE
id [a-zA-Z]*
num [0-9]*
lte <=
gte >=
equal ==
notequal !=

%%

{else}  {   return ELSE; }
{if}    {   return IF; }
{int}   {   return INT; }
{return} {  return RETURN; }
{void} {    return VOID; }
{while} {   return WHILE; }
{id} {      return ID; }
{num} {     return NUM; }
{lte} {     return LTE; }
{gte} {     return GTE; }
{equal} {   return EQUAL; }
{notequal} {    return NOTEQUAL; }
%%

yacc 文件:

/* C-Minus BNF Grammar */

%token ELSE
%token IF
%token INT
%token RETURN
%token VOID
%token WHILE

%token ID
%token NUM

%token LTE
%token GTE
%token EQUAL
%token NOTEQUAL
%%

program : declaration_list ;

declaration_list : declaration_list declaration | declaration ;

declaration : var_declaration | fun_declaration ;

var_declaration : type_specifier ID ';'
                | type_specifier ID '[' NUM ']' ';' ;

type_specifier : INT | VOID ;

fun_declaration : type_specifier ID '(' params ')' compound_stmt ;

params : param_list | VOID ;

param_list : param_list ',' param
           | param ;

param : type_specifier ID | type_specifier ID '[' ']' ;

compound_stmt : '{' local_declarations statement_list '}' ;

local_declarations : local_declarations var_declaration
                   | /* empty */ ;

statement_list : statement_list statement
               | /* empty */ ;

statement : expression_stmt
          | compound_stmt
          | selection_stmt
          | iteration_stmt
          | return_stmt ;

expression_stmt : expression ';'
                | ';' ;

selection_stmt : IF '(' expression ')' statement
               | IF '(' expression ')' statement ELSE statement ;

iteration_stmt : WHILE '(' expression ')' statement ;

return_stmt : RETURN ';' | RETURN expression ';' ;

expression : var '=' expression | simple_expression ;

var : ID | ID '[' expression ']' ;

simple_expression : additive_expression relop additive_expression
                  | additive_expression ;

relop : LTE | '<' | '>' | GTE | EQUAL | NOTEQUAL ;

additive_expression : additive_expression addop term | term ;

addop : '+' | '-' ;

term : term mulop factor | factor ;

mulop : '*' | '/' ;

factor : '(' expression ')' | var | call | NUM ;

call : ID '(' args ')' ;

args : arg_list | /* empty */ ;

arg_list : arg_list ',' expression | expression ;

I made a program that is supposed to recognize a simple grammar. When I input what I think is supposed to be a valid statement, I get an error. Specifically, if I type

int a;

int b;

it doesn't work. After I type int a; the program echoes ; for some reason. Then when I type int b; I get syntax error.

The lex file:

%{
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include "y.tab.h"

%}

else ELSE
if IF
int INT|int
return RETURN
void VOID
while WHILE
id [a-zA-Z]*
num [0-9]*
lte <=
gte >=
equal ==
notequal !=

%%

{else}  {   return ELSE; }
{if}    {   return IF; }
{int}   {   return INT; }
{return} {  return RETURN; }
{void} {    return VOID; }
{while} {   return WHILE; }
{id} {      return ID; }
{num} {     return NUM; }
{lte} {     return LTE; }
{gte} {     return GTE; }
{equal} {   return EQUAL; }
{notequal} {    return NOTEQUAL; }
%%

The yacc file:

/* C-Minus BNF Grammar */

%token ELSE
%token IF
%token INT
%token RETURN
%token VOID
%token WHILE

%token ID
%token NUM

%token LTE
%token GTE
%token EQUAL
%token NOTEQUAL
%%

program : declaration_list ;

declaration_list : declaration_list declaration | declaration ;

declaration : var_declaration | fun_declaration ;

var_declaration : type_specifier ID ';'
                | type_specifier ID '[' NUM ']' ';' ;

type_specifier : INT | VOID ;

fun_declaration : type_specifier ID '(' params ')' compound_stmt ;

params : param_list | VOID ;

param_list : param_list ',' param
           | param ;

param : type_specifier ID | type_specifier ID '[' ']' ;

compound_stmt : '{' local_declarations statement_list '}' ;

local_declarations : local_declarations var_declaration
                   | /* empty */ ;

statement_list : statement_list statement
               | /* empty */ ;

statement : expression_stmt
          | compound_stmt
          | selection_stmt
          | iteration_stmt
          | return_stmt ;

expression_stmt : expression ';'
                | ';' ;

selection_stmt : IF '(' expression ')' statement
               | IF '(' expression ')' statement ELSE statement ;

iteration_stmt : WHILE '(' expression ')' statement ;

return_stmt : RETURN ';' | RETURN expression ';' ;

expression : var '=' expression | simple_expression ;

var : ID | ID '[' expression ']' ;

simple_expression : additive_expression relop additive_expression
                  | additive_expression ;

relop : LTE | '<' | '>' | GTE | EQUAL | NOTEQUAL ;

additive_expression : additive_expression addop term | term ;

addop : '+' | '-' ;

term : term mulop factor | factor ;

mulop : '*' | '/' ;

factor : '(' expression ')' | var | call | NUM ;

call : ID '(' args ')' ;

args : arg_list | /* empty */ ;

arg_list : arg_list ',' expression | expression ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路终见情 2024-08-18 22:07:09

好的...您还需要在语言规范中添加一个分号作为标记...仅供参考,对此进行谷歌搜索...有一些用于 C 编程语言的 lex/yacc 文件,例如嗯...有很多关于这方面的教程...flex/bison 对程序规范错误并不完全宽容...你真的需要了解它如何工作的元素...寻找 Jack Crenshaw 的著名教程如何构建编译器。

Ok...you need to add a semi-colon as a token as well in your language spec...as a fyi, do a google search on this ...there are a few lex/yacc files for C programming language as well...and there are plenty of tutorials on this...flex/bison are not exactly forgiving on program spec errors...you really need to understand the elements of how it works...Look for Jack Crenshaw's famous tutorial on how to build a compiler.

叹沉浮 2024-08-18 22:07:09

Lex:

id [a-zA-Z]*
num [0-9]*

两种情况都可以遇到空字符串,请使用 '+' 代替

Lex:

id [a-zA-Z]*
num [0-9]*

both cases can meet empty strings, use '+' instead

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文