Skip to content
This repository has been archived by the owner on Jul 26, 2018. It is now read-only.

Random updates #6

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
*.[oa]
greg
greg.exe
samples/*.c
rpeg
rpeg.exe
samples/*.leg.c
8 changes: 8 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Ian Piumarta (Original author of peg/leg)
_why (Original author of greg, derived from leg)
Amos Wenger (Improved error reporting)
Onne Gortner
Amos Wenger
Ali Rantakari
Steve White (leg samples for greg derived from peg samples)
Giulio Paci (Original author of rpeg, derived from peg and greg)
50 changes: 26 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ OFLAGS = -O3 -DNDEBUG

OBJS = tree.o compile.o

all : greg
all : greg rpeg

rpeg : rpeg.o $(OBJS)
$(CC) $(CFLAGS) -o $@-new rpeg.o $(OBJS)
mv $@-new $@

greg : greg.o $(OBJS)
$(CC) $(CFLAGS) -o $@-new greg.o $(OBJS)
Expand All @@ -14,43 +18,41 @@ ROOT =
PREFIX = /usr
BINDIR = $(ROOT)$(PREFIX)/bin

install : $(BINDIR)/greg
install : $(BINDIR)/rpeg $(BINDIR)/greg

$(BINDIR)/% : %
cp -p $< $@
strip $@

uninstall : .FORCE
rm -f $(BINDIR)/rpeg
rm -f $(BINDIR)/greg

rpeg.o : rpeg.c rpeg.peg-c

%.peg-c : %.peg
./rpeg -o $@ $<

greg.o : greg.c

grammar : .FORCE
./greg -o greg.c greg.g
greg.c : greg.g
./greg -o $@ $<

check : rpeg .FORCE
./rpeg < rpeg.peg > rpeg.out
diff rpeg.peg-c rpeg.out
rm rpeg.out

test examples : .FORCE
$(SHELL) -ec '(cd examples; $(MAKE))'

clean : .FORCE
rm -rf *~ *.o *.greg.[cd] greg samples/*.o samples/calc samples/*.dSYM testing1.c testing2.c *.dSYM selftest/
rm -rf *~ *.o *.greg.[cd] *.rpeg.[cd]
$(SHELL) -ec '(cd examples; $(MAKE) $@)'

spotless : clean .FORCE
rm -f rpeg
rm -f greg

samples/calc.c: samples/calc.leg greg
./greg -o $@ $<

samples/calc: samples/calc.c
$(CC) $(CFLAGS) -o $@ $<

test: samples/calc greg-testing
echo '21 * 2 + 0' | ./samples/calc | grep 42

run: greg.g greg
mkdir -p selftest
./greg -o testing1.c greg.g
$(CC) $(CFLAGS) -o selftest/testing1 testing1.c $(OBJS)
$(TOOL) ./selftest/testing1 -o testing2.c greg.g
$(CC) $(CFLAGS) -o selftest/testing2 testing2.c $(OBJS)
$(TOOL) ./selftest/testing2 -o selftest/calc.c ./samples/calc.leg
$(CC) $(CFLAGS) -o selftest/calc selftest/calc.c
$(TOOL) echo '21 * 2 + 0' | ./selftest/calc | grep 42
$(SHELL) -ec '(cd examples; $(MAKE) $@)'

.FORCE :
12 changes: 8 additions & 4 deletions README
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
greg is a re-entrant peg/leg, with some bug fixes.

The original peg/leg 0.1.9 was released with re-entrant capabilities.

And now most relevant features of greg (e.g., error handling) are
being integrated into the original peg/leg software
<http://piumarta.com/software/peg/>

the most comprehensive example of greg usage is in
nagaqueen, an ooc grammar, used in rock, an ooc
compiler written in ooc.
The most comprehensive example of greg usage is in nagaqueen, an ooc
grammar, used in rock, an ooc compiler written in ooc.
<http://github.com/nddrylliog/nagaqueen>
<http://github.com/nddrylliog/rock>

peg/leg is copyright (c) 2007 by Ian Piumarta
peg/leg is copyright (c) 2007-2012 by Ian Piumarta
released under an MIT license. as is greg.
69 changes: 38 additions & 31 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
*
* THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK.
*
* Last edited: 2007-08-31 13:55:23 by piumarta on emilia.local
* Last edited: 2011-11-25 11:16:57 by piumarta on emilia
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "greg.h"
#include "version.h"
#include "tree.h"

static int yyl(void)
{
Expand All @@ -34,28 +35,17 @@ static void charClassClear(unsigned char bits[], int c) { bits[c >> 3] &= ~(1 <<

typedef void (*setter)(unsigned char bits[], int c);

static int readChar(unsigned char **cp)
static inline int oigit(int c) { return '0' <= c && c <= '7'; }

static int cnext(unsigned char **ccp)
{
unsigned char *cclass = *cp;
int c= *cclass++, i = 0;
unsigned char *cclass = *ccp;
int c= *cclass++;
if (c)
{
if ('\\' == c && *cclass)
{
c= *cclass++;
if (c >= '0' && c <= '9')
{
unsigned char oct= 0;
for (i= 2; i >= 0; i--) {
if (!(c >= '0' && c <= '9'))
break;
oct= (oct * 8) + (c - '0');
c= *cclass++;
}
cclass--;
c= oct;
goto done;
}

switch (c)
switch (c= *cclass++)
{
case 'a': c= '\a'; break; /* bel */
case 'b': c= '\b'; break; /* bs */
Expand All @@ -65,12 +55,18 @@ static int readChar(unsigned char **cp)
case 'r': c= '\r'; break; /* cr */
case 't': c= '\t'; break; /* ht */
case 'v': c= '\v'; break; /* vt */
default: break;
default:
if (oigit(c))
{
c -= '0';
if (oigit(*cclass)) c= (c << 3) + *cclass++ - '0';
if (oigit(*cclass)) c= (c << 3) + *cclass++ - '0';
}
break;
}
}

