How to declare a global std::list in bison

Hello,
I have a Bison code.
I want to declare a std::list<Circuit*> circuit_lists to be global so that I can add to this list once a certain grammar is found. But I always get an error when compiling "parser.y:28:1: error: ‘Circuit_lists’ does not name a type"

Here is a sample of the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
%debug

%{
/*--------------This part is normal C++ code  that would appear before the Bison code-----*/


#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <stdexcept>


#include "Circuit.h"


#include <list>
#include <vector>

//declare the list of crcuit classes to add the elements to it
//this is also usefull when we add subcircuits. The first element in this list is always the main circtui

std::list<Circuit*>* Circuit_lists;
Circuit_lists->push_back(new Circuit); //I always get the error here
Circuit* MainCircuit = Circuit_lists->front();

extern "C" FILE *yyin;

void yyerror(const char *str)
{
        fprintf(stderr,"error: %s\n",str);
}
  
int yylex(void);

extern "C"
{
        int yyparse(void);
        int yywrap()
        {
                return 1;
        }
	int yylinno;
}

%}

%%
//Here I put my grammar
subcircuit_statment:
	|SUBCKT STRING node_list{
	  circuit_lists.push_back(new Circuit);
	}
	;
%%
int main(int argc, char** argv)
{

int yydebug = 0;
	
	FILE *file;
	file = fopen(argv[1],"r");
	if(!file) throw std::runtime_error("Can not open the file");
		
	yyin = file;
	
        yyparse();
	
	//Start simulating the circuit
	MainCircuit->start_analysis();
	
	return 0;
} 
You can't put statements outside of functions like that. Even if you could your code would still not work because Circuit_lists is a pointer and you haven't made it point to an object yet.
Thank you,
I just made a small mistake...it is not a pointer to ta a list.
Here is the modified code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
%debug

%{
/*--------------This part is normal C++ code  that would appear before the Bison code-----*/


#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <stdexcept>


#include "Circuit.h"


#include <list>
#include <vector>

//declare the list of crcuit classes to add the elements to it
//this is also usefull when we add subcircuits. The first element in this list is always the main circtui

std::list<Circuit*> Circuit_lists;
Circuit_lists.push_back(new Circuit); //I always get the error here
Circuit* MainCircuit = Circuit_lists.front();

extern "C" FILE *yyin;

void yyerror(const char *str)
{
        fprintf(stderr,"error: %s\n",str);
}
  
int yylex(void);

extern "C"
{
        int yyparse(void);
        int yywrap()
        {
                return 1;
        }
	int yylinno;
}

%}

%%
//Here I put my grammar
subcircuit_statment:
	|SUBCKT STRING node_list{
	  circuit_lists.push_back(new Circuit);
	}
	;
%%
int main(int argc, char** argv)
{

int yydebug = 0;
	
	FILE *file;
	file = fopen(argv[1],"r");
	if(!file) throw std::runtime_error("Can not open the file");
		
	yyin = file;
	
        yyparse();
	
	//Start simulating the circuit
	MainCircuit->start_analysis();
	
	return 0;
} 
You will have to initialize Circuit_lists in the same row as it's being defined (or move the push_back to a function).

This should work in a modern compiler:
 
std::list<Circuit*> Circuit_lists{new Circuit};


Otherwise this should work:
 
std::list<Circuit*> Circuit_lists(1, new Circuit);
Topic archived. No new replies allowed.