在 Flex 中,您使用 [a-zA-Z][a-zA-Z0-9]* 来定义一个词。如何定义一个句子?以下是我的弹性代码:
%{
#include <stdio.h>
#include <string.h>
#include "y.tab.h"
%}
%%
[0-9]+ yylval=atoi(yytext);return NUMBER;
[a-zA-Z][a-zA-Z]* return WORD;
[a-zA-Z][a-zA-Z0-9]* return TERM;
%%
以下是野牛:
%{
#include <stdio.h>
#include <string.h>
void yyerror(const char *str)
{
fprintf(stderr,"error: %s\n",str);
}
int yywrap()
{
return 1;
}
main()
{
yyparse();
}
%}
%token NUMBER WORD TERM
commands: /* empty */
| commands command
;
command:
rule1
|
rule2
;
......
谢谢