pl0 词法分析
谁有C++写的pl0词法分析,发我一份,只要词法分析的。谢
lovelydavid@hotmail.com
问题点数:100、回复次数:2Top
1 楼o1n(小毛子)回复于 2004-12-03 23:14:48 得分 100
#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;
}Top
2 楼greenteanet(扎扎实实打基础,保持一颗平常心。)回复于 2004-12-03 23:17:26 得分 0
不好意思,我没有Top




