| 1 |
|
| 2 |
/* define some general tokens */ |
| 3 |
|
| 4 |
%token MOLECULE ATOM BOND BEND TORSION POSITION MEMBERS CONSTRAINT |
| 5 |
%token COMPONENT START_INDEX DEFINED ORIENTATION |
| 6 |
|
| 7 |
/* more advanced tokens */ |
| 8 |
|
| 9 |
%union { |
| 10 |
int i_val; /* integer value */ |
| 11 |
double d_val; /* double value */ |
| 12 |
char * s_ptr; /* string pointer */ |
| 13 |
struct node_tag* node_ptr; /* pointer to the statement node tree */ |
| 14 |
struct integer_list_tag* il_ptr; /*pointer to a int_list item */ |
| 15 |
} |
| 16 |
|
| 17 |
%token <i_val> INTEGER |
| 18 |
%token <i_val> ARRAY_INDEX |
| 19 |
|
| 20 |
%token <d_val> DOUBLE |
| 21 |
|
| 22 |
%token <s_ptr> IDENTIFIER |
| 23 |
%token <s_ptr> QUOTED_STRING |
| 24 |
|
| 25 |
%type <node_ptr> stmt |
| 26 |
%type <node_ptr> stmt_list |
| 27 |
%type <node_ptr> assignment |
| 28 |
%type <node_ptr> member |
| 29 |
%type <node_ptr> constraint |
| 30 |
%type <node_ptr> orientation |
| 31 |
%type <node_ptr> position |
| 32 |
%type <node_ptr> block |
| 33 |
%type <node_ptr> molecule_block |
| 34 |
%type <node_ptr> atom_block |
| 35 |
%type <node_ptr> bond_block |
| 36 |
%type <node_ptr> bend_block |
| 37 |
%type <node_ptr> torsion_block |
| 38 |
%type <node_ptr> component_block |
| 39 |
%type <node_ptr> start_index |
| 40 |
|
| 41 |
%type <il_ptr> int_list |
| 42 |
|
| 43 |
|
| 44 |
%{ |
| 45 |
|
| 46 |
#include <stdlib.h> |
| 47 |
#include <stdio.h> |
| 48 |
#include <string.h> |
| 49 |
#include "node_list.h" |
| 50 |
#include "make_nodes.h" |
| 51 |
#include "parse_tree.h" |
| 52 |
|
| 53 |
extern int yylineno; |
| 54 |
|
| 55 |
struct filename_list{ |
| 56 |
char my_name[300]; |
| 57 |
struct filename_list* next; |
| 58 |
}; |
| 59 |
extern struct filename_list* yyfile_name; |
| 60 |
|
| 61 |
extern void change_in_file( FILE* in_file ); |
| 62 |
extern void yacc_model( char* file_name ); |
| 63 |
extern void kill_lists(void); |
| 64 |
|
| 65 |
/* top of the node list */ |
| 66 |
|
| 67 |
struct node_tag* head_node; |
| 68 |
struct node_tag* current_node; |
| 69 |
|
| 70 |
%} |
| 71 |
|
| 72 |
%% |
| 73 |
|
| 74 |
program: |
| 75 |
commands |
| 76 |
; |
| 77 |
|
| 78 |
commands: /* NULL */ |
| 79 |
| commands stmt { |
| 80 |
current_node->next_stmt = $2; |
| 81 |
$2->prev_stmt = current_node; |
| 82 |
current_node = $2; |
| 83 |
} |
| 84 |
; |
| 85 |
|
| 86 |
stmt: |
| 87 |
assignment { $$ = $1; } |
| 88 |
| member { $$ = $1; } |
| 89 |
| constraint { $$ = $1; } |
| 90 |
| orientation { $$ = $1; } |
| 91 |
| position { $$ = $1; } |
| 92 |
| start_index { $$ = $1; } |
| 93 |
| block { $$ = $1; } |
| 94 |
; |
| 95 |
|
| 96 |
assignment: |
| 97 |
IDENTIFIER '=' INTEGER ';' |
| 98 |
{ $$ = assign_i( $1, $3 ); } |
| 99 |
| IDENTIFIER '=' DOUBLE ';' |
| 100 |
{ $$ = assign_d( $1, $3 ); } |
| 101 |
| IDENTIFIER '=' IDENTIFIER ';' |
| 102 |
{ $$ = assign_s( $1, $3 ); } |
| 103 |
| IDENTIFIER '=' QUOTED_STRING ';' |
| 104 |
{ $$ = assign_s( $1, $3 ); } |
| 105 |
; |
| 106 |
|
| 107 |
member: |
| 108 |
MEMBERS '(' INTEGER ',' INTEGER ')' ';' |
| 109 |
{ $$ = members_2( $3, $5 ); } |
| 110 |
| MEMBERS '(' INTEGER ',' INTEGER ',' INTEGER ')' ';' |
| 111 |
{ $$ = members_3( $3, $5, $7 ); } |
| 112 |
| MEMBERS '(' INTEGER ',' INTEGER ',' INTEGER ',' INTEGER ')' ';' |
| 113 |
{ $$ = members_4( $3, $5, $7, $9 ); } |
| 114 |
; |
| 115 |
|
| 116 |
constraint: |
| 117 |
CONSTRAINT '(' INTEGER ')' ';' |
| 118 |
{ $$ = constraint( (double)$3 ); } |
| 119 |
| CONSTRAINT '(' DOUBLE ')' ';' |
| 120 |
{ $$ = constraint( $3 ); } |
| 121 |
; |
| 122 |
|
| 123 |
orientation: |
| 124 |
ORIENTATION '(' DOUBLE ',' DOUBLE ',' DOUBLE ')' ';' |
| 125 |
{ $$ = orientation( $3, $5, $7 ); } |
| 126 |
| ORIENTATION '(' INTEGER ',' DOUBLE ',' DOUBLE ')' ';' |
| 127 |
{ $$ = orientation( (double)$3, $5, $7 ); } |
| 128 |
| ORIENTATION '(' DOUBLE ',' INTEGER ',' DOUBLE ')' ';' |
| 129 |
{ $$ = orientation( $3, (double)$5, $7 ); } |
| 130 |
| ORIENTATION '(' DOUBLE ',' DOUBLE ',' INTEGER ')' ';' |
| 131 |
{ $$ = orientation( $3, $5, (double)$7 ); } |
| 132 |
| ORIENTATION '(' INTEGER ',' INTEGER ',' INTEGER ')' ';' |
| 133 |
{ $$ = orientation( (double)$3, (double)$5, (double)$7 ); } |
| 134 |
| ORIENTATION '(' DOUBLE ',' INTEGER ',' INTEGER ')' ';' |
| 135 |
{ $$ = orientation( $3, (double)$5, (double)$7 ); } |
| 136 |
| ORIENTATION '(' INTEGER ',' DOUBLE ',' INTEGER ')' ';' |
| 137 |
{ $$ = orientation( (double)$3, $5, (double)$7 ); } |
| 138 |
| ORIENTATION '(' INTEGER ',' INTEGER ',' DOUBLE ')' ';' |
| 139 |
{ $$ = orientation( (double)$3, (double)$5, $7 ); } |
| 140 |
; |
| 141 |
|
| 142 |
position: |
| 143 |
POSITION '(' DOUBLE ',' DOUBLE ',' DOUBLE ')' ';' |
| 144 |
{ $$ = position( $3, $5, $7 ); } |
| 145 |
| POSITION '(' INTEGER ',' DOUBLE ',' DOUBLE ')' ';' |
| 146 |
{ $$ = position( (double)$3, $5, $7 ); } |
| 147 |
| POSITION '(' DOUBLE ',' INTEGER ',' DOUBLE ')' ';' |
| 148 |
{ $$ = position( $3, (double)$5, $7 ); } |
| 149 |
| POSITION '(' DOUBLE ',' DOUBLE ',' INTEGER ')' ';' |
| 150 |
{ $$ = position( $3, $5, (double)$7 ); } |
| 151 |
| POSITION '(' INTEGER ',' INTEGER ',' INTEGER ')' ';' |
| 152 |
{ $$ = position( (double)$3, (double)$5, (double)$7 ); } |
| 153 |
| POSITION '(' DOUBLE ',' INTEGER ',' INTEGER ')' ';' |
| 154 |
{ $$ = position( $3, (double)$5, (double)$7 ); } |
| 155 |
| POSITION '(' INTEGER ',' DOUBLE ',' INTEGER ')' ';' |
| 156 |
{ $$ = position( (double)$3, $5, (double)$7 ); } |
| 157 |
| POSITION '(' INTEGER ',' INTEGER ',' DOUBLE ')' ';' |
| 158 |
{ $$ = position( (double)$3, (double)$5, $7 ); } |
| 159 |
; |
| 160 |
|
| 161 |
start_index: |
| 162 |
START_INDEX int_list ';' |
| 163 |
{ $$ = start_index( $2 ); } |
| 164 |
; |
| 165 |
|
| 166 |
block: |
| 167 |
molecule_block { $$ = $1; } |
| 168 |
| atom_block { $$ = $1; } |
| 169 |
| bond_block { $$ = $1; } |
| 170 |
| bend_block { $$ = $1; } |
| 171 |
| torsion_block { $$ = $1; } |
| 172 |
| component_block { $$ = $1; } |
| 173 |
; |
| 174 |
|
| 175 |
molecule_block: |
| 176 |
MOLECULE '{' stmt_list '}' |
| 177 |
{ $$ = molecule_blk( $3 ); } |
| 178 |
; |
| 179 |
|
| 180 |
atom_block: |
| 181 |
ATOM ARRAY_INDEX '{' stmt_list '}' |
| 182 |
{ $$ = atom_blk( $2, $4 ); } |
| 183 |
; |
| 184 |
|
| 185 |
bond_block: |
| 186 |
BOND ARRAY_INDEX '{' stmt_list '}' |
| 187 |
{ $$ = bond_blk( $2, $4 ); } |
| 188 |
; |
| 189 |
|
| 190 |
bend_block: |
| 191 |
BEND ARRAY_INDEX '{' stmt_list '}' |
| 192 |
{ $$ = bend_blk( $2, $4 ); } |
| 193 |
; |
| 194 |
|
| 195 |
torsion_block: |
| 196 |
TORSION ARRAY_INDEX '{' stmt_list '}' |
| 197 |
{ $$ = torsion_blk( $2, $4 ); } |
| 198 |
; |
| 199 |
|
| 200 |
component_block: |
| 201 |
COMPONENT '{' stmt_list '}' |
| 202 |
{ $$ = component_blk( $3 ); } |
| 203 |
; |
| 204 |
|
| 205 |
stmt_list: |
| 206 |
stmt { $$ = $1; } |
| 207 |
| stmt_list stmt { |
| 208 |
$1->next_stmt = $2; |
| 209 |
$2->prev_stmt = $1; |
| 210 |
$$ = $2; |
| 211 |
} |
| 212 |
; |
| 213 |
|
| 214 |
int_list: |
| 215 |
'<' INTEGER { $$ = il_node( $2 ); } |
| 216 |
| int_list ',' INTEGER { |
| 217 |
struct integer_list_tag* temp; |
| 218 |
temp = il_node( $3 ); |
| 219 |
$1->next = temp; |
| 220 |
temp->prev = $1; |
| 221 |
$$ = temp; |
| 222 |
} |
| 223 |
| int_list '>' { $$ = il_top( $1 ); } |
| 224 |
; |
| 225 |
|
| 226 |
%% |
| 227 |
|
| 228 |
int yyerror( char *err_msg ){ |
| 229 |
|
| 230 |
fprintf( stderr, "yacc parse error in %s at line %d: %s\n", yyfile_name->my_name, yylineno, err_msg ); |
| 231 |
exit(8); |
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
void yacc_BASS( char* file_name ){ |
| 236 |
|
| 237 |
FILE* in_file; |
| 238 |
|
| 239 |
head_node = (struct node_tag* )malloc( sizeof( node ) ); |
| 240 |
|
| 241 |
head_node->type = GLOBAL_HEAD; |
| 242 |
head_node->index =0; |
| 243 |
head_node->next_stmt = NULL; |
| 244 |
head_node->prev_stmt = NULL; |
| 245 |
head_node->stmt_list = NULL; |
| 246 |
|
| 247 |
current_node = head_node; |
| 248 |
|
| 249 |
in_file = fopen( file_name, "r" ); |
| 250 |
if( in_file == NULL ){ |
| 251 |
fprintf( stderr, "yacc error: couldn't open file =>%s\n", file_name ); |
| 252 |
exit(0); |
| 253 |
} |
| 254 |
|
| 255 |
yyfile_name = (struct filename_list*)malloc( sizeof( struct filename_list ) ); |
| 256 |
yyfile_name->next = NULL; |
| 257 |
strcpy( yyfile_name->my_name, file_name ); |
| 258 |
change_in_file( in_file ); |
| 259 |
|
| 260 |
yyparse(); |
| 261 |
fclose( in_file ); |
| 262 |
kill_lists(); |
| 263 |
|
| 264 |
pt_me( head_node ); |
| 265 |
kill_tree( head_node ); |
| 266 |
head_node = NULL; |
| 267 |
} |