Tiny语言的词法分析器
- 格式:docx
- 大小:34.53 KB
- 文档页数:14
课程 名称 编译原理课程设计 实验课时 8
实验 项目 手工构造Tiny语言的词法分析器 实验时间 7-10 周
实验 熟悉Tiny语言词法;构造DFA设计数据类型、数据结构;用 C++实现Tiny语 目的 言的词法分析器
实验 Win dows 10专业版 环境 Microsoft Visual Studio 2013
一、Tiny 语言记号 Reserved words Special Symbols Other if + 实验 then -
内容 else * nu mber(1 or more digits)
(算 end <
法、 repeat =
程 un til /
序、 read
( )
5 步骤 write
ide ntifier(1 or more
和方 letters) 法) 二、构造Tiny语言DFA
ID: letter(letter)* Number: digit(digit)*
三、根据DFA编写词法分析器 #include #include #include
using namespacestd; static int rowCounter = 1; //静态变量,用于存储行数
static bool bracketExist = false ;// 判断注释存在与否,false 为不存在
class Lex
ofstream output; stri ng line = Lex( string inputLine )
line = in putL ine sca n( Trim(l in e)); rowCou nter++; } string Trim( string &str )//函数用于去除每行前后空格
int s = ( " \t"); int e = ( “ \t");str = (s, e - s + 1); str += "\0"; return str ; }
void scan( string inputLine ) { ofstream output; ("", ios::app); string line = inputLine ; int i = 0; string str = ""; int temp; string token ="“ ; output << rowCounter << ": " << line << endl; // 输岀每行
while (line[i] != '\0' )// 根据 DFAm描并判断 { if (line[i] == '{' )// 注释
{ bracketExist = true ; } if (bracketExist == true ) { output << "\t" << rowCou nter << ":"; while (line[i] != '}') {
output << lin e[i]; //不处理,直接输出
if (li ne[i + 1] != NULL { i++; }
else break; } if (li ne[i] == '}' )//注释结束
{ output << lin e[i] 1 if (bracketExist == false ) { //数字 while (isdigit(line[i])) { temp = temp * 10 + lin e[i]; if (!isdigit(line[i + 1])) { output << "\t" << rowCounter << ": " << "NUM, va匸"<< temp -'O' << endl; }] if (line[i + 1] != NULL&isdigit(line[i + 1])) i++; else break; } temp = 0; //符号 while (!(isdigit(line[i]) || (line[i] >= 'a' &&line[i] <= 'z' ) || (li ne[i] >= 'A' &&line[i] <= 'Z' ) || line[i]== '' || lin e[i]== '{' || lin e[i]== '}')) { toke n = toke n + lin e[i]; if (isdigit(line[i + 1]) || (line[i + 1] >= 'a' &&line[i + 1] <= 'z') || (li ne[i + 1] >= 'A' &&line[i + 1] <= 'Z' ) || line[i + 1]== ' '|| line[i + 1]== {' II lin e[i + 1]== '}' || line[i + 1] == NULL { if (isToken(token)) while (token[j] != '\0') { output << "\t" << rowCou nter << ": " << toke n[j] << en dl; j++; } } }] else { i++; continue ; } if (line[i + 1] != NULL i++; else break; } token =""; //字母 while ((line[i] >= 'a' &&line[i] <= 'z' ) || (line[i] >= 'A' &&line[i] <= 'Z')) { str = str + lin e[i]; if (!((line[i + 1] >= 'a' &&line[i + 1] <= 'z' ) || (line[i + 1] >= 'A' &&line[i + 1] <= 'Z'))) { if (isResearvedWord(str)) // 判断是否是保留字 { output << "\t" << rowCounter << ": " << "Reversed Word: "<< str << endl; break; output << "\t" << rowCou nter << str << en dl;":"<< "ID, name=" << break; }j } | if (line[i + 1] != NULL i++; } str =""; if (line[i + 1] != NULL { i++; } else break; if (line[i]== ';') output << "\t" << rowCou nter << ": " << lin e[i]; break; } } //清空,以备下一行读取 line =""; str =""; temp = 0; token =""; output << en dl; (); } bool isResearvedWord( string s)〃 存储保留字,并判断 { string reservedWord[8] = { "if" , "then" , "else" , "end" , "repeat" , "until" , "read", "write" }; bool judge = false ; for ( int i = 0; i < 8; i++) { if ( s == reservedWord[i]) { judge = true ; break; } } return judge; 二} bool isToken( string s)〃 存储符号,并判断 { string token[10] = { "+" , "-" , "*" , "/" , "=" , "<" , "(" , ")" ,";",":=" }; bool judge = false ; for ( int i = 0; i < 10; i++) { if ( s == token[i]) { judge = true ; break; } } return judge; } }; int main() { ifstream in put; ("");] str ing lin e[50]; int i = 0; while (getline(input, line[i])) { //cout << lin e[i] << en dl; i++; } (); cout << endl << endl << "Read ing source file completed!!" << en dl; int j = 0; remove(""); for (j = 0; j < i; j++) { Lex lex(li ne[j]); } cout << endl << endl << "Writ ing file completed!!" << endl << endl << en dl; return 0; }