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.csdn.net/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