如何提供输入

既然我们已经有了一个可以识别标记序列的简单语法,将需要寻求一种将这些标记提供给语法分析器的方法。lex 这种工具可以接受输入,将它转换成标记,然后将这些标记传递给 yacc。下面,我们将描述 lex 要将其转换成标记的表达式:

eval.l

%{
/* Again, this is C code that is inserted into the beginning of the output */
#include 

#include "y.tab.h"  /* Include the token definitions generated by yacc */
%}

/* Prevent the need for linking with -lfl */
%option noyywrap

/* This next section is a set of regular expressions that describe input
   tokens that are passed back to yacc.  The tokens are defined in y.tab.h,
   which is generated by yacc.
 */
%%
.*		/* ignore comments */
-[0-9]+|[0-9]+	{ yylval.number=atoi(yytext); return NUMBER; }
[ /t/n]		/* ignore whitespace */
&&		{ return AND; }
/|/|		{ return OR; }
==		{ return EQUALS; }
.		return yytext[0];
%%

现在,在当前目录中已经有了分析源码,我们需要一个 Makefile 来构建它们:

Makefile

all: eval 

y.tab.c: eval.y
	yacc -d ___FCKpd___1lt;

lex.yy.c: eval.l
	lex ___FCKpd___1lt;

eval: y.tab.o lex.yy.o
	$(CC) -o $@ $^

缺省情况下,yacc 输出到 y.tab.c,lex 输出到 lex.yy.c,因此我们使用那些名称作为源文件。Makefile 包含了根据分析描述文件构建源码的规则。一切就绪之后,可以输入 "make" 来构建语法分析器。然后我们可以运行该语法分析器并输入脚本以检查逻辑。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值