done:
*cp = cclass;
*ccp = cclass;
}
return c;
}

Expand All @@ -93,16 +89,19 @@ static char *makeCharClass(unsigned char *cclass)
memset(bits, 0, 32);
set= charClassSet;
}
while (0 != (c= readChar(&cclass)))

while (*cclass)
{
if ('-' == c && *cclass && prev >= 0)
if ('-' == *cclass && cclass[1] && prev >= 0)
{
for (c= readChar(&cclass); prev <= c; ++prev)
++cclass;
for (c= cnext(&cclass); prev <= c; ++prev)
set(bits, prev);
prev= -1;
}
else
{
c= cnext(&cclass);
set(bits, prev= c);
}
}
Expand Down Expand Up @@ -155,10 +154,18 @@ static void Node_compile_c_ko(Node *node, int ko)
case String:
{
int len= strlen(node->string.value);
if (1 == len || (2 == len && '\\' == node->string.value[0]))
fprintf(output, " if (!yymatchChar(G, '%s')) goto l%d;", node->string.value, ko);
if (1 == len)
{
if ('\'' == node->string.value[0])
fprintf(output, " if (!yymatchChar(G, '\\'')) goto l%d;", ko);
else
fprintf(output, " if (!yymatchChar(G, '%s')) goto l%d;", node->string.value, ko);
}
else
fprintf(output, " if (!yymatchString(G, \"%s\")) goto l%d;", node->string.value, ko);
if (2 == len && '\\' == node->string.value[0])
fprintf(output, " if (!yymatchChar(G, '%s')) goto l%d;", node->string.value, ko);
else
fprintf(output, " if (!yymatchString(G, \"%s\")) goto l%d;", node->string.value, ko);
}
break;

Expand Down
125 changes: 125 additions & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
EXAMPLES = test test-greg rule rule-greg accept accept-greg wc dc-greg dc dcv dcv-greg calc basic

CFLAGS = -g -O3

DIFF = diff
TEE = cat >

all : $(EXAMPLES)

test : .FORCE
../rpeg -o test.peg.c test.peg
$(CC) $(CFLAGS) -o test test.c
echo 'ab.ac.ad.ae.afg.afh.afg.afh.afi.afj.' | ./$@ | $(TEE) [email protected]
$(DIFF) [email protected] [email protected]
rm -f [email protected]
@echo

test-greg : .FORCE
../greg -o $(@:-greg=.leg).c $(@:-greg=.leg)
sed -e 's/[.]peg[.]c/.leg.c/g' $(@:-greg=.c) > [email protected]
$(CC) $(CFLAGS) -o $@ [email protected]
echo 'ab.ac.ad.ae.afg.afh.afg.afh.afi.afj.' | ./$@ | $(TEE) [email protected]
$(DIFF) $(@:-greg=.ref) [email protected]
rm -f [email protected]
@echo

rule : .FORCE
../rpeg -o rule.peg.c rule.peg
$(CC) $(CFLAGS) -o rule rule.c
echo 'abcbcdabcbcdabcbcdabcbcd' | ./$@ | $(TEE) [email protected]
$(DIFF) [email protected] [email protected]
rm -f [email protected]
@echo

