2

我正在尝试使用 g++ 为玩具语言编译扫描仪和解析器。这是我使用的每个文件的代码(如果需要,我可以将其发布到pastebin或其他任何地方)。

caesar.ll

/* Simple scanner for a Caesar language */
%{
#include "caesar.tab.h"
#include <iostream>
#include <string>
int chars = 0;
int words = 0;
int lines = 0;
%}

/* Define constants */
OWS            [" "\t]*
COMMA          {OWS}","{OWS}
ID             [A-Za-z_][A-Za-z0-9_]*
INT            ([0-9]+)|("0x"[A-Ha-h0-9]+)
FLOAT          [0-9]+"."[0-9]+
BSTREAM        b[\'\"].*[\'\"]
USTREAM        u?[\'\"].*[\'\"]
ARRAY          {LBRACE}({INT}|{FLOAT})({COMMA}({INT}|{FLOAT})){RBRACE}
LIST           {LBRACKET}.*({COMMA}.*){RBRACKET}
RANGE          {LBRACE}{INT}":"{INT}(":"{INT})?{RBRACE}
ARGS           {ID}({COMMA}{ID})*
LPARENTHESIS   "("{OWS}
RPARENTHESIS   {OWS}")"
LBRACE         "{"{OWS}
RBRACE         {OWS}"}"
LBRACKET       "["{OWS}
RBRACKET       {OWS}"]"

%%
%{
/*============================================================================*/
/* Define types */
/*============================================================================*/
%}
{INT} {
  cout << "int: " << yytext << endl;
  yylval = atoi(yytext);
  return INT;
} /* int type */

{FLOAT} {
  cout << "float: " << yytext << endl;
  yylval = atof(yytext);
  return FLOAT;
} /* float type */

{BSTREAM} {
  cout << "bstream: " << yytext << endl;
  return BSTREAM;
} /* bstream type */

{USTREAM} {
  cout << "ustream: " << yytext << endl;
  return USTREAM;
} /* ustream type */

%{
/*============================================================================*/
/* Define operators */
/*============================================================================*/
%}
"+"    { return ADD; }
"-"    { return SUB; }
"*"    { return MUL; }
"/"    { return DIV; }
"//"   { return FDIV; }
"|"    { return ABS; }
"\n"   { return EOL; }

%{
/*============================================================================*/
/* Define statements */
/*============================================================================*/
%}
{RANGE} {
  cout << "range: " << yytext << endl;
  return RANGE;
} /* range function */

%%

caesar.yy

/* Simple parser for a Caesar language */
%{
#include <iostream>
using namespace std;
%}

/* Define built-in types */
%token INT FLOAT BSTREAM USTREAM 
%token ADD SUB MUL DIV FDIV ABS
%token EOL

%%

calclist: /* nothing */
  | calclist exp EOL {
      cout << $2 << endl;
    }
  | calclist EOL {
      cout << ">>> ";
    }
  ;

exp: factor
  | exp ADD exp { $$ = $1 + $3; }
  | exp SUB factor { $$ = $1 - $3; }
  | exp ABS factor { $$ = $1 | $3; }
  ;

factor: term
  | factor MUL term { $$ = $1 * $3; }
  | factor DIV term { $$ = $1 / $3; }
  ;

term: INT
  | ABS term { $$ = $2 >= 0? $2 : - $2; }
  ;

%%

main()
{
  cout << ">>> ";
  yyparse();
}

yyerror(char *error)
{
  cerr << error;
}

Makefile

caesar: caesar.ll caesar.yy
    bison -d caesar.yy
    flex caesar.ll
    g++ -o $@ caesar.tab.cc lex.yy.c -lfl

当我尝试使用 编译它时make,我看到几个错误:

bison -d caesar.yy
caesar.yy: conflicts: 3 shift/reduce
flex caesar.ll
g++ -o caesar caesar.tab.cc lex.yy.c -lfl
caesar.tab.cc: In function 'int yyparse()':
caesar.tab.cc:1281:16: error: 'yylex' was not declared in this scope
caesar.tab.cc:1470:35: error: 'yyerror' was not declared in this scope
caesar.tab.cc:1612:35: error: 'yyerror' was not declared in this scope
caesar.yy: At global scope:
caesar.yy:46:20: error: ISO C++ forbids declaration of 'yyerror' with no type [-fpermissive]
caesar.ll:3:24: fatal error: caesar.tab.h: No such file or directory
compilation terminated.
make: *** [caesar] Error 1

请问你能帮帮我吗?谢谢!

更新:我已经修复了函数类型不正确的错误。

4

3 回答 3

4

首先修复明显的错误——在 caesar.yy 的顶部添加声明:

int yylex(void);
void yyerror(const char *);

main和返回类型yyerror(注意——我还添加const到 yyerror 的参数中,以消除有关传递给它的字符串文字的警告)。

您需要对 caesar.ll 进行类似的简单修复:

#include "caesar.tab.hh"
using namespace std;

现在您可以看到真正的错误:

caesar.yy: conflicts: 3 shift/reduce
caesar.ll: In function ‘int yylex()’:
caesar.ll:79:10: error: ‘RANGE’ was not declared in this scope

第二个首先 - 您的扫描仪正在尝试返回未定义的令牌 RANGE。您可以添加%token RANGEtocaesaer.yy来定义它,但由于您在语法中不使用它(或各种其他标记,如BSTREAMor USTREAM),它只会导致语法错误。

这给我们带来了语法冲突。这些并不是真正的错误(更像是警告),但您确实需要注意它们。bison在命令中添加 -v 标志Makefile,您将获得一个caesaer.output包含冲突信息的文件。

3 个冲突都来自状态 16,您可以在 .output 文件中看到:

state 16

    5 exp: exp . ADD exp
    5    | exp ADD exp .
    6    | exp . SUB factor
    7    | exp . ABS factor

    ADD  shift, and go to state 10
    SUB  shift, and go to state 11
    ABS  shift, and go to state 12

    ADD       [reduce using rule 5 (exp)]
    SUB       [reduce using rule 5 (exp)]
    ABS       [reduce using rule 5 (exp)]
    $default  reduce using rule 5 (exp)

这告诉您所有 3 个冲突都来自您的exp: exp ADD exp规则。拥有左递归和右递归的规则总是模棱两可的,但在这种情况下,修复是显而易见的——将其更改为exp: exp ADD factor,匹配您的其余规则。

于 2012-10-23T01:45:20.107 回答
3

http://dinosaur.compilertools.net/flex/flex_19.html阅读这篇文章,了解如何g++使用flex. 这里的问题是您在C模式下使用它并且它会生成C词法分析器。flex-+开关一起使用。

于 2012-10-22T22:15:35.827 回答
0

FWIW,手动检测您的代码以查看匹配的规则是没有用的。Flex 和 Bison 都免费为您完成这项工作。Flex参见http://westes.github.io/flex/manual/Debugging-Options.html,Bison参见http://www.gnu.org/software/bison/manual/bison.html#Tracing

于 2012-10-23T14:59:03.027 回答