Compiler Principle(1)Lex and Yacc

本文介绍了Lex与Yacc的基本概念及其使用方法。Lex用于构建词法分析器,而Yacc则用于创建语法分析器。文章通过具体示例展示了如何用Lex统计单词数量,并用Yacc实现简单的语法解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Compiler Principle(1)Lex and Yacc

1. Some Concept
Abstract Syntax Tree AST
Lex - Lexical Analyzer
Yacc - Yet Another Compiler Compiler

2. Lex
>lex --version
flex 2.5.35 Apple(flex-31)

Declare the wordCount
%{       

int wordCount = 0;

%}

chars [A-za-z\_\'\.\"]

numbers ([0-9])+

delim [" "\n\t]

whitespace {delim}+

words {chars}+

%%

Lex Rules
{words} {        

 wordCount++;

 /*        increase the word count by one*/ }

{whitespace}

 { /* do nothing*/ }

{numbers}

{ /* one may want to add some processing here*/ }

%%

The last part is usually, the main Function
void main()

{  

yylex();

/* start the analysis*/  

printf(" No. of words:%d\n", wordCount);

}

 

int yywrap(){  

 return 1;

}

3. Yacc
>yacc -V
bison (GNU Bison) 2.3 Written by Robert Corbett and Richard Stallman.

>yacc filename.y

Declaration
%{
 #typedef char* string; /* to specify token types as char* */
#define YYSTYPE string /*a Yacc variable which has the value of returned token */
%}
%token NAME EQ AGE
%%

Grammar Rules
file: record file
     | record ;
record: NAME EQ AGE {
     printf(“%s is now %s years old!, $1, $3);
};
%%

C Function
void main(){
     yyparse();
}
int yyerror(char* msg){
     printf(“Error: %s encountered \n”, msg);
}

>yacc -d filename.y

4. Lex and Yacc
Take the example in 
http://www.ualberta.ca/dept/chemeng/AIX-43/share/man/info/C/a_doc_lib/aixprggd/genprogc/ie_prog_4lex_yacc.htm 

I need more time to understand more.

References:
java AST
http://rwl6813021.iteye.com/blog/174666
http://lym6520.iteye.com/blog/747840

go AST
http://golang.org/src/pkg/go/ast/example_test.go
https://github.com/Joinhack/peony/blob/master/mole/gowalker.go
https://github.com/Joinhack/peony/blob/master/mole/comment_expr.y
https://github.com/Joinhack/peony/blob/master/mole/gowalker.go

Yacc
http://www.ibm.com/developerworks/cn/linux/sdk/lex/
http://blog.youkuaiyun.com/yuucyf/article/details/7108590
http://www.360doc.com/content/12/0806/19/532901_228697197.shtml

Yacc and Lex
http://epaperpress.com/lexandyacc/
http://www.ualberta.ca/dept/chemeng/AIX-43/share/man/info/C/a_doc_lib/aixprggd/genprogc/ie_prog_4lex_yacc.htm 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值