Grammar optimization from NetBSD: Two adjacent string constants are merged.

This commit is contained in:
Arnold D. Robbins 2019-07-28 20:09:24 +03:00
parent 795a06b58c
commit c95b96020f
5 changed files with 30 additions and 2 deletions

5
FIXES
View File

@ -25,12 +25,17 @@ THIS SOFTWARE.
This file lists all bug fixes, changes, etc., made since the AWK book This file lists all bug fixes, changes, etc., made since the AWK book
was sent to the printers in August, 1987. was sent to the printers in August, 1987.
July 28, 2019:
Import grammar optimization from NetBSD: Two string constants
concatenated together get turned into a single string.
July 26, 2019: July 26, 2019:
Support POSIX-specified C-style escape sequences "\a" (alarm) Support POSIX-specified C-style escape sequences "\a" (alarm)
and "\v" (vertical tab) in command line arguments and regular and "\v" (vertical tab) in command line arguments and regular
expressions, further to the support for them in strings added on expressions, further to the support for them in strings added on
Apr 9, 1989. These now no longer match as literal "a" and "v" Apr 9, 1989. These now no longer match as literal "a" and "v"
characters (as they don't on other awk implementations). characters (as they don't on other awk implementations).
Thanks to Martijn Dekker.
July 17, 2019: July 17, 2019:
Pull in a number of code cleanups and minor fixes from Pull in a number of code cleanups and minor fixes from

View File

@ -71,6 +71,7 @@ Node *arglist = 0; /* list of args for current function */
%type <i> do st %type <i> do st
%type <i> pst opt_pst lbrace rbrace rparen comma nl opt_nl and bor %type <i> pst opt_pst lbrace rbrace rparen comma nl opt_nl and bor
%type <i> subop print %type <i> subop print
%type <cp> string
%right ASGNOP %right ASGNOP
%right '?' %right '?'
@ -348,6 +349,11 @@ subop:
SUB | GSUB SUB | GSUB
; ;
string:
STRING
| string STRING { $$ = catstr($1, $2); }
;
term: term:
term '/' ASGNOP term { $$ = op2(DIVEQ, $1, $4); } term '/' ASGNOP term { $$ = op2(DIVEQ, $1, $4); }
| term '+' term { $$ = op2(ADD, $1, $3); } | term '+' term { $$ = op2(ADD, $1, $3); }
@ -394,7 +400,7 @@ term:
| SPLIT '(' pattern comma varname ')' | SPLIT '(' pattern comma varname ')'
{ $$ = op4(SPLIT, $3, makearr($5), NIL, (Node*)STRING); } /* default */ { $$ = op4(SPLIT, $3, makearr($5), NIL, (Node*)STRING); } /* default */
| SPRINTF '(' patlist ')' { $$ = op1($1, $3); } | SPRINTF '(' patlist ')' { $$ = op1($1, $3); }
| STRING { $$ = celltonode($1, CCON); } | string { $$ = celltonode($1, CCON); }
| subop '(' reg_expr comma pattern ')' | subop '(' reg_expr comma pattern ')'
{ $$ = op4($1, NIL, (Node*)makedfa($3, 1), $5, rectonode()); } { $$ = op4($1, NIL, (Node*)makedfa($3, 1), $5, rectonode()); }
| subop '(' pattern comma pattern ')' | subop '(' pattern comma pattern ')'

2
main.c
View File

@ -22,7 +22,7 @@ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE. THIS SOFTWARE.
****************************************************************/ ****************************************************************/
const char *version = "version 20190726"; const char *version = "version 20190728";
#define DEBUG #define DEBUG
#include <stdio.h> #include <stdio.h>

View File

@ -111,6 +111,7 @@ extern char *getsval(Cell *);
extern char *getpssval(Cell *); /* for print */ extern char *getpssval(Cell *); /* for print */
extern char *tostring(const char *); extern char *tostring(const char *);
extern char *qstring(const char *, int); extern char *qstring(const char *, int);
extern Cell *catstr(Cell *, Cell *);
extern void recinit(unsigned int); extern void recinit(unsigned int);
extern void initgetrec(void); extern void initgetrec(void);

16
tran.c
View File

@ -516,6 +516,22 @@ char *tostring(const char *s) /* make a copy of string s */
return(p); return(p);
} }
Cell *catstr(Cell *a, Cell *b) /* concatenate a and b */
{
Cell *c;
char *p;
char *sa = getsval(a);
char *sb = getsval(b);
size_t l = strlen(sa) + strlen(sb) + 1;
p = malloc(l);
if (p == NULL)
FATAL("out of space concatenating %s and %s", sa, sb);
snprintf(p, l, "%s%s", sa, sb);
c = setsymtab(p, p, 0.0, CON|STR|DONTFREE, symtab);
free(p);
return c;
}
char *qstring(const char *is, int delim) /* collect string up to next delim */ char *qstring(const char *is, int delim) /* collect string up to next delim */
{ {
const char *os = is; const char *os = is;