资源描述:
《简单上机实验词法分析程序.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、*==========词法分析程序========== *学生:万治鑫 学号:20000668 *班级:00计本-4 *本程序在Turbo C 3.0下调试通过 *运行时,请将源文件改名为 in.pas *最后修改:2003/6/6 *========Copyright (C)2003========*/#include #include #include #define input "in.pas" //source file name(if need,you can modify it)#de
2、fine output "cifa.txt" //output file name#define KEY 1 //关键字(保留字)#define ID 2 //标志符#define CONST 3 //常数#define OPER 4 //关系运算符 < <= = <> >= >#define INTER 5 //标点 + - * / ( ) [ ] , : ; . .. '#define EVAL 6 //赋值号 :=#define N 10000 //二元表最大长度//关键字及保留字表char *Key[44] = {
3、 "and", "array", "begin", "case", "const", "div", "do", "downto", "else","end", "file", "for", "function", "goto", "if", "in", "label", "mod","nil", "not", "of", "or", "packed", "procedure", "program", "record","repeat", "set", "then", "to", "type","until","uses", "var
4、", "while", "with","false", "true", "integer", "char", "real","boolean", "writeln", "readln" };//符号表char *Character[23] = {"+", "-", "*", "/", "=", "<>", "<=", ">=", "<", ">", "(", ")", "[", "]", "{", "}", ":=", ",", ";", ".", ":", "..","'"};//globa
5、l variablechar ch; //从文件中读出字符int p = 0;//定位标志int table[N][2]; //二元组序列//int SeekChr()int IsLetter(char c){ if ( (c>='a' && c<='z')
6、
7、 (c>='A' && c<='Z') ) return 1; else return 0;}int IsDigit(char c){ if (c>='0' && c<='9') return 1; else retu
8、rn 0;}/* * 查符号表函数 * 作用:使二元表p条目第2项指向符号表中符号所在位置 * 并使p加1 */void SeekSymbol(char *buf){ for (int i=0;i<23;i++) if ( strcmp(Character[i],buf)==0 ) table[p][1]=i; p++;}//查关键字表int SeekKey(char *buf){ for (int i=0;i<44;i++) { if ( strcmp(Key[i],bu
9、f)==0 ) //modify in 16:26 { table[p][0]=KEY; table[p][1]=i; p++; return 1; } } return 0;}/************************* *输入参数:文件指针 *作用:词法扫描器 **************************/void ScanToken(FILE *fp){ //char ch; int j=0,k=0
10、; char strID[32];//ID字符