lex&yacc系列(4)--- yacc语法分析探索及calculator实例

                                                               音乐是人生的艺术


1 移进/规约

When yacc processes a parser, it creates a set of states each of which reflects a possible position in one or more partially parsed rules.

As the parser reads tokens, each time it reads a token that doesn’t complete a rule it pushes the token on an internal stack and switches to a new state reflecting the token it just read. This action is called a shift.

When it has found all the symbols that constitute the right-hand side of a rule, it pops the right-hand side symbols off the stack, pushes the left-hand side symbol onto the stack, and switches to a new state  reflecting the new symbol on the stack. This action is called a reduction, since it usually reduces the number of items on the stack. (Not always, since it is possible to have rules with empty right-hand sides.)

Whenever yacc reduces a rule, it executes user code associated with the rule. This is how you actually do something with the material that the parser parses.

Often the action code builds a parse tree corresponding to the input, so that later code can process a whole statement or even a whole program at a time。----- crow_bar语言就是这个样子的。

 


lex & yacc》书上举的一个有 移进规约 冲突的例子:

 

Shift指的是将一个token加入堆栈,reduce指的是将堆栈中现存的进行一次替换:

 

Parser是可以提前向后看一个token的,这时它会提前看到3后面的*,从而有下面的两种选择:

 

书中给出的解决方案有两个,一个是利用yacc的语法规则去强行指定运算的优先级(书中不推荐使用这种),另一种是用下面类似分层的方法:

为什么用上面这种方法改写了之后,就可以让乘法的优先级更高而消掉不确定性,需要进一步探索yacc


实验:用下面的例子没有移进-规约冲突,乘法优先级别高于加法的例子做实验:

编译运行后,输入2+3*4 计算过程:

从运行结果来看,parser接受到3之后,将3规约为term之后,就不继续将其规约为expression了,为什么呢?这是yacc的一个语法规则?我还没发现?

有帖子说 yacc先规约小的规则:https://blog.csdn.net/nosources/article/details/38943265 , term向对于expression是更小更底层的规则。

另,书《lex and yacc》的第8章中有对移进规约冲突的进一步解释,已经看了一部分,继续看可能有收获。
 

实验用.y文件:

%{
#include <stdio.h>
#include <stdlib.h>
#define YYDEBUG 1
%}
%union {
    int          int_value;
    double       double_value;
}
%token <double_value>      DOUBLE_LITERAL
%token ADD SUB MUL DIV CR
%type <double_value> expression term primary
%% 

expression
    :expression ADD term
    {
        $$ = $1 + $3;
	printf("expression: expression ADD term %lf + %lf = %lf\n", $1, $3, $$);
    }
    | expression SUB term
    {
        $$ = $1 - $3;
    }
    | term
    {
        printf("expression: term  %lf\n", $$);
    }
    ;

term
    :term MUL primary
    {
        $$ = $1 * $3;
        printf("term: term MUL primary %lf * %lf = %lf\n", $1, $3, $$);
    }
    | primary{printf("term: primary %lf\n",$$);}
    ;

primary
    :
    DOUBLE_LITERAL{printf("primary: DOUBLE_LITERAL %lf\n",$$);} 
       
%%
int
yyerror(char const *str)
{
    extern char *yytext;
    //fprintf(stderr, "parser error near %s\n", yytext);
    return 0;
}

int main(void)
{
    extern int yyparse(void);
    extern FILE *yyin;

    yyin = stdin;
    if (yyparse()) {
        fprintf(stderr, "Error ! Error ! Error !\n");
        exit(1);
    }
}

 

 

实验用.l文件:

%{
#include <stdio.h>
#include "calcul.tab.h"

int
yywrap(void)
{
    return 1;
}
%}
%%
"+"             {printf("flex found + \n");return ADD;}
"-"             return SUB;
"*"             {printf("flex found * \n");return MUL;}
"/"             return DIV;
"\n"            return CR;
([1-9][0-9]*)|0|([0-9]+\.[0-9]*) {
    double temp;
    sscanf(yytext, "%lf", &temp);
    /* yylval是yacc的变量,对应.y文件中定义的union类型,在yacc生成的calcul.tab.h文件中有     extern YYSTYPE yylval;这个变量用于flex与 yacc之间传递数据,yyval传递到yacc后对应我们使用的$1,$2*/
    yylval.double_value = temp; 
    return DOUBLE_LITERAL;
}
[ \t] ;
. {
    fprintf(stderr, "lexical error.\n");
    exit(1);
}
%%

Ref:

《lex and yacc--second edition》 – 作者:John R. Levine

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
作者: 胡彦 本框架是一个lex/yacc完整的示例,用于学习lex/yacc程序基本的搭建方法,在linux/cygwin下敲入make就可以编译和执行。 本例子虽小却演示了lex/yacc程序最常见和重要的特征: * lex/yacc文件格式、程序结构。 * 如何在lex/yacc中使用C++和STL库,用extern "C"声明那些lex/yacc生成的、要链接的C函数,如yylex(), yywrap(), yyerror()。 * 重定义YYSTYPE/yylval为复杂类型。 * 用%token方式声明yacc记号。 * 用%type方式声明非终结符的类型。 * lex里正则表达式的定义、识别方式。 * lex里用yylval向yacc返回属性值。 * 在yacc嵌入的C代码动作里,对记号属性($1, $2等)、和非终结符属性($$)的正确引用方法。 * 对yyin/yyout重赋值,以改变yacc默认的输入/输出目标。 * 如何开始解析(yyparse函数),结束或继续解析(yywrap函数)。 本例子功能是,对当前目录下的file.txt文件,解析出其中的标识符、数字、其它符号,显示在屏幕上。linux调试环境是Ubuntu 10.04。 总之,大部分框架已经搭好了,你只要稍加扩展就可以成为一个计算器之类的程序,用于《编译原理》的课程设计。 文件列表: lex.l: lex程序文件。 yacc.y: yacc程序文件。 main.hpp: 共同使用的头文件。 Makefile: makefile文件。 file.txt: 给程序解析的文本文件。 使用方法: 1-把lex_yacc_example.rar解压到linux/cygwin下。 2-命令行进入lex_yacc_example目录。 3-敲入make,这时会自动执行以下操作: (1) 自动调用flex编译.l文件,生成lex.yy.c文件。 (2) 自动调用bison编译.y文件,生成yacc.tab.c和yacc.tab.h文件。 (3) 自动调用g++编译、链接出可执行文件main。 (4) 自动执行main,得到如下结果:。 bison -d yacc.y g++ -c lex.yy.c g++ -c yacc.tab.c g++ lex.yy.o yacc.tab.o -o main id: abc id: defghi int: 123 int: 45678 op: ! op: @ op: # op: $ AllId: abc defghi 参考资料:《Lex和Yacc从入门到精通(6)-解析C-C++包含文件》, http://blog.csdn.net/pandaxcl/article/details/1321552 其它文章和代码请留意我的blog: http://blog.csdn.net/huyansoft 2013-4-27

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

First Snowflakes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值