java词法分析器实验报告
Java 词法分析器实验报告 --07111101 --奥特曼 一.词法分析器功能概述: 1. 使用DFA实现词法分析器的设计; 2. 实现对Java 源程序中注释和空格(空行) 的过滤; 3. 利用两对半缓冲区从文件中逐一读取单词; 4. 词法分析结果属性字流存放在独立文件(c:\words.txt) 中; 5. 统计源程序所有单词数以、错误单词数、单词所在的行数; 6. 具有报告词法错误和出错位置(源程序行号)的功能; 二.源程序设计实现://程序大部分参照网络,自己做了小部分改动 #include #include #include #include #include #include “const.h“ using namespace std; char rbuf[RBUFSIZE]; //读文件缓冲区 int rp; //读文件缓冲区指针 char ch; //当前扫描到的字符 int type; //单词的类型 char sbuf[SBUFSIZE]; //单词字符串缓冲区 int sp; //单词字符串缓冲区指针 ifstream inFile; //输入文件 ofstream outFile; //输出文件 void clear_rbuf()//清空读文件缓冲区 { int i; for(i=0;i= 0 else result = -1;}else if(c>= a elseresult=-1;}else if (c>= A elseresult=-1;}elseresult=-1;return result; } void scan_fraction()//扫描指数 {while(digit(10)>=0){put_ch(ch);get_ch();}if(ch== e ||ch== E ){put_ch(ch);get_ch();if(ch== + ||ch== - ){put_ch(ch);get_ch();} while(digit(10)>=0) { put_ch(ch); get_ch(); } return; } return;} void scan_suffix() //扫描浮点数后缀 {scan_fraction();if(ch== f ||ch== F ||ch== d ||ch== D ){ put_ch(ch);get_ch(); } type=T_FLOAT; return; } bool is_spectial(char else return false; } void scan_operator()//扫描运算符 {while (is_spectial(ch)){put_ch(ch);get_ch();} get_type(sbuf); if(type==0) type=T_ERROR; return; } void scan_number(int radix)//扫描8、10、16进制数值 {while(digit(radix)>=0){put_ch(ch);get_ch();}if(radix!=10get_ch();type=T_ERROR; } else if(radix==10 get_ch(); if(digit(10)>=0) scan_suffix(); } else if(radix==10 else if(ch == l || ch == L ) { put_ch(ch); get_ch();type=T_INT; } else type=T_INT; return; } void skip_comment()//跳过注释内容 {while(ch!= \0 ){switch(ch){case * :get_ch();if (ch== / ){get_ch();return;}break;default:get_ch();break;}} } bool is_idchar(char }