求一简单词法分析程序

ccpp1982 2004-05-06 05:47:05
随便什么语法都行,只是想看看究竟是怎么做的
...全文
85 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Cathryn0000000 2004-05-06
  • 打赏
  • 举报
回复
#define BUFLEN 256 /* 读入行缓冲区大小 */
#define MAXTOKENLEN 20 /* 存放表达式字符数组大小 */

#define ENDFILE 0 /* 文件结束标志 */
#define ERROR 1 /* 出错标志 */


#define MAIN 2 /* 关键字main */
#define IF 3 /* 关键字if */
#define WHILE 4 /* 关键字while */
#define ID 5 /* 标识符 */
#define NUM 6 /* 数字 */
#define ASSIGN 7 /* = */
#define EQ 8 /* == */
#define NE 9 /* != */
#define MORE 10 /* > */
#define ME 11 /* >= */
#define LESS 12 /* < */
#define LE 13 /* <= */
#define LBRACKET 14 /* { */
#define RBRACKET 15 /* } */
#define PLUS 16 /* + */
#define MINUS 17 /* - */
#define TIMES 18 /* * */
#define OVER 19 /* / */
#define LPAREN 20 /* ( */
#define RPAREN 21 /* ) */
#define SEMI 22 /* ; */

#include <stdio.h>
#include <ctype.h>
#include <conio.h>


typedef enum /* 状态集 */
{
START, INEQULE, INNEQULE, INMORE, INLESS, INNUM, INID, DONE
} StateType;


char tokenString[MAXTOKENLEN + 1];

static char lineBuf[BUFLEN];
static int linepos = 0;
static int lineno = 0;
static int bufsize = 0;
FILE *fpin,*fpout;

static void openInputFile(){
if((fpin=fopen("in.txt","r"))==NULL){
printf("open InputFile error!!");
exit(-1);
}
}

static void openOutputFile(){
if((fpout=fopen("out.txt","w"))==NULL){
printf("open OutputFile error!!");
exit(-1);
}
}

static char getNextChar(void)
{
if ( !(linepos < bufsize) ) {
lineno++;
if ( fgets(lineBuf, BUFLEN - 1, fpin) ) {
bufsize = strlen(lineBuf);
linepos = 0;
return lineBuf[linepos++];
}
else {
return EOF;
}
}
else
return lineBuf[linepos++];
}

static void ungetNextChar(void)
{
linepos--;
}


static struct
{
char* str;
int tok;
} reservedWords[3] =
{ {"main",MAIN},
{"if", IF },
};
int getToken(void)
{
int tokenStringIndex = 0;
int currentToken;
StateType state = START;
int save;
static j=0;

while (state != DONE) {
char c = getNextChar();
save = 1;

switch (state) {
case START:
if (isdigit(c))
state = INNUM;
else if (isalpha(c))
state = INID;
else if (c == '=')
state = INEQULE;
else if (c == '>')
state =INMORE;
else if (c == '<')
state =INLESS;
else if (c == '!')
state =INNEQULE;
else if ( (c == ' ') || (c == '\t') || (c == '\n') )
save = 0;
else {
state = DONE;
switch (c) {
case EOF:
save = 0;
currentToken = ENDFILE;
break;
case '+':
currentToken = PLUS;
break;
case '-':
currentToken = MINUS;
break;
case '*':
currentToken = TIMES;
break;
case '/':
currentToken = OVER;
break;
case '(':
currentToken = LPAREN;
break;
case ')':
currentToken = RPAREN;
break;
case ';':
currentToken = SEMI;
break;
case '{':
currentToken = LBRACKET;
break;
case '}':
currentToken = RBRACKET;
break;
default:

currentToken = ERROR;
break;
}
}
break;
case INEQULE:
state = DONE;
if (c == '=')
currentToken = EQ;
else {
ungetNextChar();
save = 0;
currentToken = ASSIGN;
}
break;
case INNEQULE:
state = DONE;
if (c == '=')
currentToken = NE;
else {
ungetNextChar();
save = 0;
currentToken = ERROR;
}
break;
case INMORE:
state = DONE;
if (c == '=')
currentToken = ME;
else {
ungetNextChar();
save = 0;
currentToken = MORE;
}
break;
case INLESS:
state = DONE;
if (c == '=')
currentToken = LE;
else {
ungetNextChar();
save = 0;
currentToken = LESS;
}
break;

case INNUM:
if (!isdigit(c)) {
if(!isalpha(c)){
ungetNextChar();
save = 0;
state = DONE;
currentToken = NUM;
}
else{
save = 1;
state = DONE;
currentToken =ERROR;
}
}
break;

case DONE:
default:

state = DONE;
currentToken = ERROR;
break;
}

if ( (save) && (tokenStringIndex <= MAXTOKENLEN) )
tokenString[tokenStringIndex++] = c;
if ( state == DONE) {
tokenString[tokenStringIndex] = '\0';
if (currentToken == ID)
currentToken = reservedLookup(tokenString);
}

}

if (currentToken!=ERROR) {
if(currentToken){
printf("(%d,%s) ",currentToken,tokenString);
fprintf(fpout,"(%d,%s)",currentToken,tokenString);
if(!(++j%10)) printf("\n");
}
else {
gotoxy(1,24);
printf("Succeed! Result been put into 'out.txt'.");
printf("\nPress any key to continue... ");
getch();
}
}
else {
gotoxy(1,24);
printf("Illegal character '%s' (Row %d,Column %d)",tokenString,lineno,linepos);
printf("\nPress any key to continue... ");
window(linepos,lineno+2,linepos,lineno+2);
textbackground(4);
clrscr();
printf("%c",tokenString[tokenStringIndex-1]);
textbackground(0);
fclose(fpin);
fclose(fpout);
getch();
exit(-1);
}


return currentToken;
}



