- 论坛徽章:
- 0
|
本帖最后由 yizhengming 于 2012-09-14 13:55 编辑
这是编译原理虎书的例子, 小弟对标记有1,2,3的行百思不得其解?(觉得3是打印单词的行号,和列号)- #include <stdio.h>
- #include <stdlib.h>
- #include <stdarg.h>
- #include "util.h"
- #include "errormsg.h"
- bool anyErrors= FALSE;
- static string fileName = "";
- static int lineNum = 1;
- int EM_tokPos=0;
- extern FILE *yyin;
- typedef struct intList {int i; struct intList *rest;} *IntList;
- static IntList intList(int i, IntList rest)
- {
- IntList l= checked_malloc(sizeof *l);
- l->i=i; l->rest=rest;
- return l;
- }
- static IntList linePos=NULL;
- void EM_newline(void)
- {
- lineNum++;
- linePos = intList(EM_tokPos, linePos);
- }
- void EM_error(int pos, char *message,...)
- {
- va_list ap;
- IntList lines = linePos;
- int num=lineNum;
- anyErrors=TRUE;
- while (lines && lines->i >= pos) 1
- {lines=lines->rest; num--;} 2
- if (fileName) fprintf(stderr,"%s:",fileName);
- if (lines) fprintf(stderr,"%d.%d: ", num, pos-lines->i); 3
- va_start(ap,message);
- vfprintf(stderr, message, ap);
- va_end(ap);
- fprintf(stderr,"\n");
- }
- void EM_reset(string fname)
- {
- anyErrors=FALSE; fileName=fname; lineNum=1;
- linePos=intList(0,NULL);
- yyin = fopen(fname,"r");
- if (!yyin) {EM_error(0,"cannot open"); exit(1);}
- }
复制代码 |
|