0
所以我想避免全局變量,但我想使用Flex來標記輸入。我想知道是否有可能將一個值傳遞給yylex,以便我可以擺脫全局。將值傳遞給yylex
現在我有這個
%{
#include
#include
#include
#include
#include "lex.h"
%}
%option noyywrap
digit [0-9]
alpha [a-zA-Z]
alphanum {alpha}|{digit}|"_"
%%
[\t\n ] printf("WS:\n");
{alpha}{alphanum}* printf("symbol: %s\n",yytext);
{digit}+ printf("int: %s\n",yytext);
{digit}+"."{digit} printf("float: %s\n",yytext);
"\"".*"\"" printf("litral: %s\n",yytext);
"+" printf("op: %s\n",yytext);
"-" printf("op: %s\n",yytext);
"*" printf("op: %s\n",yytext);
"/" printf("op: %s\n",yytext);
"%" printf("op: %s\n",yytext);
"=" printf("op: %s\n",yytext);
"" printf("op: %s\n",yytext);
"==" printf("op: %s\n",yytext);
"!=" printf("op: %s\n",yytext);
"(" printf("op: %s\n",yytext);
")" printf("op: %s\n",yytext);
"," printf("op: %s\n",yytext);
"=" printf("op: %s\n",yytext);
%%
void LexInit() {
Tokens = malloc(sizeof(TokenStream));
Tokens->size=0;
}
void LexPush(const char* str) {
size_t size = strlen(str);
char* newstr = malloc(size*sizeof(char));
realloc(Tokens->tokens,++Tokens->size*sizeof(char*));
}
void Lex(const char* filepath) {
LexInit();
yyin = fopen(filepath,"r");
yylex();
}