Imported Upstream version 20200910
[platform/upstream/byacc.git] / reader.c
index 78f81e1..5a9bd38 100644 (file)
--- a/reader.c
+++ b/reader.c
@@ -1,11 +1,11 @@
-/* $Id: reader.c,v 1.77 2019/11/04 01:49:56 tom Exp $ */
+/* $Id: reader.c,v 1.84 2020/09/10 20:26:13 tom Exp $ */
 
 #include "defs.h"
 
 /*  The line size must be a positive integer.  One hundred was chosen  */
 /*  because few lines in Yacc input grammars exceed 100 characters.    */
 /*  Note that if a line exceeds LINESIZE characters, the line buffer   */
-/*  will be expanded to accomodate it.                                 */
+/*  will be expanded to accommodate it.                                        */
 
 #define LINESIZE 100
 
@@ -185,7 +185,7 @@ line_directive(void)
            else
                UNLESS(!isdigit(UCH(ch)));
            line_1st = n;
-           ld = ldNUM;
+           ld = ldNUM;         /* this is needed, but cppcheck says no... */
            /* FALLTHRU */
        case ldNUM:
            if (isdigit(UCH(ch)))
@@ -261,11 +261,12 @@ static void
 get_line(void)
 {
     FILE *f = input_file;
-    int c;
-    int i;
 
     do
     {
+       int c;
+       int i;
+
        if (saw_eof || (c = getc(f)) == EOF)
        {
            if (line)
@@ -457,7 +458,7 @@ keywords[] = {
     { "error-verbose",ERROR_VERBOSE },
     { "expect",      EXPECT },
     { "expect-rr",   EXPECT_RR },
-    { "ident",       IDENT }, 
+    { "ident",       IDENT },
 #if defined(YYBTYACC)
     { "initial-action", INITIAL_ACTION },
 #endif
@@ -469,11 +470,11 @@ keywords[] = {
     { "nonassoc",    NONASSOC },
     { "parse-param", PARSE_PARAM },
     { "pure-parser", PURE_PARSER },
-    { "right",       RIGHT }, 
+    { "right",       RIGHT },
     { "start",       START },
     { "term",        TOKEN },
     { "token",       TOKEN },
-    { "token-table", TOKEN_TABLE }, 
+    { "token-table", TOKEN_TABLE },
     { "type",        TYPE },
     { "union",       UNION },
     { "yacc",        POSIX_YACC },
@@ -493,11 +494,12 @@ keyword(void)
 {
     int c;
     char *t_cptr = cptr;
-    struct keyword *key;
 
     c = *++cptr;
     if (isalpha(UCH(c)))
     {
+       struct keyword *key;
+
        cinc = 0;
        for (;;)
        {
@@ -587,7 +589,6 @@ static char *
 copy_string(int quote)
 {
     struct mstring *temp = msnew();
-    int c;
     struct ainfo a;
     a.a_lineno = lineno;
     a.a_line = dup_line();
@@ -595,7 +596,8 @@ copy_string(int quote)
 
     for (;;)
     {
-       c = *cptr++;
+       int c = *cptr++;
+
        mputc(temp, c);
        if (c == quote)
        {
@@ -686,7 +688,8 @@ copy_code(void)
 {
     int c;
     int curl;
-    int cline = 0;
+    int cline;
+    int on_line = 0;
     int pos = CODE_HEADER;
     struct mstring *code_mstr;
 
@@ -725,7 +728,7 @@ copy_code(void)
            if (pos == -1 || !check_key(pos))
            {
                syntax_error(lineno, line, cptr);
-               return;
+               /*NOTREACHED */
            }
        }
     }
@@ -736,9 +739,17 @@ copy_code(void)
     curl = 1;                  /* nesting count */
 
     /* gather text */
-    code_mstr = msnew();
+    code_lines[pos].name = code_keys[pos];
+    if ((cline = (int)code_lines[pos].num) != 0)
+    {
+       code_mstr = msrenew(code_lines[pos].lines);
+    }
+    else
+    {
+       code_mstr = msnew();
+    }
     cline++;
-    msprintf(code_mstr, "/* %%code %s block start */\n", code_keys[pos]);
+    msprintf(code_mstr, line_format, lineno, input_file_name);
     for (;;)
     {
        c = *cptr++;
@@ -749,11 +760,12 @@ copy_code(void)
            if (line == NULL)
            {
                unexpected_EOF();
-               return;
+               /*NOTREACHED */
            }
            continue;
        case '\n':
            cline++;
+           on_line = 0;
            break;
        case L_CURL:
            curl++;
@@ -761,9 +773,11 @@ copy_code(void)
        case R_CURL:
            if (--curl == 0)
            {
-               cline++;
-               msprintf(code_mstr, "/* %%code %s block end */\n",
-                        code_keys[pos]);
+               if (on_line > 1)
+               {
+                   mputc(code_mstr, '\n');
+                   cline++;
+               }
                code_lines[pos].lines = msdone(code_mstr);
                code_lines[pos].num = (size_t) cline;
                return;
@@ -773,6 +787,7 @@ copy_code(void)
            break;
        }
        mputc(code_mstr, c);
+       on_line++;
     }
 }
 
@@ -1129,17 +1144,21 @@ copy_param(int k)
        {
            buf_size = (size_t) linesize;
            buf = TMALLOC(char, buf_size);
+           NO_SPACE(buf);
        }
        else if (c == '\n')
        {
+           char *tmp;
+
            get_line();
            if (line == NULL)
                unexpected_EOF();
            --cptr;
            buf_size += (size_t) linesize;
-           buf = TREALLOC(char, buf, buf_size);
+           tmp = TREALLOC(char, buf, buf_size);
+           NO_SPACE(tmp);
+           buf = tmp;
        }
-       NO_SPACE(buf);
        if (curly)
        {
            if ((state == 2) && (c == L_CURL))
@@ -1190,13 +1209,21 @@ copy_param(int k)
        if (parms[i] == ']')
        {
            int level = 1;
-           while (i >= 0 && level > 0 && parms[i] != '[')
+           while (i >= 0)
            {
-               if (parms[i] == ']')
+               char ch = parms[i--];
+               if (ch == ']')
+               {
                    ++level;
-               else if (parms[i] == '[')
-                   --level;
-               i--;
+               }
+               else if (ch == '[')
+               {
+                   if (--level <= 1)
+                   {
+                       ++i;
+                       break;
+                   }
+               }
            }
            if (i <= 0)
                unexpected_EOF();
@@ -1420,8 +1447,6 @@ get_literal(void)
 static int
 is_reserved(char *name)
 {
-    char *s;
-
     if (strcmp(name, ".") == 0 ||
        strcmp(name, "$accept") == 0 ||
        strcmp(name, "$end") == 0)
@@ -1429,7 +1454,8 @@ is_reserved(char *name)
 
     if (name[0] == '$' && name[1] == '$' && isdigit(UCH(name[2])))
     {
-       s = name + 3;
+       char *s = name + 3;
+
        while (isdigit(UCH(*s)))
            ++s;
        if (*s == NUL)
@@ -1677,14 +1703,14 @@ static void
 declare_argtypes(bucket *bp)
 {
     char *tags[MAXARGS];
-    int args = 0, c;
+    int args = 0;
 
     if (bp->args >= 0)
        retyped_warning(bp->name);
     cptr++;                    /* skip open paren */
     for (;;)
     {
-       c = nextc();
+       int c = nextc();
        if (c == EOF)
            unexpected_EOF();
        if (c != '<')
@@ -1779,15 +1805,15 @@ declare_start(void)
 static void
 read_declarations(void)
 {
-    int c, k;
-
     cache_size = CACHE_SIZE;
     cache = TMALLOC(char, cache_size);
     NO_SPACE(cache);
 
     for (;;)
     {
-       c = nextc();
+       int k;
+       int c = nextc();
+
        if (c == EOF)
            unexpected_EOF();
        if (c != '%')
@@ -2114,7 +2140,7 @@ compile_arg(char **theptr, char *yyvaltag)
 {
     char *p = *theptr;
     struct mstring *c = msnew();
-    int i, j, n;
+    int i, n;
     Value_t *offsets = NULL, maxoffset;
     bucket **rhs;
 
@@ -2128,6 +2154,8 @@ compile_arg(char **theptr, char *yyvaltag)
     }
     if (maxoffset > 0)
     {
+       int j;
+
        offsets = TMALLOC(Value_t, maxoffset + 1);
        NO_SPACE(offsets);
 
@@ -2217,7 +2245,7 @@ can_elide_arg(char **theptr, char *yyvaltag)
 {
     char *p = *theptr;
     int rv = 0;
-    int i, j, n = 0;
+    int i, n = 0;
     Value_t *offsets = NULL, maxoffset = 0;
     bucket **rhs;
     char *tag = 0;
@@ -2237,6 +2265,8 @@ can_elide_arg(char **theptr, char *yyvaltag)
     }
     if (maxoffset > 0)
     {
+       int j;
+
        offsets = TMALLOC(Value_t, maxoffset + 1);
        NO_SPACE(offsets);
 
@@ -2357,7 +2387,6 @@ advance_to_start(void)
 {
     int c;
     bucket *bp;
-    char *s_cptr;
     int s_lineno;
 #if defined(YYBTYACC)
     char *args = NULL;
@@ -2366,6 +2395,8 @@ advance_to_start(void)
 
     for (;;)
     {
+       char *s_cptr;
+
        c = nextc();
        if (c != '%')
            break;
@@ -2444,12 +2475,12 @@ start_rule(bucket *bp, int s_lineno)
 static void
 end_rule(void)
 {
-    int i;
-
     if (!last_was_action && plhs[nrules]->tag)
     {
        if (pitem[nitems - 1])
        {
+           int i;
+
            for (i = nitems - 1; (i > 0) && pitem[i]; --i)
                continue;
            if (pitem[i + 1] == 0 || pitem[i + 1]->tag != plhs[nrules]->tag)
@@ -3137,7 +3168,6 @@ static void
 copy_destructor(void)
 {
     char *code_text;
-    int c;
     struct ainfo a;
     bucket *bp;
 
@@ -3145,7 +3175,7 @@ copy_destructor(void)
 
     for (;;)
     {
-       c = nextc();
+       int c = nextc();
        if (c == EOF)
            unexpected_EOF();
        if (c == '<')
@@ -3349,14 +3379,13 @@ mark_symbol(void)
 static void
 read_grammar(void)
 {
-    int c;
-
     initialize_grammar();
     advance_to_start();
 
     for (;;)
     {
-       c = nextc();
+       int c = nextc();
+
        if (c == EOF)
            break;
        if (isalpha(UCH(c))
@@ -3413,7 +3442,8 @@ static void
 pack_names(void)
 {
     bucket *bp;
-    char *p, *s, *t;
+    char *p;
+    char *t;
 
     name_pool_size = 13;       /* 13 == sizeof("$end") + sizeof("$accept") */
     for (bp = first_symbol; bp; bp = bp->next)
@@ -3427,8 +3457,9 @@ pack_names(void)
     t = name_pool + 13;
     for (bp = first_symbol; bp; bp = bp->next)
     {
+       char *s = bp->name;
+
        p = t;
-       s = bp->name;
        while ((*t++ = *s++) != 0)
            continue;
        FREE(bp->name);
@@ -3457,14 +3488,14 @@ check_symbols(void)
 static void
 protect_string(char *src, char **des)
 {
-    unsigned len;
-    char *s;
-    char *d;
-
     *des = src;
     if (src)
     {
-       len = 1;
+       char *s;
+       char *d;
+
+       unsigned len = 1;
+
        s = src;
        while (*s)
        {
@@ -3675,8 +3706,6 @@ pack_grammar(void)
 {
     int i;
     Value_t j;
-    Assoc_t assoc;
-    Value_t prec2;
 
     ritem = TMALLOC(Value_t, nitems);
     NO_SPACE(ritem);
@@ -3707,6 +3736,9 @@ pack_grammar(void)
     j = 4;
     for (i = 3; i < nrules; ++i)
     {
+       Assoc_t assoc;
+       Value_t prec2;
+
 #if defined(YYBTYACC)
        if (plhs[i]->args > 0)
        {
@@ -3798,11 +3830,11 @@ finalize_destructors(void)
 {
     int i;
     bucket *bp;
-    char *tag;
 
     for (i = 2; i < nsyms; ++i)
     {
-       tag = symbol_type_tag[i];
+       char *tag = symbol_type_tag[i];
+
        if (symbol_destructor[i] == NULL)
        {
            if (tag == NULL)