882 yy_pop_state(); |
882 yy_pop_state(); |
883 /* now process the new file... */ |
883 /* now process the new file... */ |
884 } |
884 } |
885 |
885 |
886 |
886 |
887 <<EOF>> { /* NOTE: We must not change the value of include_stack_ptr |
887 <<EOF>> { /* NOTE: Currently bison is incorrectly using END_OF_INPUT in many rules |
888 * just yet. We must only decrement it if we are NOT |
888 * when checking for syntax errors in the input source code. |
889 * at the end of the main file. |
889 * This means that in reality flex will be asked to carry on reading the input |
890 * If we have finished parsing the main file, then we |
890 * even after it has reached the end of all (including the main) input files. |
891 * must leave include_stack_ptr at 0, in case the |
891 * In other owrds, we will be called to return more tokens, even after we have |
892 * parser is called once again with a new file. |
892 * already returned an END_OF_INPUT token. In this case, we must carry on returning |
893 * (In fact, we currently do just that!) |
893 * more END_OF_INPUT tokens. |
|
894 * |
|
895 * However, in the above case we will be asked to carry on reading more tokens |
|
896 * from the main input file, after we have reached the end. For this to work |
|
897 * correctly, we cannot close the main input file! |
|
898 * |
|
899 * This is why we WILL be called with include_stack_ptr == 0 multiple times, |
|
900 * and why we must handle it as a special case |
|
901 * that leaves the include_stack_ptr unchanged, and returns END_OF_INPUT once again. |
|
902 * |
|
903 * As a corollory, flex can never safely close the main input file, and we must ask |
|
904 * bison to close it! |
894 */ |
905 */ |
895 fclose(yyin); |
|
896 free(current_tracking); |
|
897 if (include_stack_ptr == 0) { |
906 if (include_stack_ptr == 0) { |
|
907 // fclose(yyin); // Must not do this!! |
|
908 // free(current_tracking); // Must not do this!! |
898 /* yyterminate() terminates the scanner and returns a 0 to the |
909 /* yyterminate() terminates the scanner and returns a 0 to the |
899 * scanner's caller, indicating "all done". |
910 * scanner's caller, indicating "all done". |
900 * |
911 * |
901 * Our syntax parser (written with bison) has the token |
912 * Our syntax parser (written with bison) has the token |
902 * END_OF_INPUT associated to the value 0, so even though |
913 * END_OF_INPUT associated to the value 0, so even though |
903 * we don't explicitly return the token END_OF_INPUT |
914 * we don't explicitly return the token END_OF_INPUT |
904 * calling yyterminate() is equivalent to doing that. |
915 * calling yyterminate() is equivalent to doing that. |
905 */ |
916 */ |
906 yyterminate(); |
917 yyterminate(); |
907 } else { |
918 } else { |
|
919 fclose(yyin); |
|
920 free(current_tracking); |
908 --include_stack_ptr; |
921 --include_stack_ptr; |
909 yy_delete_buffer(YY_CURRENT_BUFFER); |
922 yy_delete_buffer(YY_CURRENT_BUFFER); |
910 yy_switch_to_buffer((include_stack[include_stack_ptr]).buffer_state); |
923 yy_switch_to_buffer((include_stack[include_stack_ptr]).buffer_state); |
911 current_tracking = include_stack[include_stack_ptr].env; |
924 current_tracking = include_stack[include_stack_ptr].env; |
912 /* removing constness of char *. This is safe actually, |
925 /* removing constness of char *. This is safe actually, |
1862 |
1875 |
1863 |
1876 |
1864 /* Tell flex which file to parse. This function will not imediately start parsing the file. |
1877 /* Tell flex which file to parse. This function will not imediately start parsing the file. |
1865 * To parse the file, you then need to call yyparse() |
1878 * To parse the file, you then need to call yyparse() |
1866 * |
1879 * |
1867 * Returns -1 on error opening the file (and a valid errno), or 0 on success. |
1880 * Returns NULL on error opening the file (and a valid errno), or 0 on success. |
1868 */ |
1881 * Caller must close the file! |
1869 int parse_file(const char *filename) { |
1882 */ |
|
1883 FILE *parse_file(const char *filename) { |
1870 FILE *filehandle = NULL; |
1884 FILE *filehandle = NULL; |
1871 |
1885 |
1872 if((filehandle = fopen(filename, "r")) == NULL) |
1886 if((filehandle = fopen(filename, "r")) != NULL) { |
1873 return -1; |
1887 yyin = filehandle; |
1874 |
1888 current_filename = strdup(filename); |
1875 yyin = filehandle; |
1889 current_tracking = GetNewTracking(yyin); |
1876 current_filename = strdup(filename); |
1890 } |
1877 current_tracking = GetNewTracking(yyin); |
1891 return filehandle; |
1878 return 0; |
|
1879 } |
1892 } |
1880 |
1893 |
1881 |
1894 |
1882 |
1895 |
1883 |
1896 |