可以简单分析赋值语句 如:char a = 10;
生成AST树
const char* const AST_string[] = {
"CharDeclaration",
"AssignmentExp",
"IntLiteral",
};
enum syntactic_parser_types{
CharDeclaration = 0,
AssignmentExp,
Ast_IntLiteral,
}syntactic_parser_type;
typedef Save_Token_t tokens_fun_t(void);
typedef struct tokens_fun{
tokens_fun_t *peek;
tokens_fun_t *read;
}Token_fun;
static int save_token_read_index = 0;
Save_Token_t token_peek(void){
if(save_token_read_index >= save_token_index){
//return NULL;
}
return Mytokens[save_token_read_index];
}
Save_Token_t token_read(void){
if(save_token_read_index >= save_token_index){
//return NULL;
}
return Mytokens[save_token_read_index++];
}
typedef struct SimpleASTNode{
enum syntactic_parser_types type;
char text[Id_LENGTH];
int level;
}SimpleASTNode_t;
SimpleASTNode_t AST_node[20] = {};
static int AST_index = 0;
void add_AST(enum syntactic_parser_types type,char* text,int level){
AST_node[AST_index].type = type;
strcpy(AST_node[AST_index].text,text);
AST_node[AST_index].level = level;
AST_index++;
}
void printf_AST(){
int i,j;
printf("\r\n=====syntactic_parser======\r\n\r\n");
for( i = 0; i < AST_index; i++){
for(j = 0; j < (AST_node[i].level-1);j++){
//printf("\t");
printf(" ");
}
printf("%s %s\r\n",AST_string[AST_node[i].type],AST_node[i].text);
}
}
int syntactic_parser(){
Save_Token_t token;
Token_fun tokens_fun;
tokens_fun.peek = token_peek;
tokens_fun.read = token_read;
token = tokens_fun.peek();//预读
//if((token != NULL) && (token.type == CHAR)){
if( (token.type == CHAR)){
token = tokens_fun.read();//消耗掉char
token = tokens_fun.peek();
if(token.type == Id){
token = tokens_fun.read();
//创建当前节点,并把变量名记到AST节点的文本值中,
add_AST(CharDeclaration,token.text,1);
token = tokens_fun.peek();//预读
if(token.type == Assignment){
add_AST(AssignmentExp,token.text,2);
token = tokens_fun.read();
token = tokens_fun.peek();//预读
if(token.type == IntLiteral){
token = tokens_fun.read();
add_AST(Ast_IntLiteral,token.text,3);
}
}
}
}
printf_AST();
}
int main(){
Lexical_analyzer();
syntactic_parser();
}
生成AST树
const char* const AST_string[] = {
"CharDeclaration",
"AssignmentExp",
"IntLiteral",
};
enum syntactic_parser_types{
CharDeclaration = 0,
AssignmentExp,
Ast_IntLiteral,
}syntactic_parser_type;
typedef Save_Token_t tokens_fun_t(void);
typedef struct tokens_fun{
tokens_fun_t *peek;
tokens_fun_t *read;
}Token_fun;
static int save_token_read_index = 0;
Save_Token_t token_peek(void){
if(save_token_read_index >= save_token_index){
//return NULL;
}
return Mytokens[save_token_read_index];
}
Save_Token_t token_read(void){
if(save_token_read_index >= save_token_index){
//return NULL;
}
return Mytokens[save_token_read_index++];
}
typedef struct SimpleASTNode{
enum syntactic_parser_types type;
char text[Id_LENGTH];
int level;
}SimpleASTNode_t;
SimpleASTNode_t AST_node[20] = {};
static int AST_index = 0;
void add_AST(enum syntactic_parser_types type,char* text,int level){
AST_node[AST_index].type = type;
strcpy(AST_node[AST_index].text,text);
AST_node[AST_index].level = level;
AST_index++;
}
void printf_AST(){
int i,j;
printf("\r\n=====syntactic_parser======\r\n\r\n");
for( i = 0; i < AST_index; i++){
for(j = 0; j < (AST_node[i].level-1);j++){
//printf("\t");
printf(" ");
}
printf("%s %s\r\n",AST_string[AST_node[i].type],AST_node[i].text);
}
}
int syntactic_parser(){
Save_Token_t token;
Token_fun tokens_fun;
tokens_fun.peek = token_peek;
tokens_fun.read = token_read;
token = tokens_fun.peek();//预读
//if((token != NULL) && (token.type == CHAR)){
if( (token.type == CHAR)){
token = tokens_fun.read();//消耗掉char
token = tokens_fun.peek();
if(token.type == Id){
token = tokens_fun.read();
//创建当前节点,并把变量名记到AST节点的文本值中,
add_AST(CharDeclaration,token.text,1);
token = tokens_fun.peek();//预读
if(token.type == Assignment){
add_AST(AssignmentExp,token.text,2);
token = tokens_fun.read();
token = tokens_fun.peek();//预读
if(token.type == IntLiteral){
token = tokens_fun.read();
add_AST(Ast_IntLiteral,token.text,3);
}
}
}
}
printf_AST();
}
int main(){
Lexical_analyzer();
syntactic_parser();
}