如果你还在为多文件组织的重复定义烦恼时,就来这里看看吧。
main.cpp
#include"analyse.h"
#include<iostream>
#include<stdio.h>
using namespace std;
string sentence;
int main()
{
c = ' ';
i = 0; j = 0; k = 0;
sentence = "";
getline(cin,sentence);//输入语句
getsym(sentence);//得到语句
return 0;
}
analyse.h
#ifndef ANALYSE_H_INCLUDED
#define ANALYSE_H_INCLUDED
/*
Copyright:MyCompany
Author: JZF
Date:2016-11-02
Description:Provide functions to lexical analysis;
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<conio.h>
using namespace std;
extern string sentence;
extern char c;
extern char word[10];
extern char word2[10];
extern int i, k;
static int j;
bool judge_keyword(char word[10]);
bool judge_constant(char word[10]);
bool judge_identifiers(char word[10]);
bool judge_operator(char word[10]);
bool judge_delimiter(char c);
void getsym(string sentence);
#endif // ANALYSE_H_INCLUDED
#include "analyse.h"
#include<iostream>
#include<stdio.h>
using namespace std;
char c;
char word[10];
char word2[10];
int i, k;
bool judge_keyword(char word[10])//判断是否为关键字
{
if (strcmp(word, "begin") == 0)
{
cout << "(1,begin)";
}
else if (strcmp(word, "if") == 0)
{
cout << "(2,if)";
}
else if (strcmp(word, "then") == 0)
{
cout << "(3,then)";
}
else if (strcmp(word, "while") == 0)
{
cout << "(4,while)";
}
else if (strcmp(word, "do") == 0)
{
cout << "(5,do)";
}
else if (strcmp(word, "end") == 0)
{
cout << "(6,end)";
}
else
{
return false;
}
return true;
}
bool judge_constant(char word[10])//判断是否为常量
{
c = word[j-1];
if (c <= '9' && c >= '0')
{
k = 0;
word2[k] = c;
k++;
while (word[j] <= '9' && word[j] >= '0')
{
word2[k] = word[j];
j++;
k++;
}
if (word[j] == '.')//判断是否为小数
{
j++;
word2[k] = '.';
k++;
while (word[j] <= '9' && word[j] >= '0')
{
word2[k] = word[j];
j++;
k++;
}
}
word2[k] = '\0';
cout << "(11," << word2 << ")";
return true;
}
else
return false;
}
bool judge_identifiers(char word[10])//判断是否为标识符
{
c = word[j-1];
if (c <= 'z' && c >= 'a')
{
k = 0;
word2[k] = c;
k++;
while ((word[j] <= 'z' && word[j] >= 'a')||(word[j] <= '9' && word[j] >= '0'))
{
word2[k] = word[j];
j++;
k++;
}
word2[k] = '\0';
cout << "(10," << word2 << ")";
return true;
}
else
return false;
}
bool judge_operator(char word[10])//判断是否为运算符
{
c = word[j-1];
if (c == '+')
{
cout << "(13,+)";
}
else if (c == '-')
{
cout << "(14,-)";
}
else if (c == '*')
{
cout << "(15,*)";
}
else if (c == '/')
{
cout << "(16,/)";
}
else if (c == ':')
{
if (word[j] == '=')
{
cout << "(18,:=)";
j++;
}
else
cout << "(17,:)";
}
else if (c == '<')
{
if (word[j] == '>')
{
cout << "(21,<>)";
}
else if (word[j] == '=')
{
cout << "(22,<=)";
}
else
cout << "(20,<)";
j++;
}
else if (c == '>')
{
if (word[j] == '=')
{
cout << "(24,>=)";
j++;
}
else
cout << "(23,>)";
}
else if (c == '=')
{
cout << "(25,=)";
}
else
{
return false;
}
return true;
}
bool judge_delimiter(char c)//判断是否为界符
{
if (c == ';')
{
cout << "(26,;)";
}
else if (c == '(')
{
cout << "(27,()";
}
else if (c == ')')
{
cout << "(28,))";
}
else
{
return false;
}
return true;
}
void getsym(string sentence)//得到语句
{
i = 0;
j = 0;
k = 0;
c = sentence[0];
while (c != '#')
{
c = sentence[i];
word[j] = c;
i++;
j++;
if (c == ' ' || c == '#')//得到一个单词
{
word[j-1] = '\0';
if(judge_keyword(word)){}//判断是否为关键字
else
{
j = 0;
c=word[0];
k = 0;
while(c != '\0')
{
c = word[j];
j++;
if (judge_constant(word)){}//判断是否为常量
else if (judge_identifiers(word)){}//判断是否为标识符
else if (judge_operator(word)){}//判断是否为运算符
else if (judge_delimiter(c)){}//判断是否为界符
}
}
j = 0;//单词初始化
}
}
}