我正在尝试编写一个能够使用以下输入的语法:
begin #this is a example
x = 56;
while x > 0 do
begin
point 15.6 78.96;
end;
end;
这是 lexer.l 文件:
%option noyywrap
%{
#include "parser.h"
#include <stdlib.h>
#include <stdio.h>
const char * const unrecognizedToken = "Unrecognized token";
%}
NewLine \n
WhiteSpaces [\r\t\f\v ]+
Semicolon ;
digit [0-9]
number1 {digit}+\.?([eE][-+]?{digit}+)?
number2 {digit}*\.{digit}+([eE][-+]?{digit}+)?
double_number {number1}|{number2}
BEGIN "begin"
END "end"
WHILE "while"
DO "do"
POINT "point"
%x POINT_DEFINITIONS
%%
{WhiteSpaces} {
printf("WhiteSpaces");
printf("\n");
}
{NewLine} {
printf("NewLine");
printf("\n");
}
{WHILE} {
printf("While");
printf("\n");
return TOKEN_WHILE;
}
{BEGIN} {
printf("TOKEN_BEGIN");
printf("\n");
return TOKEN_BEGIN;
}
{END} {
printf("TOKEN_END");
printf("\n");
return TOKEN_END;
}
{DO} {
printf("DO");
printf("\n");
return TOKEN_DO;
}
{POINT} {
printf("POINT_START");
printf("\n");
BEGIN POINT_DEFINITIONS;
return TOKEN_POINT;
}
<POINT_DEFINITIONS>{double_number} {
printf("POINT_DEFINITIONS %s", yytext);
printf("\n");
yylval.dval = atof(yytext);
return TOKEN_DOUBLE;
}
<POINT_DEFINITIONS>{WhiteSpaces} {
printf("WhiteSpaces");
printf("\n");
}
[a-zA-Z_][a-zA-Z0-9_]* {
printf("TOKEN_IDENTIFIER");
printf("\n");
yylval.name = strdup(yytext);
return TOKEN_IDENTIFIER;
}
[()=;] {
printf("yytext = %s", yytext);
printf("\n");
return *yytext;
}
[*/+\-<>] {
printf("TOKEN_OPERATOR");
printf("\n");
yylval.op = *yytext;
return TOKEN_OPERATOR;
}
[-]?[0-9]+ {
printf("TOKEN_VALUE");
printf("\n");
yylval.val = atoi(yytext);
return TOKEN_VALUE;
}
#.* {
printf("COMMENT");
printf("\n");
/*comment*/
}
. { printf("%s", unrecognizedToken); }
这是 parser.y 文件:
%error-verbose
%{
#define YYDEBUG 1
%}
%union {
int val;
double dval;
char op;
char* name;
}
%token TOKEN_BEGIN TOKEN_END TOKEN_WHILE TOKEN_DO TOKEN_POINT TOKEN_OPERATOR TOKEN_VALUE TOKEN_IDENTIFIER TOKEN_DOUBLE
%start program
%{
void yyerror(const char* const message);
%}
%%
program: statement';';
block: TOKEN_BEGIN statements TOKEN_END { printf("rule block\n"); };
statements:
statement';' statements { printf("rule statements\n"); }
|;
statement:
| assignment
| command
| whileStmt
| block;
assignment: TOKEN_IDENTIFIER '=' TOKEN_VALUE {
printf("rule Assignment\n");
} ;
whileStmt: TOKEN_WHILE condition TOKEN_DO block {printf("rule While\n");};
condition: TOKEN_IDENTIFIER { printf("rule token_identifier\n"); }
| TOKEN_VALUE { printf("rule token_value\n"); }
| condition TOKEN_OPERATOR condition { printf("rule condition TOKEN_OPERATOR condition\n"); };
command: TOKEN_POINT TOKEN_DOUBLE TOKEN_DOUBLE { printf("rule Command\n"); };
%%
#include <stdlib.h>
void yyerror(const char* const message)
{
printf("Parse error:%s\n", message);
exit(1);
}
int main()
{
yyparse();
}
但我收到以下错误消息:
Parse error:syntax error, unexpected $end, expecting ';'
编译如下:
flex -o lexer.c lexer.l
bison -v -d -o parser.c parser.y
gcc parser.c lexer.c -o parser -g -DYYDEBUG=1
要运行解析器:
./parser < example
你能帮我找出问题所在吗?为什么语法不能接受上面的例子作为输入?