开发者

How can I separate Lexical error from Syntax error in flex & bison?

I want to make simple calculator using bison, and I have one problem.

Here is my code:

flex

[0-9]+[A-Za-z][A-Za-z0-9]* { printerr();  return LEXERR;开发者_StackOverflow社区 }
[0-9]*"."[0-9]+[A-Za-z][A-Za-z0-9]* { printerr(); return LEXERR; }
. { printerr(); return LEXERR; }

bison

stmt: 
    | err NEWLINE
    | error NEWLINE { yyerrok; }
    | stmt expr NEWLINE  { printf("%d\n", $2); }
    | stmt real_expr NEWLINE { printf("%f\n", $2); }
    ;

err: LEXERR
    | err LEXERR
    ;

Even if Lexical and Syntax error occur at the same time, I want only Lexical error to be output. But now, when ERROR occurs at the same time, both occur.

For example:

> %%
lexical error(1:1): %
lexical error(1:2): %
> %%
lexical error(2:1): %
syntax error(2)
lexical error(2:2): %
> 10 * %
lexical error(3:6): %
syntax error(3)
> 10 * *
syntax error(4)

I want to print like this:

> %%
lexical error(1:1): %
lexical error(1:2): %
> %%
lexical error(2:1): %
lexical error(2:2): %
> 10 * %
lexical error(3:6): %
> 10 * *
syntax error(4)

I tried use yyclearin, but it didn't work.

How can I solve this problem?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