Flex和Bison中数字常量之前的预期非限定id

时间:2016-04-21 11:09:33

标签: data-structures bison flex-lexer return-type

我正在尝试将结构从Flex文件返回到Bison文件但是我在编译时遇到了一些错误 我正在解析以下文件:

import groovy.io.FileType
import java.io.File;
def list = []
def dir = new File("C:\\Users\\Desktop\\CodeTest")

    dir.eachFileRecurse (FileType.FILES) 
         { 
            file ->list << file
         }
    list.each 
        {
            println it.path
        }
//Replace the pattern in file and write to file sequentially.
def replacePatternInFile(file, Closure replaceText)
    {
            file.write(replaceText(file.text))
    }   
def file = new File(file)
def patternToFind1 = ~/</
def patternToFind2 = ~/>/
def patternToReplace1 = '&lt'
def patternToReplace2 = '&gt'

//Call the method
replacePatternInFile(file){
    it.replaceAll(patternToFind1,patternToReplace1) 
}

replacePatternInFile(file){
    it.replaceAll(patternToFind2,patternToReplace2) 
}

println file.getText()

为此,我写了两个文件:第一个是Flex,第二个是Bison。

final.l

**********************************************************************
* Main Circuit Netlist:
*
* Block: SPSMHD_BB_1024x8m4_aTlmr
*
**********************************************************************

.subckt SPSMHD_BB_1024x8m4_aTlmr 
+ A<6> A<5> A<4> A<3> A<2> A<1> A<0> ATP CK 
+ CSN D<7> D<6> D<5> D<4> D<3> D<2> D<1> D<0> IG INITN Q<7> Q<6> Q<5> Q<4> Q<3> 
+ Q<2> Q<1> Q<0> RRA<5> RRA<4> RRA<3> RRA<2> RRA<1> RRA<0> RRAE RRMATCH SCTRLI 
+ SCTRLO SDLI SDLO SDRI SDRO SE STDBY TA<6> TA<5> TA<4> TA<3> TA<2> TA<1> TA<0> 
+ TBIST TBYPASS TCSN TED TOD TRRAE TWEN WEN gndm vddm gndsm vddsm 
DDSMRI gndm gndm tdndsx 15.222f perim=526.0n 
DDSDRI gndm SDRI tdndsx 15.222f perim=526.0n 
XITOP A<6> A<5> A<4> A<3> A<2> A<1> A<0> ATP CK CSN D<7> D<6> D<5> D<4> D<3> 
+ D<2> D<1> D<0> gndm IG INITN gndm gndm gndm gndm gndm gndm gndm gndm gndm Q<7> 
+ Q<6> Q<5> Q<4> Q<3> Q<2> Q<1> Q<0> gndm gndm gndm RRA<0> RRA<1> RRA<2> RRA<3> 
+ RRA<4> RRA<5> RRAE RRMATCH SCTRLI SCTRLO SDLI SDLO SDRI SDRO SE gndm gndm SMLO 
+ gndm SMRO STDBY gndm TA<6> TA<5> TA<4> TA<3> TA<2> TA<1> TA<0> TBIST TBYPASS 
+ TCSN TED TEDO gndm TEMO TOD TODO gndm TOMO TORS TRRAE TWEN WEN gndm gndm gndm 
+ gndsm vddm vddmo vddm vddsm 
+ SPHDDR_TOPLEVEL_112x8m2B1_aTRST_SPSMHD_BB_1024x8m4_aTlmr 
.ends

sr.y

%{
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "sr.tab.h"

    //typedef myStruct m;


    myStruct *a_head = NULL;
    myStruct *a_tail = NULL;

    #define YY_DECL extern "C" int yylex()
    int line_num = 1;
    int flag = 0;
    int i = 0;
    using namespace std;

%}



