c - FLEX- Bison : error: expected identifier or ‘(’ before string constant

标签 c gcc bison yacc flex-lexer

我在尝试同时使用 Flex 和 Bison 时遇到问题。当我到达使用 gcc 命令(gcc -c y.tab.c lex.yy.c)进行编译的部分时,我不断收到 flex 文件的错误提示

error: expected identifier or ‘(’ before string constant

代码如下:

FLEX(文件名为 arxeioflex.l):

%{
#include "y.tab.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
%}

%option noyywrap

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

%%
%%

%%
"extern" {return EXTERN;}
"void" {return VOID;}
"(" {return LP;}
")" {return RP;}
"int" {return INT;}
"bool" {return BOOL;}
"string" {return STRING;}
";" {return SC;}
"," {return S;}
"&" {return DEC;}
"begin" {return BEGIN;}
"end" {return END;}
"{" {return LB;}
"}" {return RB;}
"if" {return IF;}
"else" {return ELSE;}
"=" {return ASIGN;}
"return" {return RETURN;}
"||" {return OR;}
"&&" {return AND;}
"!" {return NOT;}
"==" {return EQUAL;}
"!=" {return NEQUAL;}
"<" {return LESS;}
">" {return GREATER;}
"<=" {return LESSEQUAL;}
">=" {return GREATEREQUAL;}
"*" {return 'MUL';}
"/" {return DIV;}
"%" {return MOD;}
"+" {return 'PLUS';}
"-" {return MINUS;}
"true" {return TRUE;}
"false" {return FALSE;}

Bison :

%{

#include <stdio.h>

#include <math.h>

void yyerror(char *);

extern FILE *yyin;

extern FILE *yyout;

%}



%token EXTERN;

%token VOID;

%token LP RP;

%token ID;

%token INT BOOL STRING;

%token SC;

%token S; /*Seperator*/

%token DEC; /*&*/

%token BEGIN END;

%token LB RB;

%token IF ELSE;

%token ASSIGN; /*=*/

%token RETURN;

%token OR;

%token AND;

%token NOT;

%token EQUAL;

%token NEQUAL;

%token LESS;

%token GREATER;

%token LESSEQUAL;

%token GREATEREQUAL;

%token MUL;

%token DIV;

%token MOD;

%token PLUS;

%token MINUS;

%token TRUE;

%token FALSE;

%%



program: externs header definitions commands;



externs: extern_prot

        |externs externs;



extern_prot: EXTERN function_prot;



header: VOID ID SC SC;

definitions: definition 

           | definition definitions;



definition: variable_def

           | function_def  

           | function_prot;



variable_def: data_type var_list;

data_type: INT

         | BOOL

         | STRING;

var_list: ID

        | ID S var_list;



function_def: f_header definitions commands;



function_prot: f_header SC;



f_header: f_type ID LP typical_par_list RP;



f_type: INT

      | BOOL

      | VOID;

typical_par_list: typical_par

                | typical_par S typical_par_list;



typical_par: data_type DEC ID;



commands: BEGIN n_command  END;

/*n_command = (entolh*) */

n_command: command

         | command n_command;


command: plain_command SC

       | struct_command

       | complex_command; 



complex_command: LB n_command RB;  



struct_command: if_clause;



plain_command: assignment

             | function_call

             | return_command

             | null_command;



if_clause: IF LP general_expr RP command else_clause;



else_clause: ELSE command;



assignment: ID ASSIGN general_expr;



function_call: ID LP real_par_list RP;



real_par_list: real_par

             | real_par real_par_list;


real_par: general_expr;



return_command: RETURN general_expr;



null_command: ;



general_expr: general_term 

            | general_term OR general_expr;



general_term: general_factor  

            | general_factor AND general_term;

general_factor: NOT general_first_term;



general_first_term: plain_expr comparison_field;



comparison_field: comparison_effector plain_expr;



comparison_effector: EQUAL

                   | NEQUAL

                   | LESS

                   | GREATER

                   | LESSEQUAL

                   | GREATEREQUAL;


plain_expr: plain_term

          | plain_term PLUS plain_expr

          | plain_term MINUS plain_expr;



plain_term: plain_factor

          | plain_factor MUL plain_term

          | plain_factor DIV plain_term

          | plain_factor MOD plain_term;



plain_factor: plain: PLUS plain_first_term

                   | MINUS plain_first_term;


plain_first_term: ID

                | constant

                | function_call

                | LP general_expr RP;



constant: ID

        | TRUE

        | FALSE;



%%



void yyerror(char *s) {

    fprintf(stderr, "%s\n", s);

}





int main ( int argc, char **argv  )

  {

  ++argv; --argc;

  if ( argc > 0 )  

        yyin = fopen( argv[0], "r" );

  else

        yyin = stdin;

  yyout = fopen ( "output", "w" );

  yyparse ();

 return 0;

}

错误:

arxeioflex.l:13: error: expected identifier or ‘(’ before ‘%’ token
arxeioflex.l:15: error: expected identifier or ‘(’ before string constant
arxeioflex.l:16: error: expected identifier or ‘(’ before string constant
arxeioflex.l:17: error: expected identifier or ‘(’ before string constant
arxeioflex.l:18: error: expected identifier or ‘(’ before string constant
arxeioflex.l:19: error: expected identifier or ‘(’ before string constant
arxeioflex.l:20: error: expected identifier or ‘(’ before string constant
arxeioflex.l:21: error: expected identifier or ‘(’ before string constant
arxeioflex.l:22: error: expected identifier or ‘(’ before string constant
arxeioflex.l:23: error: expected identifier or ‘(’ before string constant
arxeioflex.l:24: error: expected identifier or ‘(’ before string constant
arxeioflex.l:25: error: expected identifier or ‘(’ before string constant
arxeioflex.l:26: error: expected identifier or ‘(’ before string constant
arxeioflex.l:27: error: expected identifier or ‘(’ before string constant
arxeioflex.l:28: error: expected identifier or ‘(’ before string constant
arxeioflex.l:29: error: expected identifier or ‘(’ before string constant
arxeioflex.l:30: error: expected identifier or ‘(’ before string constant
arxeioflex.l:31: error: expected identifier or ‘(’ before string constant
arxeioflex.l:32: error: expected identifier or ‘(’ before string constant
arxeioflex.l:33: error: expected identifier or ‘(’ before string constant
arxeioflex.l:34: error: expected identifier or ‘(’ before string constant
arxeioflex.l:35: error: expected identifier or ‘(’ before string constant
arxeioflex.l:36: error: expected identifier or ‘(’ before string constant
arxeioflex.l:37: error: expected identifier or ‘(’ before string constant
arxeioflex.l:38: error: expected identifier or ‘(’ before string constant
arxeioflex.l:39: error: expected identifier or ‘(’ before string constant
arxeioflex.l:40: error: expected identifier or ‘(’ before string constant
arxeioflex.l:41: error: expected identifier or ‘(’ before string constant
arxeioflex.l:42: error: expected identifier or ‘(’ before string constant
arxeioflex.l:43: error: expected identifier or ‘(’ before string constant
arxeioflex.l:44: error: expected identifier or ‘(’ before string constant
arxeioflex.l:45: error: expected identifier or ‘(’ before string constant
arxeioflex.l:46: error: expected identifier or ‘(’ before string constant
arxeioflex.l:47: error: expected identifier or ‘(’ before string constant
arxeioflex.l:49: error: expected identifier or ‘(’ before ‘%’ token

最佳答案

arxeioflex.l 段太多,替换:

num [0-9]*

%%
%%

%%
"extern" {return EXTERN;}

通过

num [0-9]*

%%
"extern" {return EXTERN;}

关于c - FLEX- Bison : error: expected identifier or ‘(’ before string constant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30597178/

相关文章:

c - 如何从 .t​​ext 文件中查找最小值和最大值

c++ - gcc的 "__builtin_popcount"是怎么工作的?

c - 错误使用字符串化

gcc - GNU C++ 错误消息

parsing - Bison 减少/减少与强制转换和表达式括号的冲突

c++ - 如何为动态类型语言构建编译器?

C 数组 char 与数组 int 比较

c - 测试多个数组元素

c - 强制 fscanf 消耗可能的空白

c - YACC 文件是否有文件包含机制?