资源简介
简易词法分析程序,用于学生学习时分析使用。可以更了解计算机底层工作。
代码片段和文件信息
#include
#include
#include
#define _KEY_WORD_END “waiting for your expanding“
typedef struct
{
int typenum;
char * word;
} WORD;
char input[255];
char token[255]=““;
int p_input;
int p_token;
char ch;
char* KEY_WORDS[]={“main““int““char““if““else““for““while“_KEY_WORD_END};
WORD* scaner();
void main()
{
int over=1;
WORD* oneword=new WORD;
printf(“Enter Your words(end with $):“);
scanf(“%[^$]s“input);
p_input=0;
printf(“Your words:\n%s\n“input);
while(over<1000&&over!=-1){
oneword=scaner();
if(oneword->typenum<1000)
printf(“(%d%s)“oneword->typenumoneword->word);
over=oneword->typenum;
}
printf(“\npress # to exit:“);
scanf(“%[^#]s“input);
}
char m_getch(){
ch=input[p_input];
p_input=p_input+1;
return (ch);
}
void getbc(){
while(ch==‘ ‘||ch==10){
ch=input[p_input];
p_input=p_input+1;
}
}
void concat(){
token[p_token]=ch;
p_token=p_token+1;
token[p_token]=‘\0‘;
}
int letter(){
if(ch>=‘a‘&&ch<=‘z‘||ch>=‘A‘&&ch<=‘Z‘)return 1;
else return 0;
}
int digit(){
if(ch>=‘0‘&&ch<=‘9‘)return 1;
else return 0;
}
int reserve(){
int i=0;
while(strcmp(KEY_WORDS[i]_KEY_WORD_END)){
if(!strcmp(KEY_WORDS[i]token)){
return i+1;
}
i=i+1;
}
return 10;
}
void retract(){
p_input=p_input-1;
}
char* dtb(){
return NULL;
}
WORD* scaner(){
WORD* myword=new WORD;
myword->typenum=10;
myword->word=““;
p_token=0;
m_getch();
getbc();
if(letter()){
while(letter()||digit()){
concat();
m_getch();
}
retract();
myword->typenum=reserve();
myword->word=token;
return(myword);
}
else if(digit()){
while(digit()){
concat();
m_getch();
}
retract();
myword->typenum=20;
myword->word=token;
return(myword);
}
else switch(ch){
case ‘=‘: m_getch();
if (ch==‘=‘){
myword->typenum=39;
myword->word=“==“;
return(myword);
}
retract();
myword->typenum=21;
myword->word=“=“;
return(myword);
bre
- 上一篇:webwatch官方版
- 下一篇:清华大学-高等数值分析-插值实验报告
相关资源
- 基于miniprogramwebpackloader的小程序脚手
-
Actionsc
ript 3.0 Game Programming Universit - 遗传编程代码genetic programming
- Concepts of Programming Languages 11th Edition英
- Dynamic programming Formulation of Micro-Grid
- The AWK Programming Language 中文版
- 《The C Programming Language》(答案)
- AresonMouseTestProgram
- ML编程语言
- Programming in C的课后习题答案
- Programming in Standard ML
- ENVIProgramGenerator
- Programming-Tool-PLC802
- 《集体智慧编程》(Programming Collect
-
gene ex
pression programming 编程基础 - Programming the Finite Element Method 5thEditi
- Extreme Programming Explained : Embrace Change
- IBM_PC汇编语言程序设计第二版(沈美
- 遗传算法的并行实现
- On Nonlinear Fractional Programming
- Programming Assignment 3: UDP Pinger Lab
- Introduction to 3D Game Programming with Direc
- An Introduction to Parallel Programming by Pet
- Programming Abstractions in C 的源码
- pintos project 2 user program详解
- Programming with Transactions SSD7Programming
- 高通救黑砖8064_msimage.mbn MPRG8064.hex r
- ncurses_programs.tar.gz
- 类型理论与函数式编程Type Theory and
- my-three-MiniProgram.zip
评论
共有 条评论