bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 4/5] reader: reduce the "scope" of global variables


From: Akim Demaille
Subject: [PATCH 4/5] reader: reduce the "scope" of global variables
Date: Sat, 26 Oct 2019 16:44:42 +0200

We have too many global variables, adding structure would help.  For a
start, let's hide some of the variables closer to their usage.

* src/getargs.c, src/files.h (current_file): Move to...
* src/scan-gram.c: here.
* src/scan-gram.h (gram_in, gram__flex_debug): Remove, make them
private to the scanner.
* src/reader.h, src/reader.c (reader): Take a grammar file as argument.
Move the handling of scanner variables to...
* src/scan-gram.l (gram_scanner_open, gram_scanner_close): here.
(gram_scanner_initialize): Remove, replaced by gram_scanner_open.
* src/main.c: Adjust.
---
 src/files.c     |  1 -
 src/files.h     |  3 ---
 src/getargs.c   |  2 +-
 src/main.c      |  2 +-
 src/reader.c    | 12 ++++--------
 src/reader.h    |  2 +-
 src/scan-gram.h | 15 ++++++---------
 src/scan-gram.l | 27 +++++++++++++++++----------
 8 files changed, 30 insertions(+), 34 deletions(-)

diff --git a/src/files.c b/src/files.c
index 6db33050..66000ba3 100644
--- a/src/files.c
+++ b/src/files.c
@@ -68,7 +68,6 @@ static generated_file *generated_files = NULL;
 static int generated_files_size = 0;
 
 uniqstr grammar_file = NULL;
-uniqstr current_file = NULL;
 
 /* If --output=dir/foo.c was specified,
    DIR_PREFIX is 'dir/' and ALL_BUT_EXT and ALL_BUT_TAB_EXT are 'dir/foo'.
diff --git a/src/files.h b/src/files.h
index 901b1b6c..5722cc75 100644
--- a/src/files.h
+++ b/src/files.h
@@ -58,9 +58,6 @@ extern char *dir_prefix;
    and therefore GCC warns about a name clash. */
 extern uniqstr grammar_file;
 
-/* The current file name.  Might change with #line.  */
-extern uniqstr current_file;
-
 /* The computed base for output file names.  */
 extern char *all_but_ext;
 
diff --git a/src/getargs.c b/src/getargs.c
index 815df331..bf4a938f 100644
--- a/src/getargs.c
+++ b/src/getargs.c
@@ -853,7 +853,7 @@ getargs (int argc, char *argv[])
       usage (EXIT_FAILURE);
     }
 
-  current_file = grammar_file = uniqstr_new (argv[optind]);
+  grammar_file = uniqstr_new (argv[optind]);
   MUSCLE_INSERT_C_STRING ("file_name", grammar_file);
 }
 
diff --git a/src/main.c b/src/main.c
index ab0229f5..258a6d1e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -101,7 +101,7 @@ main (int argc, char *argv[])
      the grammar; see gram.h.  */
 
   timevar_push (tv_reader);
-  reader ();
+  reader (grammar_file);
   timevar_pop (tv_reader);
 
   if (complaint_status == status_complaint)
diff --git a/src/reader.c b/src/reader.c
index 0a428234..cf481fd8 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -710,24 +710,20 @@ packgram (void)
 `------------------------------------------------------------------*/
 
 void
-reader (void)
+reader (const char *gram)
 {
   /* Set up symbol_table, semantic_type_table, and the built-in
      symbols.  */
   symbols_new ();
 
-  gram_in = xfopen (grammar_file, "r");
-
-  gram__flex_debug = trace_flag & trace_scan;
-  gram_debug = trace_flag & trace_parse;
-  gram_scanner_initialize ();
+  gram_scanner_open (gram);
   gram_parse ();
+  gram_scanner_close ();
+
   prepare_percent_define_front_end_variables ();
 
   if (complaint_status  < status_complaint)
     check_and_convert_grammar ();
-
-  xfclose (gram_in);
 }
 
 static void
diff --git a/src/reader.h b/src/reader.h
index 0171af29..85380155 100644
--- a/src/reader.h
+++ b/src/reader.h
@@ -60,7 +60,7 @@ void grammar_current_rule_action_append (const char *action, 
location loc,
                                          named_ref *nref, uniqstr tag);
 /* Attach a PREDICATE to the current rule.  */
 void grammar_current_rule_predicate_append (const char *predicate, location 
loc);
-void reader (void);
+void reader (const char *gram);
 void free_merger_functions (void);
 
 extern merger_list *merge_functions;
diff --git a/src/scan-gram.h b/src/scan-gram.h
index f25d183f..0b16e76c 100644
--- a/src/scan-gram.h
+++ b/src/scan-gram.h
@@ -21,18 +21,15 @@
 #ifndef SCAN_GRAM_H_
 # define SCAN_GRAM_H_
 
-/* From the scanner.  */
-extern FILE *gram_in;
-extern int gram__flex_debug;
-void gram_scanner_initialize (void);
+/* Initialize the scanner to read file GRAM. */
+void gram_scanner_open (const char *gram);
+/* Close the open files.  */
+void gram_scanner_close (void);
+
+/* Free all the memory allocated to the scanner. */
 void gram_scanner_free (void);
 void gram_scanner_last_string_free (void);
 
-/* These are declared by the scanner, but not used.  We put them here
-   to pacify "make syntax-check".  */
-extern FILE *gram_out;
-extern int gram_lineno;
-
 # define GRAM_LEX_DECL int gram_lex (GRAM_STYPE *val, location *loc)
 GRAM_LEX_DECL;
 
diff --git a/src/scan-gram.l b/src/scan-gram.l
index 7667648d..7fc23ab8 100644
--- a/src/scan-gram.l
+++ b/src/scan-gram.l
@@ -83,6 +83,9 @@ static boundary scanner_cursor;
       unput (Msg[i - 1]);                                       \
   } while (0)
 
+/* The current file name.  Might change with #line.  */
+static uniqstr current_file = NULL;
+
 /* A string representing the most recently saved token.  */
 static char *last_string = NULL;
 
@@ -971,25 +974,29 @@ unexpected_newline (boundary start, char const *token_end)
 }
 
 
-/*-------------------------.
-| Initialize the scanner.  |
-`-------------------------*/
-
 void
-gram_scanner_initialize (void)
+gram_scanner_open (const char *gram)
 {
+  gram__flex_debug = trace_flag & trace_scan;
+  gram_debug = trace_flag & trace_parse;
   obstack_init (&obstack_for_string);
+  current_file = gram;
+  gram_in = xfopen (gram, "r");
+}
+
+
+void
+gram_scanner_close ()
+{
+  xfclose (gram_in);
+  /* Reclaim Flex's buffers.  */
+  yylex_destroy ();
 }
 
 
-/*-----------------------------------------------.
-| Free all the memory allocated to the scanner.  |
-`-----------------------------------------------*/
 
 void
 gram_scanner_free (void)
 {
   obstack_free (&obstack_for_string, 0);
-  /* Reclaim Flex's buffers.  */
-  yylex_destroy ();
 }
-- 
2.23.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]