DOT             "."
COLON           ":"
SEMICOLON       ";"
COMMA           ","
ANGLE_LEFT      "<"
ANGLE_RIGHT     ">"
AT              "@"
EQUAL           "="
SQUARE_OPEN     "["
SQUARE_CLOSE    [^\\]"]"
OPENBRACE       "\("
CLOSEBRACE      "\)"
QUOTE           "\""
QUOTE_OPEN      "\""
QUOTE_CLOSE     [^\\]"\""
SPACE           " "
TAB             "\t"
CRLF            "\r\n"
QUOTED_PAIR     "\\"[^\r\n]
DIGIT           [0-9]
ALPHA           [a-z+]
QTEXT           [0-9a-zA-Z!#$%&&'()*+,\-.\/:;<=>?@\[\]^_`{|}~""]
STAR        [*]


%option case-insensitive

%%

[ \t]       ;

"X".*       { flag = 0;

              yylval.sval = strdup(yytext);
      return TOK_X;  }


"M".*       {yylval.sval = strdup(yytext);
      return TOK_M; }






{ALPHA}*      {yylval.sval = strdup(yytext);
      return TOK_PINS;}

.SUBCKT           {yylval.sval = strdup(yytext);
         return TOK_SUBCKT;}


.ends        {yylval.sval = strdup(yytext);
      return TOK_Ends;}

"*".*       { cout << "Comment: " << yytext << endl; }


{QTEXT}*    {    flag = 1;
                yylval.sval = strdup(yytext);
      return TOK_MASTERNAME;


              }

"D".*       {   flag = 0;
                yylval.sval = strdup(yytext);
        return TOK_D; 
            }

"R".*       {
                 yylval.sval = strdup(yytext);
         return TOK_R; 
            }


"+".*       {  if( flag == 1)
                    {
                            if ( i == 0)
                            {
                                a_head=(struct myStruct*)malloc(sizeof(myStruct));
                                a_tail=a_head;
                                //yylval.sval = strdup(yytext);
                                a_head->data = yytext;
                                a_head->next = NULL;
                                i = 1;
                            }
                            else
                            {
                                 a_tail->next=(struct myStruct*)malloc(sizeof(myStruct));
                                //yylval.sval = strdup(yytext);
                                a_tail->next->data = yytext;
                                a_tail->next->next = NULL;
                                a_tail=a_tail->next;
                             }
                        }
                        yylval.myStruct=a_head;
                        return a;
            }



\\\n            { printf("c> "); }


"//".*      /* ignore comments */

^{CRLF}                         { 
                                return TOK_EMPTY_LINE;
                            }

{CRLF}                          {}
.                               {}/* ignore unknown chars */
\n                      { ++line_num; /*RETURN(ENDL); */ }

编译如何完成以下步骤:

%{
#include <cstdio> 
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

#define YYDEBUG 1

using namespace std;

extern "C" int yylex();
extern "C" FILE *yyin;
extern int line_num;


void yyerror(const char* s);

struct myStruct{
    char* data;
    struct myStruct *next;
};

typedef struct myStruct myStruct;


struct ast_node *new_ast_node ( char *root_node , myStruct *ports );




struct ast_node {
char* root_node;
    myStruct* ports;
};

%}




// Symbols.
%union
{
  struct ast_node * ast;
  char* sval;
  int* ival;
  struct myStruct *myStruct;
};

%token <myStruct> a_head
%token <sval> TOK_X
%token <sval> TOK_PINS
%token <sval> TOK_M
%token <sval> TOK_D
%token <sval> TOK_R
%token <sval> TOK_STRAP
%token <sval> TOK_WRAP
%token <sval> TOK_VIA
%token <sval> TOK_EMPTY_LINE 
%token <sval> TOK_BLOCK
%token <sval> TOK_LINE
%token <sval> TOK_BEGIN
%token <sval> TOK_END
%token <sval> TOK_VERSION
%token <sval> TOK_STRUCT
%token <sval> TOK_UNIQUE
%token <sval> TOK_REF
%token <sval> TOK_POS
%token <sval> TOK_CON
%token <sval> TOK_SUBCKT
%token <sval> TOK_MASTERNAME
%token <sval> TOK_PORTS
%token <sval> TOK_Ends
%token END ENDL


%type <ast> language program block PortsContent PORTS XContents item 

%%

language : program ;

program : block 
| program block
;


block : TOK_SUBCKT TOK_MASTERNAME a XContents   { 
                                                                $$ = new_ast_node ($2,$3);

                                                    }                                   
| XContents 
| TOK_Ends
;


PortsContent : PORTS     
     | PortsContent PORTS
     ;


PORTS   : TOK_PORTS 
;


XContents : XContents PORTS
   | item
   ;


item :      TOK_X       
 |  TOK_M 
 |  TOK_D 
 |  TOK_R 
    ;

%%

//struct ast_node*
//new_ast_node ( char *root_node , char *ports )
//{                                                                    
//struct ast_node * ast_node = (struct ast_node*)malloc (sizeof (struct ast_node));

//ast_node->root_node = root_node;
//ast_node->ports = ports;

//  printf("Root node: %s\n",ast_node->root_node );
//  printf("Ports: %s\n",ast_node->ports );
//  return ast_node;                            
//}


int main(int argc, char *argv[]) 
{
  FILE* inputF; 
  inputF = fopen(argv[1], "r");
  if(!inputF)
  {
 cout << "Bad Input.Noexistant file" << endl;
 return -1;
  }
  yyin = inputF;
  do
  {
//yydebug = 1;
    yyparse();
   }while (!feof(yyin));      
}

void yyerror(const char *s) 
{
cout << "parse error on line " << line_num << "!  Message: " << s << endl;
exit(-1);
}

extern "C" int yywrap()
{
        return (1 == 1);
}
#include "lex.yy.c"

错误:

flex final.l
bison -d sr.y
g++ sr.tab.c -lfl -o scanner.exe
./scanner file

0 个答案:

没有答案