yacc 和 lex的上手(2)

前面做了一个计算器的经典例子,可是对lex和yacc还是不够了解。
Google一下发现了这个网站:Lex and YACC primer/HOWTO
[url]http://tldp.org/HOWTO/Lex-YACC-HOWTO.html#toc4[/url]

把sample都看一遍的话,或许会有些帮助学习。

首先从lex开始:
[b]Sample1 [/b]一个简单的例子,可以识别输入是否有stop/start 然后输出相应文字。

建立一个example1.l文件:

%{
#include <stdio.h>
%}

%%
stop printf("Stop command received\n");
start printf("Start command received\n");
%%


然后编译:

flex example1.l
cc lex.yy.c -o example1 -ll


结果失败了。。。orz

仔细看一下,原来是编译参数的问题:
NOTE: If you are using flex, instead of lex, you may have to change '-ll' to '-lfl' in the compilation scripts. RedHat 6.x and SuSE need this, even when you invoke 'flex' as 'lex'!

再来编译:

cc lex.yy.c -o example1 -lfl


这次编译成功了,
运行example1
$./example1
stop
Stop command received
start
Start command received
^D // Terminate with a EOF (^D).

[b]Sample2 [/b]可识别输入是word还是number

建立一个example2.l文件:

%{
#include <stdio.h>
%}

%%
[0123456789]+ printf("NUMBER\n");
[a-zA-Z][a-zA-Z0-9]* printf("WORD\n");
%%


然后编译: 这次参数不能设错了。

flex example2.l
cc lex.yy.c -o example2 -lfl


编译成功!
运行example2
$ ./example2
1234
NUMBER

hello
WORD

[b]最后一个例子[/b]比较复杂一点,解析一个类c的文件,把符合翻译成另外一种形式。

解析的目标test.txt:

logging {
category lame-servers { null; };
category cname { null; };
};

zone "." {
type hint;
file "/etc/bind/db.root";
};


规则:用前面的单词代替后面的字符串
WORDs, like 'zone' and 'type'
FILENAMEs, like '/etc/bind/db.root'
QUOTEs, like those surrounding the filename
OBRACEs, {
EBRACEs, }
SEMICOLONs, ;

建一个example3.l文件:

%{
#include <stdio.h>
%}

%%
[a-zA-Z][a-zA-Z0-9]* printf("WORD ");
[a-zA-Z0-9\/.-]+ printf("FILENAME ");
\" printf("QUOTE ");
\{ printf("OBRACE ");
\} printf("EBRACE ");
; printf("SEMICOLON ");
\n printf("\n");
[ \t]+ /* ignore whitespace */;
%%


然后编译:

flex example3.l
cc lex.yy.c -o example3 -lfl


编译成功

运行
$ ./example3 < test.txt
WORD OBRACE
WORD FILENAME OBRACE WORD SEMICOLON EBRACE SEMICOLON
WORD WORD OBRACE WORD SEMICOLON EBRACE SEMICOLON
EBRACE SEMICOLON

WORD QUOTE FILENAME QUOTE OBRACE
WORD WORD SEMICOLON
WORD QUOTE FILENAME QUOTE SEMICOLON
EBRACE SEMICOLON

OK 预期结果出现!

到这里为止只是使用了lex ,后面继续学习yacc 以及 lex和yac的c配套使用。

明天继续,つづく(To be continued)
中文名: DNS and BIND on IPv6 (第1版) 原名: DNS and BIND on IPv6, 1st edition 作者: Cricket Liu 资源格式: PDF 版本: 英文文字版 出版社: O'Reilly书号: 978-1449305192发行时间: 2011年05月27日 地区: 美国 语言: 英文 简介: 内容介绍: If you're preparing to roll out IPv6 on your network, this concise book provides the essentials you need to support this protocol with DNS. You'll learn how DNS was extended to accommodate IPv6 addresses, and how you can configure a BIND name server to run on the network. This book also features methods for troubleshooting problems with IPv6 forward- and reverse-mapping, techniques for helping islands of IPv6 clients communicate with IPv4 resources, and many other topics. 作者介绍: Cricket Liu Cricket Liu graduated from the University of California, Berkeley, that great bastion of free speech, unencumbered Unix, and cheap pizza. He joined Hewlett-Packard after graduation and worked for HP for nine years. Cricket began managing the hp.com zone after the Loma Prieta earthquake forcibly transferred the zone's management from HP Labs to HP's Corporate Offices (by cracking a sprinkler main and flooding a Labs computer room). Cricket was hostmaster@hp.com for over three years, and then joined HP's Professional Services Organization to co-found HP's Internet Consulting Program. Cricket left HP in 1997 to form Acme Byte & Wire, a DNS consulting and training company, with his friend Matt Larson. Network Solutions acquired Acme in June 2000, and later the same day merged with VeriSign. Cricket worked for a year as Director of DNS Product Management for VeriSign Global Registry Services. Cricket joined Infoblox, a company that develops DNS and DHCP appliances, in March, 2003. He is currently their Vice President of Architecture. Cricket, his wife, Paige, their son, Walt, and daughter, Greta, live in California with their two Siberian Huskies, Annie and Dakota. 目录: Chapter 1 DNS and IPv6 Chapter 2 BIND on IPv6 Chapter 3 Resolver Configuration Chapter 4 DNS64 Chapter 5 Troubleshooting
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值