void main(){
textbackground(0);
clrscr();
openInputFile();
openOutputFile();
while(getToken());
fclose(fpin);
fclose(fpout);
}
输入为in.txt中的程序,输出到out.txt,为二元组
附录c 编译程序实验 实验目的:用c语言对一个简单语言的子集编制一个一遍扫描的编译程序,以加深对编译原理的理解,掌握编译程序的实现方法和技术。 语法分析 C2.1 实验目的 编制一个递归下降分析程序,实现对词法分析程序所提供的单词序列的语法检查和结构分析. C2.2 实验要求 利用C语言编制递归下降分析程序,并对简单语言进行语法分析. C2.2.1待分析的简单语言的语法 实验目的 通过上机实习,加深对语法制导翻译原理的理解,掌握将语法分析所识别的语法成分变换为中间代码的语义翻译方法. 实验要求 采用递归下降语法制导翻译法,对算术表达式、赋值语句进行语义分析并生成四元式序列。 实验的输入和输出 输入是语法分析提供的正确的单词串,输出为三地址指令形式的四元式序列。 例如:对于语句串 begin a:=2+3*4;x:=(a+b)/c end# 输出的三地址指令如下: (1) t1=3*4 (2) t2=2+t1 (3) a=t2 (4) t3=a+b (5) t4=t3/c (6) x=t4 算法思想 1设置语义过程 (1) emit(char *result,char *arg1,char *op,char *ag2) 该函数功能是生成一个三地址语句送到四元式表中。 四元式表的结构如下: struct {char result[8]; char ag1[8]; char op[8]; char ag2[8]; }quad[20]; (2)char *newtemp() 该函数回送一个新的临时变量名,临时变量名产生的顺序为T1,T2,…. Char *newtemp(void) { char *p; char m[8]; p=(char *)malloc(8); k++; itoa(k,m,10); strcpy(p+1,m); p[0]=’t’; return(p); } (2)主程序示意图如图c.10所示。 (2) 函数lrparser在原来语法分析的基础上插入相应的语义动作:将输入串翻译成四元式序列。在实验中我们只对表达式、赋值语句进行翻译。 语义分析程序的C语言程序框架 int lrparser() { int schain=0; kk=0; if(syn=1) { 读下一个单词符号; schain=yucu; /调用语句串分析函数进行分析/ if(syn=6) { 读下一个单词符号; if(syn=0 && (kk==0)) 输出(“success”); } else { if(kk!=1 ) 输出 ‘缺end’ 错误;kk=1;} else{输出’begin’错误;kk=1;} } return(schain); int yucu() { int schain=0; schain=statement();/调用语句分析函数进行分析/ while(syn=26) {读下一个单词符号; schain=statement(); /调用语句分析函数进行分析/ } return(schain); } int statement() { char tt[8],eplace[8]; int schain=0; {switch(syn) {case 10: strcpy(tt,token); scanner(); if(syn=18) {读下一个单词符号; strcpy(eplace,expression()); emit(tt,eplace,””,””); schain=0; } else {输出’缺少赋值号’的错误;kk=1; } return(schain); break; } } char *expression(void) {char *tp,*ep2,*eplace,*tt; tp=(char *)malloc(12);/分配空间/ ep2=(char *)malloc(12); eplace=(char *)malloc(12); tt =(char )malloc(12); strcpy(eplace,term ());/调用term分析产生表达式计算的第一项eplace/ while(syn=13 or 14) { 操作符 tt= ‘+’或者‘—’; 读下一个单词符号; strcpy(ep2,term());/调用term分析产生表达式计算的第二项ep2/ strcpy(tp,newtemp());/调用newtemp产生临时变量tp存储计算结果/ emit(tp,eplace,tt,ep2);/生成四元式送入四元式表/ strcpy(eplace,tp); } return(eplace); } char *term(void)/仿照函数expression编写/ char *factor(void) {char *fplace; fplace=(char *)malloc(12); strcpy(fplace, “ ”); if(syn=10) {strcpy(fplace,,token);/将标识符token的值赋给fplace/ 读下一个单词符号; } else if(syn=11) {itoa(sum,fplace,10); 读下一个单词符号; } else if (syn=27) {读下一个单词符号; fplace=expression();/调用expression分析返回表达式的值/ if(syn=28) 读下一个单词符号; else{输出‘}’错误;kk=1; } } else{输出‘(’错误;kk=1; } return(fplace); }

70,025

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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