无论是词法分析,还是语法分析,给我的第一感觉就是逻辑要严谨。由于项目有自己一套完整的语言和语法,设计好其对应的词法分析器和语法分析器显得尤为重要。
这里给您提供一个免费的java词法分析器下载
java词法分析器实现原理步骤
先写其正则式,然后NFA,然后DFA,然后对其进行优化,最后准备工作做好了,就可以开始写代码了。
下面对其里面的主要函数进行讲解:
enum Token_Type {
keyword =1, //关键字
Identifier, //标识符
operatorr, //运算符
operatorrd, //单运算符
constant, //常量
escape, //转义符
separator, //界限符
notype, //没有类型
zhushi //注释类型
};
对其单词的类型定义成枚举。
typedef struct Token {
Token_Type type; //其类型
char *lexeme ; //字符串
int value; //属性值
}Token;
//返回单词的结构。
void InitScanner(char *ch){
fp1 = fopen(ch,"r+");
LineNo = 1;
}//初始化分析器。
void CloseScanner(){
fclose(fp1);
}//关闭分析器
void EmptyTokenString(){
memset(TokenBuffer,0,100);
}//清空缓冲区
static void AddCharTokenString(char Char)
{
int TokenLenth=strlen(TokenBuffer);
if(TokenLenth+1==sizeof(TokenBuffer)) return;
TokenBuffer[TokenLenth]=Char;
TokenBuffer[TokenLenth+1]='\0';
} //将一个字符添加到缓冲区
static char GetChar(){
char Char;
Char = fgetc(fp1);
return Char;
}//从文件中读取一个字符
static void BackChar(char Char) {
if(Char!=EOF){
ungetc(Char,fp1);
}
}//将其字符后退一个。
static Token JudgeKeyToken(char *IDstring);//这个函数实现对关键字进行判断
下面是源代码:
/************************************************************************
* CopyRight@ice-snow studio *
* email:bingxuefenggu@126.com *
* qq: 405116890 *
************************************************************************/
//只考虑到整形常量
//实型没考虑到
//需要解决转义符如何描述的问题
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define TOKEN_LEN 100
enum Token_Type {
keyword =1, //关键字
Identifier, //标识符
operatorr, //运算符
operatorrd, //单运算符
constant, //常量
escape, //转义符
separator, //界限符
notype, //没有类型
zhushi //注释类型
};
typedef struct Token {
Token_Type type; //其类型
char *lexeme ; //字符串
int value; //属性值
}Token;
static Token Token_Tap[] = {
{keyword,"abstract",0x103},
{keyword,"boolean",0x103},
{keyword,"break", 0x103},
{keyword,"byte" ,0x103},
{keyword,"case",0x103},
{keyword,"catch",0x103},
{keyword,"char", 0x103},
{keyword, "class",0x103},
{keyword,"const" ,0x103},
{keyword,"continue" ,0x103},
{keyword,"default" , 0x103},
{keyword,"do",0x103},
{keyword,"double",0x103},
{keyword,"else",0x103},
{keyword,"extends",0x103},
{keyword,"false",0x103},
{keyword,"final",0x103},
{keyword,"finally",0x103},
{keyword,"float",0x103},
{keyword,"for",0x103},
{keyword,"goto",0x103},
{keyword,"if",0x103},
{keyword,"implements",0x103},
{keyword,"import",0x103},
{keyword,"instanceof",0x103},
{keyword,"int",0x103},
{keyword,"interface",0x103},
{keyword,"long",0x103},
{keyword,"native",0x103},
{keyword,"new",0x103},
{keyword,"null",0x103},
{keyword,"package",0x103},
{keyword,"private",0x103},
{keyword,"protected",0x103},
{keyword,"public",0x103},
{keyword,"return",0x103},
{keyword,"short",0x103},
{keyword,"static",0x103},
{keyword,"super",0x103},
{keyword,"switch",0x103},
{keyword,"synchronized",0x103},
{keyword,"this",0x103},
{keyword,"throw",0x103},
{keyword,"throws",0x103},
{keyword,"transient",0x103},
{keyword,"true",0x103},
{keyword,"try", 0x103},
{keyword,"void",0x103},
{keyword,"volatile",0x103},
{keyword,"while",0x103},
{constant,"true",0x105},
{constant,"false",0x105}
//以上是所有的关键字
};
FILE *fp1;// 要读文件的描述符
int LineNo;//单词所在的行数
static char TokenBuffer[TOKEN_LEN];//单词符号的缓存区
void InitScanner(char *ch){
fp1 = fopen(ch,"r+");
LineNo = 1;
}
void CloseScanner(){
fclose(fp1);
}
void EmptyTokenString(){
memset(TokenBuffer,0,100);
}
static void AddCharTokenString(char Char)
{
int TokenLenth=strlen(TokenBuffer);
if(TokenLenth+1==sizeof(TokenBuffer)) return;
TokenBuffer[TokenLenth]=Char;
TokenBuffer[TokenLenth+1]='\0';
}
static char GetChar(){
char Char;
Char = fgetc(fp1);
return Char;
}
static void BackChar(char Char) {
if(Char!=EOF){
ungetc(Char,fp1);
}
}
static Token JudgeKeyToken(char *IDstring){
int loop;
Token token;
for(loop=0;loop<51;loop++){
if(strcmp(Token_Tap[loop].lexeme,IDstring)==0)
return Token_Tap[loop];
}
token.type = Identifier;
//strcpy(token.lexeme,IDstring);//youwenti.
token.lexeme = IDstring;
token.value = 0x104;
return token;
}
- PC官方版
- 安卓官方手机版
- IOS官方手机版