pl0 词法分析

qlks 2004-12-03 10:45:24
谁有C++写的pl0词法分析,发我一份,只要词法分析的。谢
lovelydavid@hotmail.com
...全文
105 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
greenteanet 2004-12-03
  • 打赏
  • 举报
回复
不好意思,我没有
o1n 2004-12-03
  • 打赏
  • 举报
回复
#define MAX 1000
#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <string.h>

int inputpro();
int sylex();
int cleararray();
int judgeword();
char *judgeoperator(char *);
char buffer[MAX];
char word[MAX];
char *p;
int x=1;
int type=1;
int m=0;

int main()
{
inputpro();
sylex();
return 0;
}

int inputpro() //输入处理函数
{

int ch,i=0;
printf( "请输入程序代码段,以'#'作为结尾:\n" );
while ((ch = getchar()) != '#') {
buffer[i] = ch;
i++;
}
return 0;
}

int sylex() //词法分析函数
{
int count=0;
p=buffer;
count = strlen(buffer);
printf("\n");
printf(" **词法分析表** \n");
printf("\n");
printf("序号 单词 类别 自身值 \n");
for (p=buffer;p<buffer+count;p++) {
if (*p == ' ' || *p == '\n' || *p == '+' || *p == '-' || *p == '*' || *p == '/' ||
*p == ':' || *p == '<' || *p == '>' || *p == '=' || *p == '(' || *p == ')' ||
*p == ';') {
judgeword();
p=judgeoperator(p);
continue;
}
word[m++] = *p;
}

return 0;
}

int cleararray() //清空单词数组
{
int count=0;
int i=0;
count = strlen(word);
for (i=0;i<count;i++) {
word[i] = '\0';
}
return 0;
}

int judgeword() //判断关键字以及整常数
{
if (strcmp(word, "if") == 0) {
type = 3;
cout<<setw (3)<< x++ <<setw (10)<<word<<setw (7)<<type<<endl;
cleararray();
m=0;

}

else if (strcmp(word, "then")== 0) {
type = 4;
cout<<setw (3)<< x++ <<setw (10)<<word<<setw (7)<<type<<endl;
cleararray();
m=0;
}

else if (strcmp(word, "else") == 0) {
type = 5;
cout<<setw (3)<< x++ <<setw (10)<<word<<setw (7)<<type<<endl;
cleararray();
m=0;
}
else if (word[0]>='0' && word[0]<='9') { //打印整常数
type =2;
cout<<setw (3)<< x++ <<setw (10)<<word<<setw (7)<<type<<setw (12)<<word<<endl;
cleararray();
m=0;
}
else if (strcmp(word, "")!=0) //打印标识符
{
type =1;
cout<<setw (3)<< x++ <<setw (10)<<word<<setw (7)<<type<<setw (12)<<word<<endl;
cleararray();
m=0;
}

return 0;
}

char *judgeoperator(char *p)
{
if (*p == '+') {
word[0] = *p;
type = 6;
cout<<setw (3)<< x++ <<setw (10)<<word<<setw (7)<<type<<endl;
cleararray();
}

if (*p == '-') {
word[0] = *p;
type = 7;
cout<<setw (3)<< x++ <<setw (10)<<word<<setw (7)<<type<<endl;
cleararray();
}

if (*p == '*') {
word[0] = *p;
type = 8;
cout<<setw (3)<< x++ <<setw (10)<<word<<setw (7)<<type<<endl;
cleararray();
}

if (*p == '/') {
word[0] = *p;
type = 9;
cout<<setw (3)<< x++ <<setw (10)<<word<<setw (7)<<type<<endl;
cleararray();
}

if (*p == ':') { //处理:=符号,暂时没有出错处理
word[0] = *p;
p++;
word[1] = *p;
type = 10;
cout<<setw (3)<< x++ <<setw (10)<<word<<setw (7)<<type<<endl;
cleararray();
}
else if (*p == '=') {
word[0] = *p;
type = 13;
cout<<setw (3)<< x++ <<setw (10)<<word<<setw (7)<<type<<endl;
cleararray();
}

if (*p == '<') { //处理<>符号
word[0] = *p;
if (*++p == '>') {
word[1] = *p;
type = 14;
cout<<setw (3)<< x++ <<setw (10)<<word<<setw (7)<<type<<endl;
cleararray();
}
else {
--p;
cleararray();
word[0] = *p;
type = 11;
cout<<setw (3)<< x++ <<setw (10)<<word<<setw (7)<<type<<endl;
cleararray();
}
}
else if (*p == '>') {
word[0] = *p;
type = 12;
cout<<setw (3)<< x++ <<setw (10)<<word<<setw (7)<<type<<endl;
cleararray();
}

if (*p == '(') {
word[0] = *p;
type = 15;
cout<<setw (3)<< x++ <<setw (10)<<word<<setw (7)<<type<<endl;
cleararray();
}

if (*p == ')') {
word[0] = *p;
type = 17;
cout<<setw (3)<< x++ <<setw (10)<<word<<setw (7)<<type<<endl;
cleararray();
}

if (*p == ';') {
word[0] = *p;
type = 17;
cout<<setw (3)<< x++ <<setw (10)<<word<<setw (7)<<type<<endl;
cleararray();
}

return p;
}

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