[ 2005/09/19 08:39 | by turbozv ]
|
1)支持文法格式有: ''?' 0个或者1个, +' 1个或多个, '*' 0个或者多个, '|' 并列选择结构. '' 空串, 'A' 单个字符, \"string\" 字符串
2)自动展开文法,自动合并相同文法项
3)生成单一一个ANSI C的标准c文件,任何编译器均可以编译通过
TODO:
1)支持ASCII的字符,比如 '\n', \"hello\250\"
2)支持字符的范围格式,比如'0'-'9'
3)需要把空串文法放在并列选择的最后,比如 A : | B ; 应该调整为 A : B | ;
源程序: http://down.turbozv.com/PIG/PIG.dpr (Delphi 7.0编译通过)
还是举个例子吧,输入exp.txt
===============exp.txt=============
exp : [ '+' | '-' ] ? term [ [ '+' | '-' ] term ] * ;
term : factor [ [ '*' | '/' ] factor ] * ;
factor : uint | '(' exp ')' ;
uint : number + ;
number : '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ;
=================================
输出exp.c
/* Generated by PIG version 1.0 (turbozv@uestc.edu.cn)
$Id: PIG.dpr,v 1.4 2005/09/19 17:15:51 ZV Exp $
DateTime: 2005-9-20 15:23:21
===================== Orignal Rules ===================
1) exp : [ '+' | '-' ] ? term [ [ '+' | '-' ] term ] * ;
2) term : factor [ [ '*' | '/' ] factor ] * ;
3) factor : uint | '(' exp ')' ;
4) uint : number + ;
5) number : '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ;
===================Processed to Rules==================
1) exp : exp_1 ? term exp_2 * ;
2) term : factor term_1 * ;
3) factor : uint | '(' exp ')' ;
4) uint : number + ;
5) number : '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ;
6) exp_1 : '+' | '-' ;
7) exp_2 : exp_1 term ;
8) term_1 : term_2 factor ;
9) term_2 : '*' | '/' ;
*/
#include <stdio.h>
typedef void Parser;
static int is_exp(Parser * pVar, char *buf);
static int is_term(Parser * pVar, char *buf);
static int is_factor(Parser * pVar, char *buf);
static int is_uint(Parser * pVar, char *buf);
static int is_number(Parser * pVar, char *buf);
static int is_exp_1(Parser * pVar, char *buf);
static int is_exp_2(Parser * pVar, char *buf);
static int is_term_1(Parser * pVar, char *buf);
static int is_term_2(Parser * pVar, char *buf);
/* 1) exp : exp_1 ? term exp_2 * ; */
static int is_exp(Parser * pVar, char *buf)
{
int ret, sum = 0;
// exp_1 ?
if (-1 != (ret = is_exp_1(pVar, buf))) {
sum += ret;
buf += ret;
}
// term
if (-1 == (ret = is_term(pVar, buf)))
return -1;
sum += ret;
buf += ret;
// exp_2 *
while (-1 != (ret = is_exp_2(pVar, buf))) {
sum += ret;
buf += ret;
}
return sum;
}
/* 2) term : factor term_1 * ; */
static int is_term(Parser * pVar, char *buf)
{
int ret, sum = 0;
// factor
if (-1 == (ret = is_factor(pVar, buf)))
return -1;
sum += ret;
buf += ret;
// term_1 *
while (-1 != (ret = is_term_1(pVar, buf))) {
sum += ret;
buf += ret;
}
return sum;
}
/* 3) factor : uint | '(' exp ')' ; */
static int is_factor(Parser * pVar, char *buf)
{
int ret, sum = 0;
// uint
if (-1 != (ret = is_uint(pVar, buf))) {
sum += ret;
buf += ret;
return sum;
}
// '('
if ('(' != *buf)
return -1;
sum++;
buf++;
// exp
if (-1 == (ret = is_exp(pVar, buf)))
return -1;
sum += ret;
buf += ret;
// ')'
if (')' != *buf)
return -1;
sum++;
buf++;
return sum;
}
/* 4) uint : number + ; */
static int is_uint(Parser * pVar, char *buf)
{
int ret, sum = 0;
// number +
if (-1 == (ret = is_number(pVar, buf)))
return -1;
do {
sum += ret;
buf += ret;
} while (-1 != (ret = is_number(pVar, buf)));
return sum;
}
/* 5) number : '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ; */
static int is_number(Parser * pVar, char *buf)
{
int sum = 0;
// '0'
if ('0' == *buf) {
sum++;
buf++;
return sum;
}
// '1'
if ('1' == *buf) {
sum++;
buf++;
return sum;
}
// '2'
if ('2' == *buf) {
sum++;
buf++;
return sum;
}
// '3'
if ('3' == *buf) {
sum++;
buf++;
return sum;
}
// '4'
if ('4' == *buf) {
sum++;
buf++;
return sum;
}
// '5'
if ('5' == *buf) {
sum++;
buf++;
return sum;
}
// '6'
if ('6' == *buf) {
sum++;
buf++;
return sum;
}
// '7'
if ('7' == *buf) {
sum++;
buf++;
return sum;
}
// '8'
if ('8' == *buf) {
sum++;
buf++;
return sum;
}
// '9'
if ('9' != *buf)
return -1;
sum++;
buf++;
return sum;
}
/* 6) exp_1 : '+' | '-' ; */
static int is_exp_1(Parser * pVar, char *buf)
{
int sum = 0;
// '+'
if ('+' == *buf) {
sum++;
buf++;
return sum;
}
// '-'
if ('-' != *buf)
return -1;
sum++;
buf++;
return sum;
}
/* 7) exp_2 : exp_1 term ; */
static int is_exp_2(Parser * pVar, char *buf)
{
int ret, sum = 0;
// exp_1
if (-1 == (ret = is_exp_1(pVar, buf)))
return -1;
sum += ret;
buf += ret;
// term
if (-1 == (ret = is_term(pVar, buf)))
return -1;
sum += ret;
buf += ret;
return sum;
}
/* 8) term_1 : term_2 factor ; */
static int is_term_1(Parser * pVar, char *buf)
{
int ret, sum = 0;
// term_2
if (-1 == (ret = is_term_2(pVar, buf)))
return -1;
sum += ret;
buf += ret;
// factor
if (-1 == (ret = is_factor(pVar, buf)))
return -1;
sum += ret;
buf += ret;
return sum;
}
/* 9) term_2 : '*' | '/' ; */
static int is_term_2(Parser * pVar, char *buf)
{
int sum = 0;
// '*'
if ('*' == *buf) {
sum++;
buf++;
return sum;
}
// '/'
if ('/' != *buf)
return -1;
sum++;
buf++;
return sum;
}
void main()
{
}
2)自动展开文法,自动合并相同文法项
3)生成单一一个ANSI C的标准c文件,任何编译器均可以编译通过
TODO:
1)支持ASCII的字符,比如 '\n', \"hello\250\"
2)支持字符的范围格式,比如'0'-'9'
3)需要把空串文法放在并列选择的最后,比如 A : | B ; 应该调整为 A : B | ;
源程序: http://down.turbozv.com/PIG/PIG.dpr (Delphi 7.0编译通过)
还是举个例子吧,输入exp.txt
===============exp.txt=============
exp : [ '+' | '-' ] ? term [ [ '+' | '-' ] term ] * ;
term : factor [ [ '*' | '/' ] factor ] * ;
factor : uint | '(' exp ')' ;
uint : number + ;
number : '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ;
=================================
输出exp.c
/* Generated by PIG version 1.0 (turbozv@uestc.edu.cn)
$Id: PIG.dpr,v 1.4 2005/09/19 17:15:51 ZV Exp $
DateTime: 2005-9-20 15:23:21
===================== Orignal Rules ===================
1) exp : [ '+' | '-' ] ? term [ [ '+' | '-' ] term ] * ;
2) term : factor [ [ '*' | '/' ] factor ] * ;
3) factor : uint | '(' exp ')' ;
4) uint : number + ;
5) number : '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ;
===================Processed to Rules==================
1) exp : exp_1 ? term exp_2 * ;
2) term : factor term_1 * ;
3) factor : uint | '(' exp ')' ;
4) uint : number + ;
5) number : '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ;
6) exp_1 : '+' | '-' ;
7) exp_2 : exp_1 term ;
8) term_1 : term_2 factor ;
9) term_2 : '*' | '/' ;
*/
#include <stdio.h>
typedef void Parser;
static int is_exp(Parser * pVar, char *buf);
static int is_term(Parser * pVar, char *buf);
static int is_factor(Parser * pVar, char *buf);
static int is_uint(Parser * pVar, char *buf);
static int is_number(Parser * pVar, char *buf);
static int is_exp_1(Parser * pVar, char *buf);
static int is_exp_2(Parser * pVar, char *buf);
static int is_term_1(Parser * pVar, char *buf);
static int is_term_2(Parser * pVar, char *buf);
/* 1) exp : exp_1 ? term exp_2 * ; */
static int is_exp(Parser * pVar, char *buf)
{
int ret, sum = 0;
// exp_1 ?
if (-1 != (ret = is_exp_1(pVar, buf))) {
sum += ret;
buf += ret;
}
// term
if (-1 == (ret = is_term(pVar, buf)))
return -1;
sum += ret;
buf += ret;
// exp_2 *
while (-1 != (ret = is_exp_2(pVar, buf))) {
sum += ret;
buf += ret;
}
return sum;
}
/* 2) term : factor term_1 * ; */
static int is_term(Parser * pVar, char *buf)
{
int ret, sum = 0;
// factor
if (-1 == (ret = is_factor(pVar, buf)))
return -1;
sum += ret;
buf += ret;
// term_1 *
while (-1 != (ret = is_term_1(pVar, buf))) {
sum += ret;
buf += ret;
}
return sum;
}
/* 3) factor : uint | '(' exp ')' ; */
static int is_factor(Parser * pVar, char *buf)
{
int ret, sum = 0;
// uint
if (-1 != (ret = is_uint(pVar, buf))) {
sum += ret;
buf += ret;
return sum;
}
// '('
if ('(' != *buf)
return -1;
sum++;
buf++;
// exp
if (-1 == (ret = is_exp(pVar, buf)))
return -1;
sum += ret;
buf += ret;
// ')'
if (')' != *buf)
return -1;
sum++;
buf++;
return sum;
}
/* 4) uint : number + ; */
static int is_uint(Parser * pVar, char *buf)
{
int ret, sum = 0;
// number +
if (-1 == (ret = is_number(pVar, buf)))
return -1;
do {
sum += ret;
buf += ret;
} while (-1 != (ret = is_number(pVar, buf)));
return sum;
}
/* 5) number : '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ; */
static int is_number(Parser * pVar, char *buf)
{
int sum = 0;
// '0'
if ('0' == *buf) {
sum++;
buf++;
return sum;
}
// '1'
if ('1' == *buf) {
sum++;
buf++;
return sum;
}
// '2'
if ('2' == *buf) {
sum++;
buf++;
return sum;
}
// '3'
if ('3' == *buf) {
sum++;
buf++;
return sum;
}
// '4'
if ('4' == *buf) {
sum++;
buf++;
return sum;
}
// '5'
if ('5' == *buf) {
sum++;
buf++;
return sum;
}
// '6'
if ('6' == *buf) {
sum++;
buf++;
return sum;
}
// '7'
if ('7' == *buf) {
sum++;
buf++;
return sum;
}
// '8'
if ('8' == *buf) {
sum++;
buf++;
return sum;
}
// '9'
if ('9' != *buf)
return -1;
sum++;
buf++;
return sum;
}
/* 6) exp_1 : '+' | '-' ; */
static int is_exp_1(Parser * pVar, char *buf)
{
int sum = 0;
// '+'
if ('+' == *buf) {
sum++;
buf++;
return sum;
}
// '-'
if ('-' != *buf)
return -1;
sum++;
buf++;
return sum;
}
/* 7) exp_2 : exp_1 term ; */
static int is_exp_2(Parser * pVar, char *buf)
{
int ret, sum = 0;
// exp_1
if (-1 == (ret = is_exp_1(pVar, buf)))
return -1;
sum += ret;
buf += ret;
// term
if (-1 == (ret = is_term(pVar, buf)))
return -1;
sum += ret;
buf += ret;
return sum;
}
/* 8) term_1 : term_2 factor ; */
static int is_term_1(Parser * pVar, char *buf)
{
int ret, sum = 0;
// term_2
if (-1 == (ret = is_term_2(pVar, buf)))
return -1;
sum += ret;
buf += ret;
// factor
if (-1 == (ret = is_factor(pVar, buf)))
return -1;
sum += ret;
buf += ret;
return sum;
}
/* 9) term_2 : '*' | '/' ; */
static int is_term_2(Parser * pVar, char *buf)
{
int sum = 0;
// '*'
if ('*' == *buf) {
sum++;
buf++;
return sum;
}
// '/'
if ('/' != *buf)
return -1;
sum++;
buf++;
return sum;
}
void main()
{
}