flex bison:创建多字符变量

flex bison:创建多字符变量,bison,flex-lexer,Bison,Flex Lexer,我想创建一种由多个字符变量(例如abc=10,num=120)组成的编程语言。我能够创建单字符变量。.y代码为: %{ #include <stdio.h> //char sym[1000]; //int x=0; int sym[26]; %} %token NUMBER ADD SUB MUL DIV ABS EOL ID ASS %% calclist : | calclist exp EOL {

我想创建一种由多个字符变量(例如abc=10,num=120)组成的编程语言。我能够创建单字符变量。.y代码为:

%{
    #include <stdio.h>
                  //char sym[1000];
    //int x=0;
    int sym[26];

%}


%token NUMBER ADD SUB MUL DIV ABS EOL ID ASS
%%
calclist : 
    | calclist exp EOL   { printf("= %d\n", $2); } 
    | ID ASS exp EOL     { sym[$1] = $3;

             }
;
exp:    factor           { $$=$1; }
    | exp ADD factor     { $$ = $1 + $3; }
    | exp SUB factor     { $$ = $1 - $3; }
;
factor :    term         { $$=$1; }
    | factor MUL term    { $$ = $1 * $3; }
    | factor DIV term    { $$ = $1 / $3; }
;
term :  NUMBER       { $$=$1; }

;

%%
int main(int argc, char **argv)
{
yyparse();
}
yyerror(char *s)
{
fprintf(stderr, "error: %s\n", s);
} 
%{
# include "P3.tab.h"
#include <stdio.h>
#include <stdlib.h>
extern int yylval;
//int m=0;
%}

%%
"+"     { return ADD; }
"-"     { return SUB; }
"*"  { return MUL; }
"/"     { return DIV; }
"=" { return ASS; }
[a-z]+  { yylval= *yytext  - 'a' ;  
     return ID ; }
[0-9]+  { yylval = atoi(yytext); return NUMBER; }
\n   { return EOL; }
[ \t]   { /* ignore whitespace */ }
.    { printf("Mystery character %c\n", *yytext); }
%%
int yywrap()
{
return 1;
}
%{
#包括
//字符符号[1000];
//int x=0;
int-sym[26];
%}
%令牌编号添加子MUL DIV ABS下线ID ASS
%%
calclist:
|calclist exp EOL{printf(=%d\n“,$2);}
|ID ASS exp EOL{sym[$1]=$3;
}
;
exp:factor{$$=$1;}
|exp添加因子{$$=$1+$3;}
|exp子因子{$$=$1-$3;}
;
因子:项{$$=$1;}
|因子多项{$$=$1*$3;}
|因子DIV项{$$=$1/$3;}
;
术语:数字{$$=$1;}
;
%%
int main(int argc,字符**argv)
{
yyparse();
}
yyerror(字符*s)
{
fprintf(标准,“错误:%s\n”,s);
} 
而.l代码是:

%{
    #include <stdio.h>
                  //char sym[1000];
    //int x=0;
    int sym[26];

%}


%token NUMBER ADD SUB MUL DIV ABS EOL ID ASS
%%
calclist : 
    | calclist exp EOL   { printf("= %d\n", $2); } 
    | ID ASS exp EOL     { sym[$1] = $3;

             }
;
exp:    factor           { $$=$1; }
    | exp ADD factor     { $$ = $1 + $3; }
    | exp SUB factor     { $$ = $1 - $3; }
;
factor :    term         { $$=$1; }
    | factor MUL term    { $$ = $1 * $3; }
    | factor DIV term    { $$ = $1 / $3; }
;
term :  NUMBER       { $$=$1; }

;

%%
int main(int argc, char **argv)
{
yyparse();
}
yyerror(char *s)
{
fprintf(stderr, "error: %s\n", s);
} 
%{
# include "P3.tab.h"
#include <stdio.h>
#include <stdlib.h>
extern int yylval;
//int m=0;
%}

%%
"+"     { return ADD; }
"-"     { return SUB; }
"*"  { return MUL; }
"/"     { return DIV; }
"=" { return ASS; }
[a-z]+  { yylval= *yytext  - 'a' ;  
     return ID ; }
[0-9]+  { yylval = atoi(yytext); return NUMBER; }
\n   { return EOL; }
[ \t]   { /* ignore whitespace */ }
.    { printf("Mystery character %c\n", *yytext); }
%%
int yywrap()
{
return 1;
}
%{
#包括“P3.表h”
#包括
#包括
外部国际组织;
//int m=0;
%}
%%
“+”{return ADD;}
“-”{return SUB;}
“*”{return MUL;}
“/”{return DIV;}
“=”{return ASS;}
[a-z]+{yylval=*yytext-'a';
返回ID;}
[0-9]+{yylval=atoi(yytext);返回编号;}
\n{return EOL;}
[\t]{/*忽略空白*/}
.    {printf(“神秘字符%c\n”,*yytext);}
%%
int yywrap()
{
返回1;
}

用这段代码,我只能创建一个=10,x=90的单字符变量。如何创建多个字符变量,并检查它是否已声明?

这与bison或flex关系不大。事实上,您的flex模式已经能够识别多个字符标识符(只要它们是纯字母),但该操作会忽略第一个字符之后的字符

您需要的是某种关联容器,如哈希表,可以将其用作符号表,而不是向量
sym


野牛手册包括一些小的计算器程序示例。例如,请参阅,其中包括一个作为简单线性关联列表实现的符号表。

如何检查变量是否已声明?我需要遍历链接列表吗?这没有多大帮助:(@jane:示例代码基本上做了你想做的事情。它工作正常,而且它的编码风格非常清晰(IMHO)。多年来它帮助了很多人。无论如何,答案是肯定的,示例代码就是这样做的(在lexer iirc中)。