rule-greg : .FORCE
../greg -o $(@:-greg=.leg).c $(@:-greg=.leg)
sed -e 's/[.]peg[.]c/.leg.c/g' $(@:-greg=.c) > [email protected]
$(CC) $(CFLAGS) -o $@ [email protected]
echo 'abcbcdabcbcdabcbcdabcbcd' | ./$@ | $(TEE) [email protected]
$(DIFF) $(@:-greg=.ref) [email protected]
rm -f [email protected]
@echo

accept : .FORCE
../rpeg -o accept.peg.c accept.peg
$(CC) $(CFLAGS) -o accept accept.c
echo 'abcbcdabcbcdabcbcdabcbcd' | ./$@ | $(TEE) [email protected]
$(DIFF) [email protected] [email protected]
rm -f [email protected]
@echo

accept-greg : .FORCE
../greg -o $(@:-greg=.leg).c $(@:-greg=.leg)
sed -e 's/[.]peg[.]c/.leg.c/g' $(@:-greg=.c) > [email protected]
$(CC) $(CFLAGS) -o $@ [email protected]
echo 'abcbcdabcbcdabcbcdabcbcd' | ./$@ | $(TEE) [email protected]
$(DIFF) $(@:-greg=.ref) [email protected]
rm -f [email protected]
@echo

wc : .FORCE
../greg -o wc.leg.c wc.leg
$(CC) $(CFLAGS) -o wc wc.leg.c
cat wc.leg | ./$@ | $(TEE) [email protected]
$(DIFF) [email protected] [email protected]
rm -f [email protected]
@echo

dc : .FORCE
../rpeg -o dc.peg.c dc.peg
$(CC) $(CFLAGS) -o dc dc.c
echo ' 2 *3 *(3+ 4) ' | ./dc | $(TEE) [email protected]
$(DIFF) [email protected] [email protected]
rm -f [email protected]
@echo

dc-greg : .FORCE
../greg -o $(@:-greg=.leg).c $(@:-greg=.leg)
sed -e 's/[.]peg[.]c/.leg.c/g' $(@:-greg=.c) > [email protected]
$(CC) $(CFLAGS) -o $@ [email protected]
echo ' 2 *3 *(3+ 4) ' | ./$@ | $(TEE) [email protected]
$(DIFF) $(@:-greg=.ref) [email protected]
rm -f [email protected]
@echo

dcv : .FORCE
../rpeg -o dcv.peg.c dcv.peg
$(CC) $(CFLAGS) -o dcv dcv.c
echo 'a = 6; b = 7; a * b' | ./dcv | $(TEE) [email protected]
$(DIFF) [email protected] [email protected]
rm -f [email protected]
@echo

dcv-greg : .FORCE
../greg -o $(@:-greg=.leg).c $(@:-greg=.leg)
sed -e 's/[.]peg[.]c/.leg.c/g' $(@:-greg=.c) > [email protected]
$(CC) $(CFLAGS) -o $@ [email protected]
echo 'a = 6; b = 7; a * b' | ./$@ | $(TEE) [email protected]
$(DIFF) $(@:-greg=.ref) [email protected]
rm -f [email protected]
@echo

calc : .FORCE
../greg -o calc.leg.c calc.leg
$(CC) $(CFLAGS) -o calc calc.leg.c
echo 'a = 6; b = 7; a * b' | ./calc | $(TEE) [email protected]
$(DIFF) [email protected] [email protected]
rm -f [email protected]
@echo

basic : .FORCE
../greg -o basic.leg.c basic.leg
$(CC) $(CFLAGS) -o basic basic.leg.c
( echo 'load "test"'; echo "run" ) | ./basic | $(TEE) [email protected]
$(DIFF) [email protected] [email protected]
rm -f [email protected]
@echo

clean : .FORCE
rm -f *~ *.o *.[pl]eg.[cd] $(EXAMPLES) *-greg.c
rm -rf *.dSYM

spotless : clean

.FORCE :
14 changes: 14 additions & 0 deletions examples/accept.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>
#include <stdlib.h>

#include "accept.peg.c"

int main()
{
GREG g;
yyinit(&g);
while (yyparse(&g));
yydeinit(&g);

return 0;
}
File renamed without changes.
8 changes: 8 additions & 0 deletions examples/accept.peg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
start <- abcd+

abcd <- 'a' { printf("A %d\n", G->pos); } bc { printf("ABC %d\n", G->pos); } &{YYACCEPT}
/ 'b' { printf("B %d\n", G->pos); } cd { printf("BCD %d\n", G->pos); } &{YYACCEPT}

bc <- 'b' { printf("B %d\n", G->pos); } 'c' { printf("C %d\n", G->pos); }

cd <- 'c' { printf("C %d\n", G->pos); } 'd' { printf("D %d\n", G->pos); }
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading