语法树的应用 草木瓜 (四) 源码见百度云盘
i=0;
while(i<=10){ print(i); i=i+1; }
print(i+i);
node.h
typedef enum { TYPE_CONTENT, TYPE_INDEX, TYPE_OP } NodeEnum;
typedef struct {
int name;
int num;
struct NodeTag * node[1];
} OpNode;
typedef struct NodeTag {
NodeEnum type;
union {
int content;
int index;
OpNode op;
};
} Node;
extern int Var[26];
lexya_e.l
%{
#include <stdlib.h>
#include "node.h"
#include "lexya_e.tab.h"
void yyerror(char *);
%}
%%
[a-z] { yylval.sIndex = *yytext - 'a'; return VARIABLE;}
[0-9]+ { yylval.iValue = atoi(yytext); return INTEGER;}
[()<>=+*/;{}.] { return *yytext;}
">=" return GE;
"<=" return LE;
"==" return EQ;
"!=" return NE;
"&&" return AND;
"||" return OR;
"while" return WHILE;
"if" return IF;
"else" return ELSE;
"print" return PRINT;
[\t\n]+ ;
. printf("unknow symbol:[%s]\n", yytext);
%%
int yywrap(void) {
return 1;
}
lexya_e.y
%{
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "node.h"
Node * opr(int name, int num, ...);
Node * set_index(int value);
Node * set_content(int value);
void freeNode(Node * p);
int exeNode(Node * p);
int yylexeNode(void);
void yyerror(char *s);
int Var[26];
%}
%union {
int iValue;
char sIndex;
Node *nPtr;
};
%token <iValue> VARIABLE
%token <sIndex> INTEGER
%token WHILE IF PRINT
%nonassoc IFX
%nonassoc ELSE
%left AND OR GE LE EQ NE '>' '<'
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
%type <nPtr> stmt expr stmt_list
%%
program:
function { exit(0); }
;
function:
function stmt { exeNode($2); freeNode($2); }
|
;
stmt:
';' { $$ = opr(';', 2, NULL, NULL); }
| expr ';' { $$ = $1; }
| PRINT expr ';' { $$ = opr(PRINT, 1, $2); }
| VARIABLE '=' expr ';' { $$ = opr('=', 2, set_index($1), $3); }
| WHILE '(' expr ')' stmt { $$ = opr(WHILE, 2, $3, $5); }
| IF '(' expr ')' stmt %prec IFX { $$ = opr(IF, 2, $3, $5); }
| IF '(' expr ')' stmt ELSE stmt %prec ELSE { $$ = opr(IF, 3, $3, $5, $7); }
| '{' stmt_list '}' { $$ = $2; }
;
stmt_list:
stmt { $$ = $1; }
| stmt_list stmt { $$ = opr(';', 2, $1, $2); }
;
expr:
INTEGER {$$ = set_content($1); }
| VARIABLE {$$ = set_index($1); }
| '-' expr %prec UMINUS {$$ = opr(UMINUS, 1, $2); }
| expr '+' expr {$$ = opr('+', 2, $1, $3); }
| expr '-' expr { $$ = opr('-', 2, $1, $3); }
| expr '*' expr { $$ = opr('*', 2, $1, $3); }
| expr '/' expr { $$ = opr('/', 2, $1, $3); }
| expr '<' expr { $$ = opr('<', 2, $1, $3); }
| expr '>' expr { $$ = opr('>', 2, $1, $3); }
| expr GE expr { $$ = opr(GE, 2, $1, $3); }
| expr LE expr { $$ = opr(LE, 2, $1, $3); }
| expr NE expr { $$ = opr(NE, 2, $1, $3); }
| expr EQ expr { $$ = opr(EQ, 2, $1, $3); }
| expr AND expr { $$ = opr(AND, 2, $1, $3); }
| expr OR expr { $$ = opr(OR, 2, $1, $3); }
| '(' expr ')' { $$ = $2; }
;
%%
#define SIZE_OF_NODE ((char *)&p->content - (char *)p)
Node *set_content(int value) {
Node *p;
size_t sizeNode;
sizeNode = SIZE_OF_NODE + sizeof(int);
if ((p = malloc(sizeNode)) == NULL)
yyerror("out of memory");
p->type = TYPE_CONTENT;
p->content = value;
return p;
}
Node *set_index(int value) {
Node *p;
size_t sizeNode;
sizeNode = SIZE_OF_NODE + sizeof(int);
if ((p = malloc(sizeNode)) == NULL)
yyerror("out of memory");
p->type = TYPE_INDEX;
p->index = value;
return p;
}
Node *opr(int name, int num, ...) {
va_list valist;
Node *p;
size_t sizeNode;
int i;
sizeNode = SIZE_OF_NODE + sizeof(OpNode) + (num - 1) * sizeof(Node*);
if ((p = malloc(sizeNode)) == NULL)
yyerror("out of memory");
p->type = TYPE_OP;
p->op.name = name;
p->op.num = num;
va_start(valist, num);
for (i = 0; i < num; i++)
p->op.node[i] = va_arg(valist, Node*);
va_end(valist);
return p;
}
void freeNode(Node *p) {
int i;
if (!p) return;
if (p->type == TYPE_OP) {
for (i = 0; i < p->op.num; i++)
freeNode(p->op.node[i]);
}
free (p);
}
void yyerror(char *s) {
fprintf(stdout, "%s\n", s);
}
int main(void) {
yyparse();
return 0;
}
parser.c
int exeNode(Node *p) {
if (!p) return 0;
switch (p->type) {
case TYPE_CONTENT: return p->content;
case TYPE_INDEX: return Var[p->index];
case TYPE_OP:
switch (p->op.name) {
case WHILE: while (exeNode(p->op.node[0]))exeNode(p->op.node[1]);
return 0;
case IF: if (exeNode(p->op.node[0]))
exeNode(p->op.node[1]);
else if (p->op.num > 2)
exeNode(p->op.node[2]);
return 0;
case PRINT: printf("%d\n", exeNode(p->op.node[0]));
return 0;
case ';': exeNode(p->op.node[0]);
return exeNode(p->op.node[1]);
case '=': return Var[p->op.node[0]->index] = exeNode(p->op.node[1]);
case UMINUS: return exeNode(p->op.node[0]);
case '+': return exeNode(p->op.node[0]) + exeNode(p->op.node[1]);
case '-': return exeNode(p->op.node[0]) - exeNode(p->op.node[1]);
case '*': return exeNode(p->op.node[0]) * exeNode(p->op.node[1]);
case '/': return exeNode(p->op.node[0]) / exeNode(p->op.node[1]);
case '<': return exeNode(p->op.node[0]) < exeNode(p->op.node[1]);
case '>': return exeNode(p->op.node[0]) > exeNode(p->op.node[1]);
case GE: return exeNode(p->op.node[0]) >= exeNode(p->op.node[1]);
case LE: return exeNode(p->op.node[0]) <= exeNode(p->op.node[1]);
case NE: return exeNode(p->op.node[0]) != exeNode(p->op.node[1]);
case EQ: return exeNode(p->op.node[0]) == exeNode(p->op.node[1]);
case AND: return exeNode(p->op.node[0]) && exeNode(p->op.node[1]);
case OR: return exeNode(p->op.node[0]) || exeNode(p->op.node[1]);
}
}
return 0;
}
- bison -d lexya_e.y
- lex -d lexya_e.l
- gcc -g -o parser lex.yy.c lexya_e.tab.c parser.c
- ./parser < input
再识语法树 草木瓜(五) 源代码有百度云存盘
node.h
typedef enum { TYPE_CONTENT, TYPE_INDEX, TYPE_OP } NodeEnum;
typedef struct {
int name;
int num;
struct NodeTag * node[1];
} OpNode;
typedef struct NodeTag {
NodeEnum type;
union {
float content;
int index;
OpNode op;
};
} Node;
struct VarIndex
{
float val;
char mark[10];
};
struct VarDefine
{
int index;
char * name;
};
#define USER_DEF_NUM 259 /* Yacc编译的保留字开始索引 */
#define MAX_VARS 100 /* 最多变量数 */
#define MAX_DEFS 20 /* 最多保留字数 */
#define MAX_BUFF_COLS 40 /* 分析语句最多行数 */
#define MAX_BUFF_ROWS 40 /* 分析语句每行最多字符数 */
extern struct VarIndex G_Var[MAX_VARS];
extern struct VarDefine G_Def[MAX_DEFS];
extern int G_iVarMaxIndex;
extern int G_iVarCurIndex;
extern char G_sBuff[MAX_BUFF_ROWS][MAX_BUFF_COLS];
extern int G_iBuffRowCount;
extern int G_iBuffColCount;
lexya_e.l
%{
#include <stdlib.h>
#include "node.h"
#include "lexya_e.tab.h"
struct VarDefine G_Def[MAX_DEFS];
char G_sBuff[MAX_BUFF_ROWS][MAX_BUFF_COLS];
int G_iBuffRowCount = 0;
int G_iBuffColCount = 0;
extern void add_var(char *);
void add_buff(char *);
void yyerror(char *);
%}
any .
%%
#{any}*[\n] {
add_buff(yytext);
G_iBuffColCount = 0;
G_iBuffRowCount++;
}
[\n] {
G_iBuffColCount = 0;
G_iBuffRowCount++;
}
"for" {
yylval.index = FOR - USER_DEF_NUM;
G_Def[yylval.index].name = "for";
add_buff(yytext);
return FOR;
}
"while" {
yylval.index = WHILE - USER_DEF_NUM;
G_Def[yylval.index].name = "while";
add_buff(yytext);
return WHILE;
}
"if" {
yylval.index = IF - USER_DEF_NUM;
G_Def[yylval.index].name = "if";
add_buff(yytext);
return IF;
}
"else" {
yylval.index = ELSE - USER_DEF_NUM;
G_Def[yylval.index].name = "else";
add_buff(yytext);
return ELSE;
}
"print" {
yylval.index = PRINT - USER_DEF_NUM ;
G_Def[yylval.index].name = "print";
add_buff(yytext);
return PRINT;
}
[a-zA-Z][a-zA-Z0-9]* {
add_var(yytext);
yylval.index = G_iVarCurIndex;
add_buff(yytext);
return VARIABLE;
}
[0-9]+ {
yylval.val = atof(yytext);
add_buff(yytext);
return NUMBER;
}
[0-9]*/.[0-9]+ {
yylval.val = atof(yytext);
add_buff(yytext);
return NUMBER;
}
"++" { yylval.index = ADD_T - USER_DEF_NUM; G_Def[yylval.index].name = "++"; G_Def[yylval.index + 1].name = "++"; add_buff(yytext); return ADD_T; }
"--" { yylval.index = MUS_T - USER_DEF_NUM; G_Def[yylval.index].name = "--"; G_Def[yylval.index + 1].name = "++"; add_buff(yytext); return MUS_T; }
">=" { yylval.index = GE - USER_DEF_NUM; G_Def[yylval.index].name = ">="; add_buff(yytext); return GE;}
"<=" { yylval.index = LE - USER_DEF_NUM; G_Def[yylval.index].name = "<="; add_buff(yytext); return LE;}
"==" { yylval.index = EQ - USER_DEF_NUM; G_Def[yylval.index].name = "=="; add_buff(yytext); return EQ;}
"!=" { yylval.index = NE - USER_DEF_NUM; G_Def[yylval.index].name = "!="; add_buff(yytext); return NE;}
"&&" { yylval.index = AND - USER_DEF_NUM; G_Def[yylval.index].name = "&&"; add_buff(yytext); return AND;}
"||" { yylval.index = OR - USER_DEF_NUM; G_Def[yylval.index].name = "||"; add_buff(yytext); return OR; }
[()<>=+/-*/;{}.] {
yylval.index = *yytext;
add_buff(yytext);
return *yytext;
}
[\t] { add_buff(yytext); }
[ ] { add_buff(yytext); }
{any} { printf("Ignore Unknow Symbol:[%s]/n", yytext); }
%%
void add_buff(char * buff) {
strcat(G_sBuff[G_iBuffRowCount], buff);
G_iBuffColCount = G_iBuffColCount + strlen(buff);
}
int yywrap(void) {
return 1;
}
lexya_e.y
%{
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "node.h"
Node * opr(int name, int num, ...);
Node * set_index(int value);
Node * set_content(float value);
void NodeFree(Node * p);
float NodeExecute(Node * p);
typedef union {
float val;
int index;
Node *node;
} yystype;
#define YYSTYPE yystype
void debug_vsp(YYSTYPE , char * , YYSTYPE *, char * );
void print_stmt();
void add_var(char *);
int G_iVarMaxIndex = 0;
int G_iVarCurIndex = -1;
struct VarIndex G_Var[MAX_VARS];
void yyerror(char *s);
%}
%union {
float val;
int index;
Node *node;
};
%token <val> NUMBER
%token <index> VARIABLE
%token PRINT
%token FOR WHILE
%nonassoc IF
%nonassoc ELSE
%left AND OR
%left GE LE EQ NE '>' '<'
%left '+' '-'
%left '*' '/'
%left ADD_T ADD_TT MUS_T MUS_TT
%nonassoc UMINUS
%type <node> stmt stmt_list expr_set expr_setself expr_comp expr
%%
program:
function { exit(0); }
;
function:
function stmt { NodeExecute($2); NodeFree($2); }
|
;
stmt:
';' {
$$ = opr(';', 2, NULL, NULL); debug_vsp(yyval, ";", yyvsp, "0");
}
| expr_set ';' {
$$ = $1; debug_vsp(yyval, "es;", yyvsp, "01");
}
| PRINT expr ';' {
$$ = opr(PRINT, 1, $2); debug_vsp(yyval, "p(e);", yyvsp, "401");
}
| PRINT expr_set ';' {
$$ = opr(PRINT, 1, $2); debug_vsp(yyval, "p(es);", yyvsp, "401");
}
| FOR '(' expr_set ';' expr_comp ';' expr_set ')' stmt {
$$ = opr(FOR, 4, $3, $5, $7, $9); debug_vsp(yyval, "for(es;ec;es) st", yyvsp, "410101010"); }
| WHILE '(' expr_comp ')' stmt {
$$ = opr(WHILE, 2, $3, $5); debug_vsp(yyval, "while(ec) st", yyvsp, "41010"); }
| IF '(' expr_comp ')' stmt %prec IF {
$$ = opr(IF, 2, $3, $5); debug_vsp(yyval, "if(ec) st", yyvsp, "41010"); }
| IF '(' expr_comp ')' stmt ELSE stmt %prec ELSE {
$$ = opr(IF, 3, $3, $5, $7); debug_vsp(yyval, "if(ec)else st", yyvsp, "4101040"); }
| '{' stmt_list '}' { $$ = $2; debug_vsp(yyval, "{stl}", yyvsp, "101"); }
;
stmt_list:
stmt {
$$ = $1; debug_vsp(yyval, "st", yyvsp, "0"); }
| stmt_list stmt {
$$ = opr(';', 2, $1, $2); debug_vsp(yyval, "stl st", yyvsp, "00"); }
;
expr_set:
VARIABLE '=' expr {
$$ = opr('=', 2, set_index($1), $3); debug_vsp(yyval, "v=e", yyvsp, "210"); }
| VARIABLE '=' expr_setself {
$$ = opr('=', 2, set_index($1), $3); debug_vsp(yyval, "v=ess", yyvsp, "210"); }
| expr_setself
;
expr_setself:
ADD_T VARIABLE {
$$ = opr(ADD_T, 1, set_index($2)); debug_vsp(yyval, "++v", yyvsp, "42"); }
| MUS_T VARIABLE {
$$ = opr(MUS_T, 1, set_index($2)); debug_vsp(yyval, "--v", yyvsp, "42"); }
| VARIABLE ADD_T {
$$ = opr(ADD_TT, 1, set_index($1)); debug_vsp(yyval, "v++", yyvsp, "24"); }
| VARIABLE MUS_T {
$$ = opr(MUS_TT, 1, set_index($1)); debug_vsp(yyval, "v--", yyvsp, "24"); }
| '(' expr_setself ')' { $$ = $2; debug_vsp(yyval, "(ess)", yyvsp, "101"); }
;
expr_comp:
expr '<' expr {
$$ = opr('<', 2, $1, $3); debug_vsp(yyval, "e<e", yyvsp, "010"); }
| expr '>' expr {
$$ = opr('>', 2, $1, $3); debug_vsp(yyval, "e>e", yyvsp, "010"); }
| expr GE expr {
$$ = opr(GE, 2, $1, $3); debug_vsp(yyval, "e>=e", yyvsp, "040"); }
| expr LE expr {
$$ = opr(LE, 2, $1, $3); debug_vsp(yyval, "e<=e", yyvsp, "040"); }
| expr NE expr {
$$ = opr(NE, 2, $1, $3); debug_vsp(yyval, "e!=e", yyvsp, "040"); }
| expr EQ expr {
$$ = opr(EQ, 2, $1, $3); debug_vsp(yyval, "e==e", yyvsp, "040"); }
| expr_comp AND expr_comp {
$$ = opr(AND, 2, $1, $3); debug_vsp(yyval, "ec&&ec", yyvsp, "040"); }
| expr_comp OR expr_comp {
$$ = opr(OR, 2, $1, $3); debug_vsp(yyval, "ec||ec", yyvsp, "040"); }
| '(' expr_comp ')' { $$ = $2; debug_vsp(yyval, "(ec)", yyvsp, "101"); }
;
expr:
NUMBER {
$$ = set_content($1); debug_vsp(yyval, "f", yyvsp, "3"); }
| VARIABLE {
$$ = set_index($1); debug_vsp(yyval, "v", yyvsp, "2"); }
| '-' NUMBER %prec UMINUS {
$$ = set_content(-$2); debug_vsp(yyval, "-e", yyvsp, "13"); }
| expr '+' expr {
$$ = opr('+', 2, $1, $3); debug_vsp(yyval, "e+e", yyvsp, "010"); }
| expr '-' expr {
$$ = opr('-', 2, $1, $3); debug_vsp(yyval, "e-e", yyvsp, "010"); }
| expr '*' expr {
$$ = opr('*', 2, $1, $3); debug_vsp(yyval, "e*e", yyvsp, "010"); }
| expr '/' expr {
$$ = opr('/', 2, $1, $3); debug_vsp(yyval, "e/e", yyvsp, "010"); }
| '(' expr ')' {
$$ = $2; debug_vsp(yyval, "(e)", yyvsp, "101");
}
;
%%
#define SIZE_OF_NODE ((char *)&p->content - (char *)p)
Node *set_content(float value) {
Node *p;
size_t sizeNode;
sizeNode = SIZE_OF_NODE + sizeof(float);
if ((p = malloc(sizeNode)) == NULL)
yyerror("out of memory");
p->type = TYPE_CONTENT;
p->content = value;
return p;
}
Node *set_index(int value) {
Node *p;
size_t sizeNode;
sizeNode = SIZE_OF_NODE + sizeof(int);
if ((p = malloc(sizeNode)) == NULL)
yyerror("out of memory");
p->type = TYPE_INDEX;
p->index = value;
return p;
}
Node *opr(int name, int num, ...) {
va_list valist;
Node *p;
size_t sizeNode;
int i;
sizeNode = SIZE_OF_NODE + sizeof(OpNode) + (num - 1) * sizeof(Node*);
if ((p = malloc(sizeNode)) == NULL)
yyerror("out of memory");
p->type = TYPE_OP;
p->op.name = name;
p->op.num = num;
va_start(valist, num);
for (i = 0; i < num; i++)
p->op.node[i] = va_arg(valist, Node*);
va_end(valist);
return p;
}
void debug_vsp(YYSTYPE yval, char * info, YYSTYPE * vsp, char * mark) {
#ifdef PARSE_DEBUG
printf("/n -RULE 0x%x %s /n ", yval.node, info );
int i;
int ilen = strlen(mark);
for (i = 1 - ilen; i <= 0; i++) {
switch (mark[ilen + i - 1]) {
case '0':
printf(" [ 0x%x ", vsp[i].node);
switch (vsp[i].node->type) {
case TYPE_CONTENT:
printf("%g ] ", vsp[i].node->content);
break;
case TYPE_INDEX:
printf("%s ] ", G_Var[vsp[i].node->index].mark);
break;
case TYPE_OP:
if (vsp[i].node->op.name < USER_DEF_NUM)
printf("%c ] ", vsp[i].node->op.name);
else
printf("%s ] ", G_Def[vsp[i].node->op.name - USER_DEF_NUM].name);
break;
}
break;
case '1':
printf(" %c ", vsp[i].index);
break;
case '2':
printf(" %s ", G_Var[vsp[i].index].mark);
break;
case '3':
printf(" %g ", vsp[i].val);
break;
case '4':
printf(" %s ", G_Def[vsp[i].index].name);
break;
}
}
printf("/n");
print_stmt();
#endif
}
void add_var(char *mark) {
if (G_iVarMaxIndex == 0) {
strcpy(G_Var[0].mark, mark);
G_iVarMaxIndex++;
G_iVarCurIndex = 0;
return;
}
int i;
for (i = 0; i <= G_iVarMaxIndex - 1; i++) {
if (strcmp(G_Var[i].mark, mark) == 0) {
G_iVarCurIndex = i;
return;
}
}
strcpy(G_Var[G_iVarMaxIndex].mark, mark);
G_iVarCurIndex = G_iVarMaxIndex;
G_iVarMaxIndex++;
}
void print_stmt() {
printf(" -STMT: /n");
if (G_iBuffColCount == 0)
printf(" %s /n", G_sBuff[G_iBuffRowCount - 1]);
else
printf(" %s /n", G_sBuff[G_iBuffRowCount]);
printf("/n");
}
void NodeFree(Node *p) {
int i;
if (!p) return;
if (p->type == TYPE_OP) {
for (i = 0; i < p->op.num; i++)
NodeFree(p->op.node[i]);
}
free (p);
}
void yyerror(char *s) {
printf("<Parser Error> Line %d ,Col %d /n", G_iBuffRowCount + 1, G_iBuffColCount + 1);
printf(" %s/n", G_sBuff[G_iBuffRowCount]);
}
int main(void) {
yyparse();
return 0;
}
parser.c
#include <stdio.h>
#include "node.h"
#include "lexya_e.tab.h"
float NodeExecute(Node *p) {
if (!p) return 0;
switch (p->type) { case TYPE_CONTENT: return p->content; case TYPE_INDEX: return G_Var[p->index].val; case TYPE_OP: switch (p->op.name) { case WHILE: while (NodeExecute(p->op.node[0]))NodeExecute(p->op.node[1]); return 0; case FOR: NodeExecute(p->op.node[0]); while (NodeExecute(p->op.node[1])) { NodeExecute(p->op.node[3]); NodeExecute(p->op.node[2]); }
return 0;
case IF: if (NodeExecute(p->op.node[0]))
NodeExecute(p->op.node[1]);
else if (p->op.num > 2)
NodeExecute(p->op.node[2]);
return 0;
case PRINT: printf("%g\n", NodeExecute(p->op.node[0]));
return 0;
case ';': NodeExecute(p->op.node[0]);
return NodeExecute(p->op.node[1]);
case '=': return G_Var[p->op.node[0]->index].val = NodeExecute(p->op.node[1]);
case UMINUS: return NodeExecute(p->op.node[0]);
case '+': return NodeExecute(p->op.node[0]) + NodeExecute(p->op.node[1]);
case '-': return NodeExecute(p->op.node[0]) - NodeExecute(p->op.node[1]);
case '*': return NodeExecute(p->op.node[0]) * NodeExecute(p->op.node[1]);
case '/': return NodeExecute(p->op.node[0]) / NodeExecute(p->op.node[1]);
case '<': return NodeExecute(p->op.node[0]) < NodeExecute(p->op.node[1]);
case '>': return NodeExecute(p->op.node[0]) > NodeExecute(p->op.node[1]);
case GE: return NodeExecute(p->op.node[0]) >= NodeExecute(p->op.node[1]);
case LE: return NodeExecute(p->op.node[0]) <= NodeExecute(p->op.node[1]);
case NE: return NodeExecute(p->op.node[0]) != NodeExecute(p->op.node[1]);
case EQ: return NodeExecute(p->op.node[0]) == NodeExecute(p->op.node[1]);
case AND: return NodeExecute(p->op.node[0]) && NodeExecute(p->op.node[1]);
case OR: return NodeExecute(p->op.node[0]) || NodeExecute(p->op.node[1]);
case ADD_T: return ++G_Var[p->op.node[0]->index].val;
case MUS_T: return
case ADD_TT: return G_Var[p->op.node[0]->index].val++;
case MUS_TT: return G_Var[p->op.node[0]->index].val
}
}
return 0;
}
k=9;
if((1>1)||(-9>-1))
for(i=0;i<=9;i=i+1)
print(i);
else
if(3>1&&2>1) {
for(j=-1.1;j<=3;j++)
print(j);
for(jdd=1;jdd<=3;++jdd)
print(jdd);
while(k<=9) {
print(k++);
print(++k);
}
}
功能介绍
- bison -d lexya_e.y
- lex -d lexya_e.l
- gcc -g -o parser lex.yy.c lexya_e.tab.c parser.c
- ./parser < input
- 以上示例显然是根据《Lex和Yacc应用教程(四).语法树》文中的示例扩展而来。主要演示C语言类似的语法编译方法。支持的功能如下:
1. 支持整型和浮点型
2. 支持变量存储,变量名可为多个字符
3. 支持+-*/()=运算法则
4. 支持负数及负数运算
5. 支持变量的自加(++)和自减运算(
6. 支持print打印值和变量
7. 支持for while if else控制结构,并支持控制结构的嵌套
8. 支持>= <= != ==四种比较运算
9. 支持&& ||的复合比较运算
10. 支持对空格和TAB的忽略处理
11. 支持
12. 支持{}多重组合
13. 支持编译错误的具体显示
14. 支持编译过程的变量堆栈信息打印,便于调试分析
15. 支持保留字的存储显示。
16. 支持语法树打印(将在下一篇文章着重说明)