#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std
bool isLetter(char ch){
if ((ch>='A' &&ch<='Z') || (ch>='a' &&ch<='z')) return true
else return false
}
bool isDigit(char ch){
if (ch>='0' &&ch<='9') return true
else return false
}
bool isP(char ch){
if(ch=='+'||ch=='*'||ch=='-'||ch=='/') return true
//ch==':'||ch==','||ch=='='||ch==''||ch=='('||ch==')'
else return false
}
bool isJ(char ch){
if(ch==','||ch==''||ch=='.'||ch=='('||ch==')'||ch=='['||ch==']'||ch=='='||ch==':'||ch=='<'||ch=='>'||ch=='{'||ch=='}'||ch=='#') return true
//
else return false
}
bool isBlank(char ch){
if(ch==' '||ch=='\t') return true
else return false
}
int main(){
string src,ste,s
char ch0,ch,ch1[2]
char ktt[48][20]={"and","begin","const","div","do","else","end","function","if","integer",
"not","or","procedure","program","read","real","then","type","var","while","write","标识符","无符号数",
",","",":",".","(",")","[","]","..","++","--","+","-","*","/","=","<",">","<>","<="
,">=",":=","{","}","#"}
int pos=0
FILE *fp
fp=fopen("d:\\in.txt","r")
ch0=fgetc(fp)
while(ch0!=EOF)
{
//if(ch0!='\t'){src+=ch0}
src+=ch0
ch0=fgetc(fp)
}
src+='#'
cout<<src<<endl
ch=src[pos++]
ste=" "
for(int j=0j<47j++){cout<<j<<ktt[j]<<endl}
cout<<"词法分析:\n"
while(ch!='#')
{
char str[20]
if(ch!='\n')
{
if(isDigit(ch))
{ //判断常数
int i=0
while(isDigit(ch)||ch=='.')
{
str[i++]=ch
//i++
ch=src[pos++]
}
str[i]='\0'
ste=ste+"戚扮|"+"22"
cout<<str
continue
}
else if(isLetter(ch))
{ //判断字符
int i=0,j
while(isLetter(ch)||isDigit(ch))
{
str[i++]=ch
//i++
ch=src[pos++]
}
str[i]='\0'
int t=strcmp(str,ktt[j])
if(t==0) {
stringstream ss
ste+="|"
ss<<stess<<j
ss>>ste
break
}
}
if(j==21){ste=ste+"|"+"21"}
// cout<<" "
cout<<str
continue
}
else if(isP(ch)){ ///判断是否运含仔扒算符
int i=0,j
str[i++]=ch
str[i]='\0'
for(j=34j<38j++){
int t=strcmp(str,ktt[j])
if(t==0) {
stringstream ss
ste+="|"
ss<<stess<<j
ss>>ste
break
}
}
cout<<str
ch=src[pos++]
continue
}
else if(isJ(ch)) //判断是否界符
{
int i=0,j
while(isJ(ch))
{
str[i++]=ch
ch=src[pos++]
}
str[i]='\0'
for(j=23j<47j++){
int t=strcmp(str,ktt[j])
if(t==0) {
stringstream ss
ste+="|"
ss<<stess<<j
ss>>ste
break
}
}
cout<<str
continue
}
else if(isBlank(ch))
{
cout<<ch
ch=src[pos++]
continue
}
}
else{
cout<<ste<<endl
ste=" "
}
ch=src[pos++]
}
return 0
}
还有运行效果图,和实验报告 ,你要的话留下邮箱
#include <iostream>#include <string>
using namespace std
string key[6] = {"begin", "if", "then", "while", "do", "end"}
//关键字
bool isKey( string str, int &syn) //判断是否为关键字,若是传虚雀回相
应关键码的种别名
{
int i
for(i=0i<6i++)
{
if(str == key[i])
{
syn = i + 1
return true
}
}
return false
}
bool isLetter(char c) //是否为字母
{
if((c >= 'A' &&c <= 'Z') || (c >= 'a' &&c <= 'z'))
return true
else
return false
}
bool isDigit(char c) //是否为数字
{
if(c >= '0' &&c <= '9')
return true
else
return false
}
void analyse(FILE *fileP)
{
int n
char c
string str = ""
while((c = fgetc(fileP)) != EOF)
{
if(c == ' ' || c == '\n' || c == '\t')
continue
else if(isDigit(c)) //数字
{
while(isDigit(c))
{
str += c
c = fgetc(fileP)
}
fseek(fileP, -1, SEEK_CUR)
cout <<"(11, " <<str <瞎誉嫌<")" <<endl
str = ""
}
else if(isLetter(c)) //字母开头的
{
while(isDigit(c) || isLetter(c))
{
str += c
c = fgetc(fileP)
}
fseek(fileP, -1, SEEK_CUR)
if(isKey(str, n))
cout <<"(" <<n <<", " <<str <<")" <<endl//关键码
else
cout <<"(10, " <<"\'"<<str <<"\'" <<")" <<endl//标志符
str = ""
}
else // *** 作符等
{
switch(c)
{
case '+':
cout <<"(13, +)" <<endl
break
case '-':
cout <<"(14, -)" <<endl
break
case '*':
cout <<"(15, *)" <<endl
break
case '/':
cout <<"(16, /)" <<endl
break
case ':':
{
if(c=fgetc(fileP) == '=')
cout <磨手<"(18, :=)" <<endl
else
{
cout <<"(17, :)" <<endl
fseek(fileP, -1, SEEK_CUR)
}
break
}
case '<':
{
c=fgetc(fileP)
if(c == '=')
cout <<"(22, <=)" <<endl
else if(c == '>')
cout <<"(21, <>)" <<endl
else
{
cout <<"(20, <)" <<endl
fseek(fileP, -1, SEEK_CUR)
}
break
}
case '>':
{
c=fgetc(fileP)
if(c == '=')
cout <<"(24, >=)" <<endl
else
{
cout <<"(23, >)" <<endl
fseek(fileP, -1, SEEK_CUR)
}
break
}
case '=':
cout <<"(25, =)" <<endl
break
case '':
cout <<"(26, )" <<endl
break
case '(':
cout <<"(27, ()" <<endl
break
case ')':
cout <<"(28, ))" <<endl
break
case '#':
cout <<"(0, #)" <<endl
break
}
}
}
}
int main()
{
FILE *fileP
fileP = fopen("test.txt", "r")
cout <<"------词法分析如下------" <<endl
analyse(fileP)
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)