Annotation of researchv8dc/cmd/awk/awk.g.y, revision 1.1.1.1

1.1       root        1: %token FIRSTTOKEN      /* must be first */
                      2: %token FATAL
                      3: %token PROGRAM PASTAT PASTAT2 XBEGIN XEND
                      4: %token NL
                      5: %token LT LE GT GE EQ NE APPEND
                      6: %token MATCH NOTMATCH
                      7: %token FINAL DOT ALL CCL NCCL CHAR OR STAR QUEST PLUS
                      8: %token ADD MINUS MULT DIVIDE MOD
                      9: %token ASSIGN ADDEQ SUBEQ MULTEQ DIVEQ MODEQ POWEQ
                     10: %token PRINT PRINTF
                     11: %token DELETE
                     12: %token IF ELSE WHILE FOR IN INTEST NEXT EXIT BREAK CONTINUE
                     13: 
                     14: %right ASGNOP
                     15: %left  BOR
                     16: %left  AND
                     17: %nonassoc RELOP MATCHOP
                     18: %left  CAT
                     19: %left  '+' '-'
                     20: %left  '*' '/' '%'
                     21: %left  NOT UMINUS
                     22: %right POWER
                     23: %left  POSTINCR PREINCR POSTDECR PREDECR INCR DECR
                     24: %left  FIELD INDIRECT SUB GSUB SUBSTR LSUBSTR INDEX CALL RETURN FUNC BLTIN
                     25: %left  SPRINTF GETLINE CLOSE
                     26: %token STRING NUMBER REGEXPR VAR ARG VARNF ARRAY SPLIT
                     27: 
                     28: %token LASTTOKEN       /* must be last */
                     29: 
                     30: %{
                     31: #include "awk.h"
                     32: yywrap() { return(1); }
                     33: #ifndef        DEBUG
                     34: #      define  PUTS(x)
                     35: #endif
                     36: Node   *beginloc = 0, *endloc = 0;
                     37: int    infunc  = 0;    /* = 1 if in arglist or body of func */
                     38: char   *curfname = 0;
                     39: Node   *arglist = 0;   /* list of args for current function */
                     40: %}
                     41: %%
                     42: 
                     43: program:
                     44:          pa_stats { if (errorflag==0)
                     45:                        winner = (Node *)stat3(PROGRAM, beginloc, $1, endloc); }
                     46:        | error    { yyclearin; bracecheck(); yyerror("bailing out"); }
                     47:        ;
                     48: 
                     49: andNL:
                     50:          AND | andNL NL
                     51:        ;
                     52: 
                     53: begin:
                     54:          XBEGIN '{' stmtlist '}'       { beginloc = (Node*) linkum(beginloc,(Node *) $3); }
                     55:        ;
                     56: 
                     57: borNL:
                     58:          BOR | borNL NL
                     59:        ;
                     60: 
                     61: commaNL:
                     62:          ',' | commaNL NL
                     63:        ;
                     64: 
                     65: compound_conditional:
                     66:          conditional borNL conditional { $$ = op2(BOR, $1, $3); }
                     67:        | conditional andNL conditional { $$ = op2(AND, $1, $3); }
                     68:        | NOT conditional               { $$ = op1(NOT, $2); }
                     69:        | '(' compound_conditional ')'  { $$ = $2; }
                     70:        ;
                     71: 
                     72: compound_pattern:
                     73:          pattern borNL pattern         { $$ = op2(BOR, $1, $3); }
                     74:        | pattern andNL pattern         { $$ = op2(AND, $1, $3); }
                     75:        | NOT pattern                   { $$ = op1(NOT, $2); }
                     76:        | '(' compound_pattern ')'      { $$ = $2; }
                     77:        ;
                     78: 
                     79: conditional:
                     80:          expr          { $$ = op2(NE,$1,valtonode(lookup("$zero&null",symtab),CCON)); }
                     81:        | compound_conditional   
                     82:        ;
                     83: 
                     84: cond_expr:
                     85:          rel_expr
                     86:        | lex_expr
                     87:        | in_expr
                     88:        ;
                     89: 
                     90: easy_expr:
                     91:          term
                     92:        | easy_expr term %prec CAT      { $$ = op2(CAT, $1, $2); }
                     93:        | var ASGNOP easy_expr          { $$ = op2($2, $1, $3); }
                     94:        ;
                     95: 
                     96: elseNL:
                     97:          ELSE | elseNL NL
                     98:        ;
                     99: 
                    100: end:
                    101:          XEND '{' stmtlist '}'         { endloc = (Node*) linkum(endloc,(Node *) $3); }
                    102:        ;
                    103: 
                    104: expr:
                    105:          easy_expr
                    106:        | getpipe
                    107:        | cond_expr
                    108:        ;
                    109: 
                    110: exprlist:
                    111:          expr
                    112:        | exprlist commaNL expr { $$ = linkum($1, $3); }
                    113:        ;
                    114: 
                    115: field:
                    116:          FIELD                 { $$ = valtonode($1, CFLD); }
                    117:        | INDIRECT term         { $$ = op1(INDIRECT, $2); }
                    118:        ;
                    119: 
                    120: for:
                    121:          FOR '(' simple_stmt ';' conditional ';' simple_stmt rparenNL stmt
                    122:                { $$ = stat4(FOR, $3, $5, $7, $9); }
                    123:        | FOR '(' simple_stmt ';'  ';' simple_stmt rparenNL stmt
                    124:                { $$ = stat4(FOR, $3, 0, $6, $8); }
                    125:        | FOR '(' varname IN varname rparenNL stmt
                    126:                { $$ = stat3(IN, $3, makearr($5), $7); }
                    127:        ;
                    128: 
                    129: func:
                    130:          FUNC funcname '(' varlist rparenNL {infunc++;} '{' stmtlist '}'
                    131:                { infunc--; curfname=0; defn($2, $4, $8); }
                    132:        ;
                    133: 
                    134: funcname:
                    135:          VAR   { setfname($1); }
                    136:        | CALL  { setfname($1); }
                    137:        ;
                    138: 
                    139: getpipe:
                    140:          easy_expr '|' GETLINE var     { $$ = op3(GETLINE, $4, $2, $1); }
                    141:        | easy_expr '|' GETLINE         { $$ = op3(GETLINE, 0, $2, $1); }
                    142:        ;
                    143: 
                    144: if:
                    145:          IF '(' conditional rparenNL   { $$ = $3; }
                    146:        ;
                    147: 
                    148: in_expr:
                    149:          expr IN varname       { $$ = op2(INTEST, $1, makearr($3)); }
                    150:        | '(' in_expr ')'       { $$ = $2; }
                    151:        ;
                    152: 
                    153: lex_expr:
                    154:          expr MATCHOP reg_expr { $$ = op3($2, 0, $1, makedfa(reparse($3), 0)); }
                    155:        | expr MATCHOP expr
                    156:                { if (((Node*)$3)->ntype == NVALUE && ((Cell*)((Node*)$3)->narg[0])->csub == CCON)
                    157:                        $$ = op3($2, 0, $1, makedfa(reparse( ((Cell*)((Node*)$3)->narg[0])->sval)), 0);
                    158:                  else
                    159:                        $$ = op3($2, 1, $1, $3); }
                    160:        | '(' lex_expr ')'      { $$ = $2; }
                    161:        ;
                    162: 
                    163: opt_plist:
                    164:          /* nothing */         { $$ = rectonode(); }
                    165:        | plist
                    166:        ;
                    167: 
                    168: pa_stat:
                    169:          pattern                       { $$ = stat2(PASTAT, $1, stat2(PRINT, rectonode(), 0)); }
                    170:        | pattern '{' stmtlist '}'      { $$ = stat2(PASTAT, $1, $3); }
                    171:        | pattern ',' pattern           { $$ = pa2stat($1, $3, stat2(PRINT, rectonode(), 0)); }
                    172:        | pattern ',' pattern '{' stmtlist '}'  { $$ = pa2stat($1, $3, $5); }
                    173:        | '{' stmtlist '}'              { $$ = stat2(PASTAT, 0, $2); }
                    174:        ;
                    175: 
                    176: pa_stats:
                    177:          /* nothing */         { $$ = (int)0; }
                    178:        | pa_stats NL
                    179:        | pa_stats pa_stat      { $$ = linkum($1, $2); }
                    180:        | pa_stats begin
                    181:        | pa_stats end
                    182:        | pa_stats func
                    183:        ;
                    184: 
                    185: 
                    186: pattern:
                    187:          reg_expr       { $$ = op3(MATCH, 0, rectonode(), makedfa(reparse($1), 0)); }
                    188:        | rel_expr
                    189:        | lex_expr
                    190:        | in_expr
                    191:        | compound_pattern
                    192:        ;
                    193: 
                    194: plist:
                    195:          easy_expr
                    196:        | plist commaNL easy_expr       { $$ = linkum($1, $3); }
                    197:        | '(' plist ')'         { $$ = $2; }
                    198:        ;
                    199: 
                    200: redir:
                    201:          GT | APPEND | LT | '|'
                    202:        ;
                    203: 
                    204: reg_expr:
                    205:          '/' {startreg();} REGEXPR '/'         { $$ = $3; }
                    206:        ;
                    207: 
                    208: rel_expr:
                    209:          expr relop expr       { $$ = op2($2, $1, $3); }
                    210:        | '(' rel_expr ')'      { $$ = $2; }
                    211:        ;
                    212: 
                    213: relop:
                    214:          EQ | NE | LT | LE | GT | GE
                    215:        ;
                    216: 
                    217: rparenNL:
                    218:          ')' | rparenNL NL
                    219:        ;
                    220: 
                    221: simple_stmt:
                    222:          PRINT opt_plist redir expr    { $$ = stat3($1, $2, $3, $4); }
                    223:        | PRINT opt_plist               { $$ = stat3($1, $2, 0, 0); }
                    224:        | PRINTF plist redir expr       { $$ = stat3($1, $2, $3, $4); }
                    225:        | PRINTF plist                  { $$ = stat3($1, $2, 0, 0); }
                    226:        | DELETE varname '[' expr ']'   { $$ = stat2(DELETE, makearr($2), $4); }
                    227:        | expr                          { $$ = exptostat($1); }
                    228:        | /* nothing */                 { $$ = (int)0; }
                    229:        | error         { yyclearin; yyerror("illegal statement"); }
                    230:        ;
                    231: 
                    232: st:
                    233:          NL | ';'
                    234:        ;
                    235: 
                    236: stmt:
                    237:          simple_stmt st
                    238:        | RETURN st             { $$ = stat1(RETURN, 0); }
                    239:        | RETURN expr st        { $$ = stat1(RETURN, $2); }
                    240:        | NEXT st               { $$ = stat1(NEXT, 0); }
                    241:        | EXIT st               { $$ = stat1(EXIT, 0); }
                    242:        | EXIT expr st          { $$ = stat1(EXIT, $2); }
                    243:        | BREAK st              { $$ = stat1(BREAK, 0); }
                    244:        | CONTINUE st           { $$ = stat1(CONTINUE, 0); }
                    245:        | '{' stmtlist '}'      { $$ = $2; }
                    246:        | if stmt               { $$ = stat3(IF, $1, $2, 0); }
                    247:        | if stmt elseNL stmt   { $$ = stat3(IF, $1, $2, $4); }
                    248:        | while stmt            { $$ = stat2(WHILE, $1, $2); }
                    249:        | for
                    250:        | varname st            { yyerror("no-effect statement"); $$ = 0; }
                    251:        ;
                    252: 
                    253: stmtlist:
                    254:          /* nothing */         { $$ = (int) 0; }
                    255:        | stmtlist NL
                    256:        | stmtlist ';'
                    257:        | stmtlist stmt         { $$ = linkum($1, $2); }
                    258:        ;
                    259: 
                    260: subop:
                    261:          SUB | GSUB
                    262:        ;
                    263: 
                    264: term:
                    265:          var
                    266:        | CALL '(' ')'                  { $$ = op2(CALL, valtonode($1,CVAR), 0); }
                    267:        | CALL '(' exprlist ')'         { $$ = op2(CALL, valtonode($1,CVAR), $3); }
                    268:        | NUMBER                        { $$ = valtonode($1, CCON); }
                    269:        | STRING                        { $$ = valtonode($1, CCON); }
                    270:        | CLOSE easy_expr               { $$ = op1(CLOSE, $2); }
                    271:        | GETLINE var LT easy_expr      { $$ = op3(GETLINE, $2, $3, $4); }
                    272:        | GETLINE LT easy_expr          { $$ = op3(GETLINE, 0, $2, $3); }
                    273:        | GETLINE var                   { $$ = op3(GETLINE, $2, 0, 0); }
                    274:        | GETLINE                       { $$ = op3(GETLINE, 0, 0, 0); }
                    275:        | BLTIN                         { $$ = op2(BLTIN, $1, rectonode()); }
                    276:        | BLTIN '(' ')'                 { $$ = op2(BLTIN, $1, rectonode()); }
                    277:        | BLTIN '(' exprlist ')'        { $$ = op2(BLTIN, $1, $3); }
                    278:        | SPRINTF '(' plist ')'         { $$ = op1($1, $3); }
                    279:        | SUBSTR '(' expr commaNL expr commaNL expr ')' { $$ = op3(SUBSTR, $3, $5, $7); }
                    280:        | SUBSTR '(' expr commaNL expr ')'              { $$ = op3(SUBSTR, $3, $5, 0); }
                    281:        | subop '(' reg_expr commaNL expr ')'           { $$ = op4($1, 0, makedfa(reparse($3), 1), $5, rectonode()); }
                    282:        | subop '(' expr commaNL expr ')'               { $$ = op4($1, 1, $3, $5, rectonode()); }
                    283:        | subop '(' reg_expr commaNL expr commaNL expr ')' { $$ = op4($1, 0, makedfa(reparse($3), 1), $5, $7); }
                    284:        | subop '(' expr commaNL expr commaNL expr ')'  { $$ = op4($1, 1, $3, $5, $7); }
                    285:        | SPLIT '(' expr commaNL varname commaNL expr ')' { $$ = op3(SPLIT, $3, makearr($5), $7); }
                    286:        | SPLIT '(' expr commaNL varname ')'            { $$ = op3(SPLIT, $3, makearr($5), 0); }
                    287:        | INDEX '(' expr commaNL expr ')'               { $$ = op2(INDEX, $3, $5); }
                    288:        | '(' expr ')'          { $$ = $2; }
                    289:        | term '+' term         { $$ = op2(ADD, $1, $3); }
                    290:        | term '-' term         { $$ = op2(MINUS, $1, $3); }
                    291:        | term '*' term         { $$ = op2(MULT, $1, $3); }
                    292:        | term '/' term         { $$ = op2(DIVIDE, $1, $3); }
                    293:        | term '%' term         { $$ = op2(MOD, $1, $3); }
                    294:        | term POWER term       { $$ = op2(POWER, $1, $3); }
                    295:        | '-' term %prec UMINUS { $$ = op1(UMINUS, $2); }
                    296:        | '+' term %prec UMINUS { $$ = $2; }
                    297:        | INCR var              { $$ = op1(PREINCR, $2); }
                    298:        | DECR var              { $$ = op1(PREDECR, $2); }
                    299:        | var INCR              { $$ = op1(POSTINCR, $1); }
                    300:        | var DECR              { $$ = op1(POSTDECR, $1); }
                    301:        ;
                    302: 
                    303: var:
                    304:          varname
                    305:        | varname '[' expr ']'  { $$ = op2(ARRAY, makearr($1), $3); }
                    306:        | field
                    307:        ;
                    308: 
                    309: varlist:
                    310:          /* nothing */         { arglist = (Node *)($$ = 0); }
                    311:        | VAR                   { $$ = valtonode($1,CVAR); arglist = (Node*) $$; }
                    312:        | varlist commaNL VAR   { $$ = linkum($1,valtonode($3,CVAR)); arglist = (Node*) $$; }
                    313:        ;
                    314: 
                    315: varname:
                    316:          VAR                   { $$ = valtonode($1, CVAR); }
                    317:        | ARG                   { $$ = op1(ARG, $1); }
                    318:        | VARNF                 { $$ = op1(VARNF, $1); }
                    319:        ;
                    320: 
                    321: 
                    322: while:
                    323:          WHILE '(' conditional rparenNL        { $$ = $3; }
                    324:        ;
                    325: 
                    326: %%
                    327: 
                    328: setfname(p)
                    329:        Cell *p;
                    330: {
                    331:        extern int bracecnt, brackcnt, parencnt;
                    332: 
                    333:        curfname = p->nval;
                    334:        /*bracecnt = brackcnt = parencnt = 0;*/
                    335: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.