求一个C语言词法分析器源代码

求一个C语言词法分析器源代码,第1张

我有,这是这学期刚做的,

#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:\\intxt","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=0;j<47;j++){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';

for(j=0;j<21;j++){ //判断是否关键字

int t=strcmp(str,ktt[j]);

if(t==0) {

stringstream ss;

ste+="|";

ss<<ste;ss<<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=34;j<38;j++){

int t=strcmp(str,ktt[j]);

if(t==0) {

stringstream ss;

ste+="|";

ss<<ste;ss<<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=23;j<47;j++){

int t=strcmp(str,ktt[j]);

if(t==0) {

stringstream ss;

ste+="|";

ss<<ste;ss<<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<stdioh>

#include<ctypeh>

#include<stringh>

#include<stdboolh>  //C99支持bool类型

int main(void)

{

    char str[] = "wordptr_239";

    int t;

    int i;

    t = strlen(str);

    bool ok = true;  //添加标识符,默认合法

    for(i = 0; i < t; i++)

    {

        if(!isdigit(str[0]))

        {

            if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z'||str[i]>='0'&&str[i]<='9'||str[i]=='_')

            {

                printf("%c", str[i]);

                while(str[i] == '#')

                {

                    break;

                }

            }

            else

            {

                printf("非法");

                ok = false;

            }

        }

        else

        {

            printf("非法");

            ok = false;

        }

    }

    if(ok) printf("正确");

    return 0;

}

给你优化了以下,

错误原因如下:

printf("%c", str[i]); //字符打印用%c 不是%s

以上就是关于求一个C语言词法分析器源代码全部的内容,包括:求一个C语言词法分析器源代码、c语言写的一个词法分析程序,识别标识符的,但是好像逻辑有问题,自己不知道怎么改,求大神指点、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9969950.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-03
下一篇2023-05-03

发表评论

登录后才能评论

评论列表(0条)

    保存