FLEX-BISON: error:期望值标识符或' (' before string constant)

时间:2022-08-16 19:30:46

I am experiencing an issue trying to use Flex and Bison together. When I reach the part of compiling with the gcc command (gcc -c y.tab.c lex.yy.c), i keep getting errors for the flex file saying

我遇到了一个问题,试图同时使用Flex和Bison。当我使用gcc命令进行编译时(gcc -c .tab)。c),我一直在为flex文件出错说。

error: expected identifier or ‘(’ before string constant

Here is the code :

这里是代码:

FLEX (filename is arxeioflex.l) :

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;

}

ERRORS :

错误:

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

1 个解决方案

#1


5  

arxeioflex.l has too many sections, replace:

arxeioflex。l有太多的分段,替换:

num [0-9]*

%%
%%

%%
"extern" {return EXTERN;}

by

通过

num [0-9]*

%%
"extern" {return EXTERN;}

#1


5  

arxeioflex.l has too many sections, replace:

arxeioflex。l有太多的分段,替换:

num [0-9]*

%%
%%

%%
"extern" {return EXTERN;}

by

通过

num [0-9]*

%%
"extern" {return EXTERN;}