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";    }
        ;
%%



原创粉丝点击