home: hub: minipeg

Download patch

ref: 6962d300a10351dcc545446cd1f2a902661d8cca
parent: 1c9faa24f4c93f37db2bddf84d973c02f090bc31
author: Andrew Chambers <ac@acha.ninja>
date: Thu Apr 7 15:38:08 CDT 2022

Work on removing peg code.

--- a/examples/Makefile
+++ b/examples/Makefile
@@ -16,7 +16,7 @@
 	@echo
 
 rule : .FORCE
-	../peg -o rule.leg.c rule.leg
+	../leg -o rule.leg.c rule.leg
 	$(CC) $(CFLAGS) -o rule rule.c
 	echo 'abcbcdabcbcdabcbcdabcbcd' | ./$@ | $(TEE) $@.out
 	$(DIFF) $@.ref $@.out
@@ -24,7 +24,7 @@
 	@echo
 
 accept : .FORCE
-	../peg -o accept.leg.c accept.leg
+	../leg -o accept.leg.c accept.leg
 	$(CC) $(CFLAGS) -o accept accept.c
 	echo 'abcbcdabcbcdabcbcdabcbcd' | ./$@ | $(TEE) $@.out
 	$(DIFF) $@.ref $@.out
@@ -40,7 +40,7 @@
 	@echo
 
 dc : .FORCE
-	../peg -o dc.leg.c dc.leg
+	../leg -o dc.leg.c dc.leg
 	$(CC) $(CFLAGS) -o dc dc.c
 	echo '  2  *3 *(3+ 4) ' | ./dc | $(TEE) $@.out
 	$(DIFF) $@.ref $@.out
@@ -48,7 +48,7 @@
 	@echo
 
 dcv : .FORCE
-	../peg -o dcv.leg.c dcv.leg
+	../leg -o dcv.leg.c dcv.leg
 	$(CC) $(CFLAGS) -o dcv dcv.c
 	echo 'a = 6;  b = 7;  a * b' | ./dcv | $(TEE) $@.out
 	$(DIFF) $@.ref $@.out
@@ -72,7 +72,7 @@
 	@echo
 
 localpeg : .FORCE
-	../peg -o test.leg.c test.leg
+	../leg -o test.leg.c test.leg
 	$(CC) $(CFLAGS) -o localpeg localpeg.c
 	echo 'ab.ac.ad.ae.afg.afh.afg.afh.afi.afj.' | ./$@ | $(TEE) $@.out
 	$(DIFF) $@.ref $@.out
@@ -96,9 +96,6 @@
 	@echo
 
 clean : .FORCE
-	rm -f *~ *.o *.[pl]eg.[cd] $(EXAMPLES)
-	rm -rf *.dSYM
-
-spotless : clean
+	rm -f *.o *.leg.c $(EXAMPLES)
 
 .FORCE :
