📄 y2.c
字号:
else if(s[2] <= '7' && s[2]>='0'){ /* \nnn sequence */ if(s[3]<'0' || s[3] > '7' || s[4]<'0' || s[4]>'7' || s[5] != '\0') error("illegal \\nnn construction"); val = 64*s[2] + 8*s[3] + s[4] - 73*'0'; if(val == 0) error("'\\000' is illegal"); } } else { val = extval++; } tokset[ntokens].value = val; toklev[ntokens] = 0; return(ntokens);}/* * write out the defines (at the end of the declaration section) */void defout(void){ register int i, c; register char *cp; for(i=ndefout; i<=ntokens; ++i){ cp = tokset[i].name; if(*cp == ' ') ++cp; /* literals */ for(; (c= *cp)!='\0'; ++cp){ if(islower(c) || isupper(c) || isdigit(c) || c=='_'); /* VOID */ else goto nodef; } fprintf(ftable, "#define %s %d\n", tokset[i].name, tokset[i].value); if(fdefine != NULL) fprintf(fdefine, "#define %s %d\n", tokset[i].name, tokset[i].value); nodef: ; } ndefout = ntokens+1;}char *cstash(register char * s){ char *temp; temp = cnamp; do { if(cnamp >= &cnames[cnamsz]) error("too many characters in id's and literals"); else *cnamp++ = *s; } while (*s++); return(temp);}int yylex(void){ register int i, base; static int peekline; /* number of '\n' seen in lookahead */ register int c, match, reserve; yylval.tokname=tokname; for(;;) { reserve = 0; lineno += peekline; peekline = 0; c = getc(finput); while(c==' ' || c=='\n' || c=='\t' || c=='\f'){ if(c == '\n') ++lineno; c=getc(finput); } if(c == '/') /* skip comment */ lineno += skipcom(); else break; } switch(c){ case EOF: return(ENDFILE); case '{': ungetc(c, finput); return('='); /* action ... */ case '<': /* get, and look up, a type name (union member name) */ i = 0; while((c=getc(finput)) != '>' && c>=0 && c!= '\n'){ tokname[i] = c; if(++i >= NAMESIZE) --i; } if(c != '>') error("unterminated < ... > clause"); tokname[i] = '\0'; for(i=1; i<=ntypes; ++i){ if(!strcmp(typeset[i], tokname)){ yylval.number = i; return(TYPENAME); } } typeset[yylval.number = ++ntypes] = cstash(tokname); return(TYPENAME); case '"': case '\'': match = c; tokname[0] = ' '; i = 1; for(;;){ c = getc(finput); if(c == '\n' || c == EOF) error("illegal or missing ' or \""); if(c == '\\'){ c = getc(finput); tokname[i] = '\\'; if(++i >= NAMESIZE) --i; } else if(c == match) break; tokname[i] = c; if(++i >= NAMESIZE) --i; } break; case '%': case '\\': switch(c=getc(finput)) { case '0': return(TERM); case '<': return(LEFT); case '2': return(BINARY); case '>': return(RIGHT); case '%': case '\\': return(MARK); case '=': return(PREC); case '{': return(LCURLY); default: reserve = 1; } default: if(isdigit(c)){ /* number */ yylval.number = c-'0' ; base = (c=='0') ? 8 : 10 ; for(c=getc(finput); isdigit(c) ; c=getc(finput)){ yylval.number = yylval.number*base + c - '0'; } ungetc(c, finput); return(NUMBER); } else if(islower(c) || isupper(c) || c=='_' || c=='.' || c=='$'){ i = 0; while(islower(c) || isupper(c) || isdigit(c) || c=='_' || c=='.' || c=='$'){ tokname[i] = c; if(reserve && isupper(c)) tokname[i] += 'a'-'A'; if(++i >= NAMESIZE) --i; c = getc(finput); } } else return(c); ungetc(c, finput); } tokname[i] = '\0'; if(reserve){ /* find a reserved word */ if(!strcmp(tokname,"term")) return(TERM); if(!strcmp(tokname,"token")) return(TERM); if(!strcmp(tokname,"left")) return(LEFT); if(!strcmp(tokname,"nonassoc")) return(BINARY); if(!strcmp(tokname,"binary")) return(BINARY); if(!strcmp(tokname,"right")) return(RIGHT); if(!strcmp(tokname,"prec")) return(PREC); if(!strcmp(tokname,"start")) return(START); if(!strcmp(tokname,"type")) return(TYPEDEF); if(!strcmp(tokname,"union")) return(UNION); error("invalid escape, or illegal reserved word: %s", tokname); } /* look ahead to distinguish IDENTIFIER from C_IDENTIFIER */ c = getc(finput); while(c==' ' || c=='\t'|| c=='\n' || c=='\f' || c== '/') { if(c == '\n') ++peekline; else if(c == '/'){ /* look for comments */ peekline += skipcom(); } c = getc(finput); } if(c == ':') return(C_IDENTIFIER); ungetc(c, finput); return(IDENTIFIER);}/* * determine the type of a symbol */int fdtype(int t){ register int v; if(t >= NTBASE) v = nontrst[t-NTBASE].tvalue; else v = TYPE(toklev[t]); if(v <= 0) error("must specify type for %s", (t>=NTBASE)?nontrst[t-NTBASE].name: tokset[t].name); return(v);}int chfind(int t, register char *s){ int i; if (s[0]==' ') t=0; TLOOP(i){ if(!strcmp(s,tokset[i].name)){ return(i); } } NTLOOP(i){ if(!strcmp(s,nontrst[i].name)) { return(i+NTBASE); } } /* cannot find name */ if(t>1) error("%s should have been defined earlier", s); return(defin(t, s));}/* * copy the union declaration to the output, and the define file if present */void cpyunion(void){ int level, c; fprintf(ftable, "\n#line %d \"%s\"\n", lineno, infile); fprintf(ftable, "typedef union "); if(fdefine) fprintf(fdefine, "\ntypedef union "); level = 0; for(;;){ if((c=getc(finput)) < 0) error("EOF encountered while processing %%union"); putc(c, ftable); if(fdefine) putc(c, fdefine); switch(c){ case '\n': ++lineno; break; case '{': ++level; break; case '}': --level; if(level == 0) { /* we are finished copying */ fprintf(ftable, " YYSTYPE;\n"); if(fdefine) fprintf(fdefine, " YYSTYPE;\nextern YYSTYPE yylval;\n"); return; } } }}/* * copies code between \{ and \} */void cpycode(void){ int c; c = getc(finput); if(c == '\n') { c = getc(finput); lineno++; } fprintf(ftable, "\n#line %d \"%s\"\n", lineno, infile); while(c>=0){ if(c=='\\') { if((c=getc(finput)) == '}') return; else putc('\\', ftable); } if(c=='%') { if((c=getc(finput)) == '}') return; else putc('%', ftable); } putc(c , ftable); if(c == '\n') ++lineno; c = getc(finput); } error("eof before %%}");}/* * skip over comments */int skipcom(void){ register int c, i=0; /* i is the number of lines skipped */ /* skipcom is called after reading a / */ if(getc(finput) != '*') error("illegal comment"); c = getc(finput); while(c != EOF){ while(c == '*'){ if((c=getc(finput)) == '/') return(i); } if(c == '\n') ++i; c = getc(finput); } error("EOF inside comment"); /* NOTREACHED */ return 0;}/* * copy C action to the next ; or closing } */void cpyact(int offset){ int brac, c, match, j, s, tok; fprintf(faction, "\n#line %d \"%s\"\n", lineno, infile); brac = 0; for(;;) { c = getc(finput); swt: switch(c){ case ';': if(brac == 0){ putc(c , faction); return; } goto lcopy; case '{': brac++; goto lcopy; case '$': s = 1; tok = -1; c = getc(finput); if(c == '<'){ /* type description */ ungetc(c, finput); if(yylex() != TYPENAME) error("bad syntax on $<ident> clause"); tok = yylval.number; c = getc(finput); } if(c == '$'){ fprintf(faction, "yyval"); if(ntypes){ /* put out the proper tag... */ if(tok < 0) tok = fdtype(*prdptr[nprod]); fprintf(faction, ".%s", typeset[tok]); } continue; } if(c == '-'){ s = -s; c = getc(finput); } if(isdigit(c)){ j=0; while(isdigit(c)){ j= j*10+c-'0'; c = getc(finput); } j = j*s - offset; if(j > 0){ error("Illegal use of $%d", j+offset); } fprintf(faction, "yypvt[-%d]", -j); if(ntypes){ /* put out the proper tag */ if(j+offset <= 0 && tok < 0) error("must specify type of $%d", j+offset); if(tok < 0) tok = fdtype(prdptr[nprod][j+offset]); fprintf(faction, ".%s", typeset[tok]); } goto swt; } putc('$' , faction); if(s<0) putc('-', faction); goto swt; case '}': if(--brac) goto lcopy; putc(c, faction); return; case '/': /* look for comments */ putc(c , faction); c = getc(finput); if(c != '*') goto swt; /* it really is a comment */ putc(c , faction); c = getc(finput); while(c != EOF){ while(c=='*'){ putc(c , faction); if((c=getc(finput)) == '/') goto lcopy; } putc(c , faction); if(c == '\n') ++lineno; c = getc(finput); } error("EOF inside comment"); case '\'': /* character constant */ match = '\''; goto string; case '"': /* character string */ match = '"'; string: putc(c , faction); while((c=getc(finput))){ if(c=='\\'){ putc(c , faction); c=getc(finput); if(c == '\n') ++lineno; } else if(c==match) goto lcopy; else if(c=='\n') error("newline in string or char. const."); putc(c , faction); } error("EOF in string or character constant"); case EOF: error("action does not terminate"); case '\n': ++lineno; goto lcopy; } lcopy: putc(c , faction); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -