GNU bison

来自开放百科 - 灰狐
(版本间的差异)
跳转到: 导航, 搜索
 
第5行: 第5行:
 
http://www.gnu.org/software/bison/
 
http://www.gnu.org/software/bison/
  
 +
==Examples==
 +
touch parse.y
 +
 +
%{
 +
#include<stdio.h>
 +
%}
 +
 +
%token NUM
 +
%left '+' '-'
 +
%left '*' '/'
 +
 +
%start line
 +
 +
%%
 +
 +
line: 
 +
        /* empty */
 +
      |line exp '\n' {printf("%d\n",$2);}
 +
      | error '\n';
 +
 +
exp:      exp '+' exp {$$ = $1 + $3;}
 +
        | exp '*' exp {$$ = $1 * $3;}
 +
        | exp '-' exp {$$ = $1 - $3;}
 +
        | exp '/' exp { if ($3 == 0)
 +
                                $$ = 0;
 +
                        else
 +
                                $$ = $1/$3;}
 +
        | NUM          {$$ = $1;};
 +
%%
 +
 +
yyerror()
 +
{
 +
        printf("Error detected in parsing\n");
 +
}
 +
 +
main()
 +
{
 +
        yyparse();
 +
}
 +
 +
bison parse.y
 
[[Category:Free compilers and interpreters]]
 
[[Category:Free compilers and interpreters]]

2007年2月13日 (二) 18:07的版本

GNU Bison is a free parser generator computer program written for the GNU project, and available for virtually all common operating systems. It is mostly compatible with Yacc, and offers several improvements over the earlier program. It is commonly used in conjunction with flex.

Bison converts a grammar description for a LALR context-free grammar into a C or C++ program to parse that grammar.

http://www.gnu.org/software/bison/

Examples

touch parse.y

%{
#include<stdio.h>
%}
%token NUM
%left '+' '-'
%left '*' '/'
%start line 
%%
line:   
       /* empty */ 
     |line exp '\n' {printf("%d\n",$2);}
     | error '\n';
exp:      exp '+' exp {$$ = $1 + $3;}
        | exp '*' exp {$$ = $1 * $3;}
        | exp '-' exp {$$ = $1 - $3;}
        | exp '/' exp { if ($3 == 0)
                                $$ = 0;
                        else
                                $$ = $1/$3;}
        | NUM          {$$ = $1;};
%%
yyerror()
{
        printf("Error detected in parsing\n");
}
main()
{
        yyparse();
}
bison parse.y
分享您的观点
个人工具
名字空间

变换
操作
导航
工具箱