--- a/examples/accept.c
+++ b/examples/accept.c
@@ -1,7 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#include "accept.peg.c"
+#include "accept.leg.c"
 
 int main()
 {
--- /dev/null
+++ b/examples/accept.leg
@@ -1,0 +1,8 @@
+start	= abcd+
+
+abcd	= 'a' { printf("A %d\n", yypos); } bc { printf("ABC %d\n", yypos); } &{YYACCEPT}
+	 | 'b' { printf("B %d\n", yypos); } cd { printf("BCD %d\n", yypos); } &{YYACCEPT}
+
+bc	= 'b' { printf("B %d\n", yypos); } 'c' { printf("C %d\n", yypos); }
+
+cd	= 'c' { printf("C %d\n", yypos); } 'd' { printf("D %d\n", yypos); }
\ No newline at end of file
--- a/examples/accept.peg
+++ /dev/null
@@ -1,8 +1,0 @@
-start	<- abcd+
-
-abcd	<- 'a' { printf("A %d\n", yypos); } bc { printf("ABC %d\n", yypos); } &{YYACCEPT}
-	 / 'b' { printf("B %d\n", yypos); } cd { printf("BCD %d\n", yypos); } &{YYACCEPT}
-
-bc	<- 'b' { printf("B %d\n", yypos); } 'c' { printf("C %d\n", yypos); }
-
-cd	<- 'c' { printf("C %d\n", yypos); } 'd' { printf("D %d\n", yypos); }
--- a/examples/dc.c
+++ b/examples/dc.c
@@ -7,7 +7,7 @@
 int push(int n)	{ return stack[++stackp]= n; }
 int pop(void)	{ return stack[stackp--]; }
 
-#include "dc.peg.c"
+#include "dc.leg.c"
 
 int main()
 {
--- /dev/null
+++ b/examples/dc.leg
@@ -1,0 +1,27 @@
+# Grammar
+
+Expr	= SPACE Sum EOL		{ printf("%d\n", pop()); }
+	 | (!EOL .)* EOL		{ printf("error\n"); }
+
+Sum	= Product ( PLUS  Product	{ int r= pop(), l= pop();  push(l + r); }
+		   | MINUS Product	{ int r= pop(), l= pop();  push(l - r); }
+		   )*
+
+Product	= Value ( TIMES  Value		{ int r= pop(), l= pop();  push(l * r); }
+		 | DIVIDE Value		{ int r= pop(), l= pop();  push(l / r); }
+		 )*
+
+Value	= NUMBER			{ push(atoi(yytext)); }
+	 | OPEN Sum CLOSE
+
+# Lexemes
+
+NUMBER	= < [0-9]+ > SPACE
+PLUS	= '+' SPACE
+MINUS	= '-' SPACE
+TIMES	= '*' SPACE
+DIVIDE	= '/' SPACE
+OPEN	= '(' SPACE
+CLOSE	= ')' SPACE
+SPACE	= [ \t]*
+EOL	= '\n' | '\r\n' | '\r'
--- a/examples/dc.peg
+++ /dev/null
@@ -1,27 +1,0 @@
-# Grammar
-
-Expr	<- SPACE Sum EOL		{ printf("%d\n", pop()); }
-	 / (!EOL .)* EOL		{ printf("error\n"); }
-
-Sum	<- Product ( PLUS  Product	{ int r= pop(), l= pop();  push(l + r); }
-		   / MINUS Product	{ int r= pop(), l= pop();  push(l - r); }
-		   )*
-
-Product	<- Value ( TIMES  Value		{ int r= pop(), l= pop();  push(l * r); }
-                 / DIVIDE Value		{ int r= pop(), l= pop();  push(l / r); }
-		 )*
-
-Value	<- NUMBER			{ push(atoi(yytext)); }
-	 / OPEN Sum CLOSE
-
-# Lexemes
-
-NUMBER	<- < [0-9]+ > SPACE
-PLUS	<- '+' SPACE
-MINUS	<- '-' SPACE
-TIMES	<- '*' SPACE
-DIVIDE	<- '/' SPACE
-OPEN	<- '(' SPACE
-CLOSE	<- ')' SPACE
-SPACE	<- [ \t]*
-EOL	<- '\n' / '\r\n' / '\r'
--- a/examples/dcv.c
+++ b/examples/dcv.c
@@ -10,7 +10,7 @@
 int pop(void)	{ return stack[stackp--]; }
 int top(void)	{ return stack[stackp]; }
 
-#include "dcv.peg.c"
+#include "dcv.leg.c"
 
 int main()
 {
--- /dev/null
+++ b/examples/dcv.leg
@@ -1,0 +1,32 @@
+
+Stmt	= SPACE Expr EOL			{ printf("%d\n", pop()); }
+	 | (!EOL .)* EOL			{ printf("error\n"); }
+
+Expr	= ID { var= yytext[0] } ASSIGN Sum	{ vars[var - 'a']= top(); }
+	 | Sum
+
+Sum	= Product ( PLUS  Product		{ int r= pop(), l= pop();  push(l + r); }
+		   | MINUS Product		{ int r= pop(), l= pop();  push(l - r); }
+		   )*
+
+Product	= Value ( TIMES  Value			{ int r= pop(), l= pop();  push(l * r); }
+                 | DIVIDE Value			{ int r= pop(), l= pop();  push(l / r); }
+		 )*
+
+Value	= NUMBER				{ push(atoi(yytext)); }
+	 | < ID > !ASSIGN			{ push(vars[yytext[0] - 'a']); }
+	 | OPEN Expr CLOSE
+
+
+NUMBER	= < [0-9]+ >	SPACE
+ID	= < [a-z]  >	SPACE
+ASSIGN	= '='		SPACE
+PLUS	= '+'		SPACE
+MINUS	= '-'		SPACE
+TIMES	= '*'		SPACE
+DIVIDE	= '/'		SPACE
+OPEN	= '('		SPACE
+CLOSE	= ')'		SPACE
+
+SPACE	= [ \t]*
+EOL	= '\n' | '\r\n' | '\r' | ';'
--- a/examples/dcv.peg
+++ /dev/null
@@ -1,34 +1,0 @@
-# Grammar
-
-Stmt	<- SPACE Expr EOL			{ printf("%d\n", pop()); }
-	 / (!EOL .)* EOL			{ printf("error\n"); }
-
-Expr	<- ID { var= yytext[0] } ASSIGN Sum	{ vars[var - 'a']= top(); }
-	 / Sum
-
-Sum	<- Product ( PLUS  Product		{ int r= pop(), l= pop();  push(l + r); }
-		   / MINUS Product		{ int r= pop(), l= pop();  push(l - r); }
-		   )*
-
-Product	<- Value ( TIMES  Value			{ int r= pop(), l= pop();  push(l * r); }
-                 / DIVIDE Value			{ int r= pop(), l= pop();  push(l / r); }
-		 )*
-
-Value	<- NUMBER				{ push(atoi(yytext)); }
-	 / < ID > !ASSIGN			{ push(vars[yytext[0] - 'a']); }
-	 / OPEN Expr CLOSE
-
-# Lexemes
-
-NUMBER	<- < [0-9]+ >	SPACE
-ID	<- < [a-z]  >	SPACE
-ASSIGN	<- '='		SPACE
-PLUS	<- '+'		SPACE
-MINUS	<- '-'		SPACE
-TIMES	<- '*'		SPACE
-DIVIDE	<- '/'		SPACE
-OPEN	<- '('		SPACE
-CLOSE	<- ')'		SPACE
-
-SPACE	<- [ \t]*
-EOL	<- '\n' / '\r\n' / '\r' / ';'
--- a/examples/left.c
+++ b/examples/left.c
@@ -7,7 +7,7 @@
   if (EOF != c) printf("<%c>\n", c);			\
 }
 
-#include "left.peg.c"
+#include "left.leg.c"
 
 int main()
 {
--- /dev/null
+++ b/examples/left.leg
@@ -1,0 +1,2 @@
+
+S	= (S 'a' | 'a') !'a'
--- a/examples/left.peg
+++ /dev/null
@@ -1,3 +1,0 @@
-# Grammar
-
-S	<- (S 'a' / 'a') !'a'
--- a/examples/rule.c
+++ b/examples/rule.c
@@ -1,7 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#include "rule.peg.c"
+#include "rule.leg.c"
 
 int main()
 {
--- /dev/null
+++ b/examples/rule.leg
@@ -1,0 +1,8 @@
+start	= abcd+
+
+abcd	= 'a' { printf("A %d\n", yypos); } bc { printf("ABC %d\n", yypos); }
+	 | 'b' { printf("B %d\n", yypos); } cd { printf("BCD %d\n", yypos); };
+
+bc	= 'b' { printf("B %d\n", yypos); } 'c' { printf("C %d\n", yypos); };
+
+cd	= 'c' { printf("C %d\n", yypos); } 'd' { printf("D %d\n", yypos); };
--- a/examples/rule.peg
+++ /dev/null
@@ -1,8 +1,0 @@
-start	<- abcd+
-
-abcd	<- 'a' { printf("A %d\n", yypos); } bc { printf("ABC %d\n", yypos); }
-	 / 'b' { printf("B %d\n", yypos); } cd { printf("BCD %d\n", yypos); }
-
-bc	<- 'b' { printf("B %d\n", yypos); } 'c' { printf("C %d\n", yypos); }
-
-cd	<- 'c' { printf("C %d\n", yypos); } 'd' { printf("D %d\n", yypos); }