Compilers(Dragon Book 2nd) - 4.9
来源:互联网 发布:windows远程桌面快捷键 编辑:程序博客网 时间:2024/05/17 07:18
--- # 4.9.1
- boolean.l:
%{
%}
ws [ \t]
%%
{ws} { /* skip blanks */ }
"true" { yylval = 1;return (TRUE);}
"false" { yylval = 0;return (FALSE);}
"or" { return (OR);}
"and" { return (AND);}
"not" { return (NOT);}
\n|. { return yytext[0]; }
%%
void yyerror(const char *str)
{
fprintf(stderr, "error:%s\n", str);
}
int yywarp(){
return 1;
}
int main(){
yyparse();
return 0;
}
- boolean.y
%{
#include <stdio.h>
%}
%token TRUE FALSE AND OR NOT
%%
lines : lines bexpr '\n' { printf("%d\n", $2); }
| lines '\n'
| /* empty */
| error '\n' { yyerror( "reenter previous line:");
yyerrok;};
;
bexpr : bexpr OR bterm { $$ = $1 || $3; }
| bterm
;
bterm : bterm AND bfactor { $$ = $1 && $3; }
| bfactor
;
bfactor : NOT bfactor { $$ = !($2); }
| '(' bexpr ')' { $$ = $2; }
| TRUE { $$ = 1; }
| FALSE
;
%%
#include "lex.yy.c"
- output: |
[20:08 luckyvan@luckyvan-VirtualBox 4.9.1] > flex boolean.l; bison boolean.y; gcc boolean*.c -ll -ly ; ./a.out
In file included from boolean.y:26:0:
boolean.l:15:6: warning: conflicting types for ‘yyerror’ [enabled by default]
boolean.y:11:7: note: previous implicit declaration of ‘yyerror’ was here
true
1
false
0
true and false
0
true or false
1
not true
0
not true and true
0
--- 4.9.2
- list.l:
%{
%}
ws [ \t]
letter [a-zA-Z]
%%
{ws} { /* skip blanks */ }
{letter} { yylval= yytext[0]; return ID; }
\n|. { return yytext[0]; }
%%
...
- list.y:
%{
#include <stdio.h>
%}
%token ID
%%
lines : lines list '\n' { printf("\n"); }
| lines '\n'
| /* empty */
| error '\n' { yyerror( "reenter previous line:");
yyerrok;};
;
list : list ',' { printf(","); } slot
| slot
;
slot : '(' { printf("("); } list ')' { printf(")"); }
| ID { printf("%c", yylval); }
;
%%
#include "lex.yy.c"
- output: |
[21:24 luckyvan@luckyvan-VirtualBox 4.9.2] > flex list.l; bison list.y; gcc list*.c -ll -ly ; ./a.out
In file included from list.y:22:0:
list.l:12:6: warning: conflicting types for ‘yyerror’ [enabled by default]
list.y:12:7: note: previous implicit declaration of ‘yyerror’ was here
a
a
b
b
(a,c)
(a,c)
((a,b),c,(d))
((a,b),c,(d))
`
error:syntax error
error:reenter previous line:
;
error:syntax error
error:reenter previous line:
--- 4.9.3
can not find a effective way to use yacc to parse a palindrome. always have a reduce/shift conflict by the following palin.y
- palin.y
%{
#include <stdio.h>
%}
%%
palin : 'a' { printf("S--> a\n"; }
| 'b' { printf("S--> b\n"; }
| 'a' palin 'a' { printf("S--> aSa\n"; }
| 'b' palin 'b' { printf("S--> bSb\n"; }
;
%%
- Compilers(Dragon Book 2nd) - 4.9
- Compilers(Dragon Book 2nd) - 5.3
- Compilers(Dragon Book 2nd) - 5.4
- Compilers (Dragon Book 2nd) - 6.3
- Compilers (Dragon Book 2nd) - 8.2
- 编译原理->nice time to spend(Compilers: Principles, Techniques, and Tools (2nd Edition))
- 《OpenGL.Shading.Language.2nd.Edition.(Orange.Book).2006 》下载
- Thinking in C++, 2nd Edition - Book Download Sites
- Dragon Book reading notes 1
- compilers
- The Windows 2000 Device Driver Book: A Guide for Programmers (2nd Edition)
- The Book of JavaScript, 2nd Edition: A Practical Guide to Interactive Web Pages
- The Windows 2000 Device Driver Book: A Guide for Programmers (2nd Edition)
- book 2
- 2nd Day
- 2nd Night
- Code Complete 2nd
- 2nd post
- 字符数组和字符指针
- JAVA的文件操作
- 哈夫曼编码
- IOS Socket使用大全 -将持续更新
- 计算机视觉整理备忘
- Compilers(Dragon Book 2nd) - 4.9
- RMI网络编程 重启server端程序,client端无法连接
- 有可能在onPause之前调用onSaveInstanceState
- uboot2012 LCD驱动流程及机制分析
- 第五周项目一(1):三角形类(带参数的构造函数)
- 关于Android的nodpi,xhdpi,hdpi,mdpi,ldpi
- 与IT&码农有关的电影和记录片
- java io
- QT学习之1---安装配置