可执行文件无法正确运行(编译flex和bison)

问题描述 投票:-1回答:1

我正在尝试运行一个示例,但是每次在可执行文件夹exp上写一些指令时,我都会显示一个错误:开始= 5;结束。->至此,我的可执行文件出现此错误:

error

这是我的代码:

lexical.l:

%{
#include"stdio.h"
 int nbr_ligne;
 #include "syntaxique.tab.h"

  extern YYSTYPE yylval;

 %}
 chiffre [0-9]
 cst [1-9][0-9]*|0
 vide [ \t]+
  saut_ligne [\n]
  idf [A-Z]([_]?[a-z0-9])*
 int ([1-9][0-9]*|0)
 float ([1-9][0-9]*|0)","([0-9]*[1-9])
 bool       "FALSE"|"TRUE" 
 varint        "INT"|"int"
  varfloat      "FLOAT"|"float"
 varbool       "BOOL"|"bool"
  const_int "const int"|"CONST INT"
 const_float "const float"|"CONST FLOAT"
 op "*"|"+"|"-"|"/"|"%"
 rat "<"|"<="|"<>"|"=="|">"|">="|"!="
 incr "++"
 decr "--"
 arc "("
 farc ")"
 aff "=" 
 vg ","
 pvg ";"
 debut "{"
 fin "}"
 if "if"|"IF"|"If"
 for "for"|"FOR"
 begin "begin"|"BEGIN"
 end "end"|"END"
 comment       "//" (.|\n)* "//"


 %%
 {idf}         {printf(" idf");if (yyleng <= 12 ){yylval.str=strdup(yytext);return(mc_idf);} }
 {const_float} {printf(" const_float");return const_float;}
 {const_int}   {printf(" const_int");return const_int;}
  {vide}
  {begin}     {return(mc_begin);}
 {end}           {return(mc_end);}
 {varint}        { yylval.type=strdup(yytext);return(mc_varint);}
 {varfloat}      { yylval.type=strdup(yytext); return(mc_float);}
 {varbool}       {yylval.type=strdup(yytext);return(mc_varbool);}
 {int}           {yylval.entier=atoi(yytext);return(mc_int);}
 {float}         {yylval.entier=atof(yytext);return(mc_float);}
 {bool}          {yylval.type=strdup(yytext);return(mc_bool);}       
 {if}              {return(mc_if);}
 {for}             {return(mc_for);}
 {rat} {return mc_rat;}
 {op} {return mc_op;}

 {pvg} {printf(" pvg");return pvg;}
 {vg} {printf(" vg");return vg;}
 {arc} {printf(" arc");return arc;}
 {farc} {printf(" farc");return farc;}
 {debut} {printf(" debut");return debut;}
 {fin} {printf(" fin");return fin;}
 {incr} {printf(" incr");return incr;}
   {decr} {printf(" decr");return decr;}
   {aff} {printf(" aff");return aff;}



  [\n] {nbr_ligne++;} 

  .+ {printf("erreur");}


  %%
  int yywrap(){
   return 1;}

 int main(){
 yylex();
 return 0;
  }

这里是syntaxique.y:

  %{
  #include <stdio.h>
  #include <string.h>



 int yylex();
 int yyerror(char *s);
 int nb_ligne = 1;
 float test;
 char suavType [20];
  char value [100] ;

  %}
  %union{ 
 int entier; 
 float reel; 
 char* str; 
 char oper; 
 char* type;
 char* idf;
 char symbol;
  char* bool;}



 %token  var <entier>mc_int <reel>mc_float<oper>mc_div
 <bool>mc_bool <type>mc_varbool <idf>mc_idf <const>mc_const <type>mc_varint <type>mc_varfloat 
  mc_begin mc_for mc_if mc_end mc_rat <oper>mc_op <symbol>pvg <symbol>vg <symbol>aff debut fin arc 
  farc incr decr const_int const_float


 %start S

 %%

   S:Declaration mc_begin LIST_INST mc_end {printf("programme correcte syntaxiquement ");YYACCEPT;}
   ;
   TYPEVAR: mc_varfloat {strcpy(suavType,$1)} | mc_varint {strcpy(suavType,$1)} | mc_varbool 
    {strcpy(suavType,$1)}
    ;
  // LE CAS D'UNE DÉCLARATION DE PLUSIEURS VARIABLES: int a,b...
      VG: vg Idf VG |
      ;
  // LE CAS D'UNE DÉCLARATION DE PLUSIEURS CONSTANTE: const int a=6,b=6...
      VGCONST : vg Idf aff CONST VG|
      ;
      Idf: mc_idf
     ;
    IDFConst: const_int  mc_idf | const_float mc_idf
    ;

 Declaration:
       Declaration  TYPEVAR Idf VG pvg   |
       Declaration IDFConst aff CONST VGCONST pvg   |
    ;
  // LISTE of CONSTANTES
        CONST: mc_float{ test=$1; strcpy(value,"FLOAT");}|mc_int{ 
       test=$1;strcpy(value,"INT");}|mc_bool{strcpy(value,"BOOL");}|mc_idf|
        ; 
  //  ARITHMETIQUE  operation
  EXPARITH:CONST mc_op EXPARITH | CONST |arc EXPARITH farc|
  CONST mc_op mc_idf EXPARITH | mc_idf mc_op CONST EXPARITH
  ;
  AFFEC:mc_idf aff EXPARITH pvg | mc_idf aff mc_idf pvg | const_int mc_idf aff mc_int VGCONST pvg
  ;
  AFFECOND:mc_idf aff EXPARITH 
 ;

 LIST_INST:  AFFEC LIST_INST | InstIF LIST_INST | InstFOR LIST_INST |
 ; 
 InstIF: mc_if arc CONDITION farc debut LIST_INST fin 
 ;
  InstFOR: mc_for arc AFFECOND vg CONDITION vg Compteur farc debut LIST_INST fin 
 ;
 CONDITION:CONST mc_rat CONST | mc_bool | EXPARITH mc_rat EXPARITH
 ;
 Compteur: mc_idf incr| mc_idf decr

 ;

 %%

 int yyerror(char *msg) {
    printf("ERREUR syntaxique a la ligne %d" ,nb_ligne);
   return 0;
  }





 int main () { 
 yyparse();

 return 0; } 

请帮助我正确编译它,我需​​要它。

gcc bison lex
1个回答
0
投票

如果您的问题中显示的代码大致正确,那么输出的是

erreur

不是

error

在寻求帮助时,您提供的信息无懈可击,这一点非常重要。问题中显示的代码,提供的输入和输出必须与原始代码,输入和输出完全相同。如果没有,那么一切都是猜测。

如果在打印erreur的行中还打印了匹配的字符(在yytext中,则可以获得更好的调试信息。这样可以使您更清楚地了解导致错误报告的原因。

问题在于整行都由模式为.+的模式{ printf("erreur");进行匹配。请记住,flex始终选择longest match,因此将报告此匹配项。

但是即使它是固定的,变量名a似乎也不匹配任何模式,因为idf模式要求标识符以大写字母开头。如果您解决了这个问题,您会发现关键字不匹配,因为idf模式出现在文件的前面(请参见上面的链接)。

© www.soinside.com 2019 - 2024. All rights reserved.