Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added original examples

  • Loading branch information...
commit 07152d39baf2af92f9d46643e89e7a53846eac0a 1 parent 91465be
@tj authored
View
79 examples/Makefile
@@ -0,0 +1,79 @@
+EXAMPLES = test rule accept wc dc dcv calc basic
+
+CFLAGS = -g -O3
+
+DIFF = diff
+TEE = cat >
+
+all : $(EXAMPLES)
+
+test : .FORCE
+ ../peg -o test.peg.c test.peg
+ $(CC) $(CFLAGS) -o test test.c
+ echo 'ab.ac.ad.ae.afg.afh.afg.afh.afi.afj.' | ./$@ | $(TEE) $@.out
+ $(DIFF) $@.ref $@.out
+ rm -f $@.out
+ @echo
+
+rule : .FORCE
+ ../peg -o rule.peg.c rule.peg
+ $(CC) $(CFLAGS) -o rule rule.c
+ echo 'abcbcdabcbcdabcbcdabcbcd' | ./$@ | $(TEE) $@.out
+ $(DIFF) $@.ref $@.out
+ rm -f $@.out
+ @echo
+
+accept : .FORCE
+ ../peg -o accept.peg.c accept.peg
+ $(CC) $(CFLAGS) -o accept accept.c
+ echo 'abcbcdabcbcdabcbcdabcbcd' | ./$@ | $(TEE) $@.out
+ $(DIFF) $@.ref $@.out
+ rm -f $@.out
+ @echo
+
+wc : .FORCE
+ ../leg -o wc.leg.c wc.leg
+ $(CC) $(CFLAGS) -o wc wc.leg.c
+ cat wc.leg | ./$@ | $(TEE) $@.out
+ $(DIFF) $@.ref $@.out
+ rm -f $@.out
+ @echo
+
+dc : .FORCE
+ ../peg -o dc.peg.c dc.peg
+ $(CC) $(CFLAGS) -o dc dc.c
+ echo ' 2 *3 *(3+ 4) ' | ./dc | $(TEE) $@.out
+ $(DIFF) $@.ref $@.out
+ rm -f $@.out
+ @echo
+
+dcv : .FORCE
+ ../peg -o dcv.peg.c dcv.peg
+ $(CC) $(CFLAGS) -o dcv dcv.c
+ echo 'a = 6; b = 7; a * b' | ./dcv | $(TEE) $@.out
+ $(DIFF) $@.ref $@.out
+ rm -f $@.out
+ @echo
+
+calc : .FORCE
+ ../leg -o calc.leg.c calc.leg
+ $(CC) $(CFLAGS) -o calc calc.leg.c
+ echo 'a = 6; b = 7; a * b' | ./calc | $(TEE) $@.out
+ $(DIFF) $@.ref $@.out
+ rm -f $@.out
+ @echo
+
+basic : .FORCE
+ ../leg -o basic.leg.c basic.leg
+ $(CC) $(CFLAGS) -o basic basic.leg.c
+ ( echo 'load "test"'; echo "run" ) | ./basic | $(TEE) $@.out
+ $(DIFF) $@.ref $@.out
+ rm -f $@.out
+ @echo
+
+clean : .FORCE
+ rm -f *~ *.o *.[pl]eg.[cd] $(EXAMPLES)
+
+spotless : clean
+
+.FORCE :
View
11 examples/accept.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "accept.peg.c"
+
+int main()
+{
+ while (yyparse());
+
+ return 0;
+}
View
8 examples/accept.peg
@@ -0,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); }
View
32 examples/accept.ref
@@ -0,0 +1,32 @@
+A 3
+B 3
+C 3
+ABC 3
+B 3
+C 3
+D 3
+BCD 3
+A 3
+B 3
+C 3
+ABC 3
+B 3
+C 3
+D 3
+BCD 3
+A 3
+B 3
+C 3
+ABC 3
+B 3
+C 3
+D 3
+BCD 3
+A 3
+B 3
+C 3
+ABC 3
+B 3
+C 3
+D 3
+BCD 3
View
360 examples/basic.leg
@@ -0,0 +1,360 @@
+# A 'syntax-directed interpreter' (all execution is a side-effect of parsing).
+# Inspired by Dennis Allison's original Tiny BASIC grammar, circa 1975.
+#
+# Copyright (c) 2007 by Ian Piumarta
+# All rights reserved.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the 'Software'),
+# to deal in the Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish, distribute, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, provided that the above copyright notice(s) and this
+# permission notice appear in all copies of the Software. Acknowledgement
+# of the use of this Software in supporting documentation would be
+# appreciated but is not required.
+#
+# THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK.
+#
+# Last edited: 2007-05-14 11:32:49 by piumarta on emilia
+
+%{
+# include <stdio.h>
+
+ typedef struct line line;
+
+ struct line
+ {
+ int number;
+ int length;
+ char *text;
+ };
+
+ line *lines= 0;
+ int numLines= 0;
+ int pc= -1, epc= -1;
+ int batch= 0;
+
+ int getline(char *buf, int max);
+
+# define min(x, y) ((x) < (y) ? (x) : (y))
+
+# define YY_INPUT(buf, result, max_size) \
+ { \
+ if ((pc >= 0) && (pc < numLines)) \
+ { \
+ line *linep= lines+pc++; \
+ result= min(max_size, linep->length); \
+ memcpy(buf, linep->text, result); \
+ } \
+ else \
+ result= getline(buf, max_size); \
+ }
+
+ union value {
+ int number;
+ char *string;
+ int (*binop)(int lhs, int rhs);
+ };
+
+# define YYSTYPE union value
+
+ int variables[26];
+
+ void accept(int number, char *line);
+
+ void save(char *name);
+ void load(char *name);
+ void type(char *name);
+
+ int lessThan(int lhs, int rhs) { return lhs < rhs; }
+ int lessEqual(int lhs, int rhs) { return lhs <= rhs; }
+ int notEqual(int lhs, int rhs) { return lhs != rhs; }
+ int equalTo(int lhs, int rhs) { return lhs == rhs; }
+ int greaterEqual(int lhs, int rhs) { return lhs >= rhs; }
+ int greaterThan(int lhs, int rhs) { return lhs > rhs; }
+
+ int input(void);
+
+ int stack[1024], sp= 0;
+
+ char *help;
+
+ void error(char *fmt, ...);
+%}
+
+line = - s:statement CR
+| - n:number < ( !CR . )* CR > { accept(n.number, yytext); }
+| - CR
+| - < ( !CR . )* CR > { epc= pc; error("syntax error"); }
+| - !. { exit(0); }
+
+statement = 'print'- expr-list
+| 'if'- e1:expression r:relop e2:expression { if (!r.binop(e1.number, e2.number)) yythunkpos= 0; }
+ 'then'- statement
+| 'goto'- e:expression { epc= pc; if ((pc= findLine(e.number, 0)) < 0) error("no such line"); }
+| 'input'- var-list
+| 'let'- v:var EQUAL e:expression { variables[v.number]= e.number; }
+| 'gosub'- e:expression { epc= pc; if (sp < 1024) stack[sp++]= pc, pc= findLine(e.number); else error("too many gosubs");
+ if (pc < 0) error("no such line"); }
+| 'return'- { epc= pc; if ((pc= sp ? stack[--sp] : -1) < 0) error("no gosub"); }
+| 'clear'- { while (numLines) accept(lines->number, "\n"); }
+| 'list'- { int i; for (i= 0; i < numLines; ++i) printf("%5d %s", lines[i].number, lines[i].text); }
+| 'run'- s:string { load(s.string); pc= 0; }
+| 'run'- { pc= 0; }
+| 'end'- { pc= -1; if (batch) exit(0); }
+| 'rem'- ( !CR . )*
+| ('bye'|'quit'|'exit')- { exit(0); }
+| 'save'- s:string { save(s.string); }
+| 'load'- s:string { load(s.string); }
+| 'type'- s:string { type(s.string); }
+| 'dir'- { system("ls *.bas"); }
+| 'help'- { fprintf(stderr, "%s", help); }
+
+expr-list = ( e:string { printf("%s", e.string); }
+ | e:expression { printf("%d", e.number); }
+ )? ( COMMA ( e:string { printf("%s", e.string); }
+ | e:expression { printf("%d", e.number); }
+ )
+ )* ( COMMA
+ | !COMMA { printf("\n"); }
+ )
+
+var-list = v:var { variables[v.number]= input(); }
+ ( COMMA v:var { variables[v.number]= input(); }
+ )*
+
+expression = ( PLUS? l:term
+ | MINUS l:term { l.number = -l.number }
+ ) ( PLUS r:term { l.number += r.number }
+ | MINUS r:term { l.number -= r.number }
+ )* { $$.number = l.number }
+
+term = l:factor ( STAR r:factor { l.number *= r.number }
+ | SLASH r:factor { l.number /= r.number }
+ )* { $$.number = l.number }
+
+factor = v:var { $$.number = variables[v.number] }
+| n:number
+| OPEN expression CLOSE
+
+var = < [a-z] > - { $$.number = yytext[0] - 'a' }
+
+number = < digit+ > - { $$.number = atoi(yytext); }
+
+digit = [0-9]
+
+string = '"' < [^\"]* > '"' - { $$.string = yytext; }
+
+relop = '<=' - { $$.binop= lessEqual; }
+| '<>' - { $$.binop= notEqual; }
+| '<' - { $$.binop= lessThan; }
+| '>=' - { $$.binop= greaterEqual; }
+| '>' - { $$.binop= greaterThan; }
+| '=' - { $$.binop= equalTo; }
+
+EQUAL = '=' - CLOSE = ')' - OPEN = '(' -
+SLASH = '/' - STAR = '*' - MINUS = '-' -
+PLUS = '+' - COMMA = ',' -
+
+- = [ \t]*
+
+CR = '\n' | '\r' | '\r\n'
+
+%%
+
+#include <unistd.h>
+#include <stdarg.h>
+
+char *help=
+ "print <num>|<string> [, <num>|<string> ...] [,]\n"
+ "if <expr> <|<=|<>|=|>=|> <expr> then <stmt>\n"
+ "input <var> [, <var> ...] let <var> = <expr>\n"
+ "goto <expr> gosub <expr>\n"
+ "end return\n"
+ "list clear\n"
+ "run [\"filename\"] rem <comment...>\n"
+ "dir type \"filename\"\n"
+ "save \"filename\" load \"filename\"\n"
+ "bye|quit|exit help\n"
+ ;
+
+void error(char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ if (epc > 0)
+ fprintf(stderr, "\nline %d: %s", lines[epc-1].number, lines[epc-1].text);
+ else
+ fprintf(stderr, "\n");
+ vfprintf(stderr, fmt, ap);
+ fprintf(stderr, "\n");
+ va_end(ap);
+ epc= pc= -1;
+}
+
+#ifdef USE_READLINE
+# include <readline/readline.h>
+# include <readline/history.h>
+#endif
+
+int getline(char *buf, int max)
+{
+ pc= -1;
+ if (batch) exit(0);
+ if (isatty(fileno(stdin)))
+ {
+# ifdef USE_READLINE
+ char *line= readline(">");
+ if (line)
+ {
+ int len= strlen(line);
+ if (len >= max) len= max - 1;
+ strncpy(buf, line, len);
+ (buf)[len]= '\n';
+ add_history(line);
+ free(line);
+ return len + 1;
+ }
+ else
+ {
+ printf("\n");
+ return 0;
+ }
+# endif
+ putchar('>');
+ fflush(stdout);
+ }
+ return fgets(buf, max, stdin) ? strlen(buf) : 0;
+}
+
+int maxLines= 0;
+
+int findLine(int n, int create)
+{
+ int lo= 0, hi= numLines - 1;
+ while (lo <= hi)
+ {
+ int mid= (lo + hi) / 2, lno= lines[mid].number;
+ if (lno > n)
+ hi= mid - 1;
+ else if (lno < n)
+ lo= mid + 1;
+ else
+ return mid;
+ }
+ if (create)
+ {
+ if (numLines == maxLines)
+ {
+ maxLines *= 2;
+ lines= realloc(lines, sizeof(line) * maxLines);
+ }
+ if (lo < numLines)
+ memmove(lines + lo + 1, lines + lo, sizeof(line) * (numLines - lo));
+ ++numLines;
+ lines[lo].number= n;
+ lines[lo].text= 0;
+ return lo;
+ }
+ return -1;
+}
+
+void accept(int n, char *s)
+{
+ if (s[0] < 32) /* delete */
+ {
+ int lno= findLine(n, 0);
+ if (lno >= 0)
+ {
+ if (lno < numLines - 1)
+ memmove(lines + lno, lines + lno + 1, sizeof(line) * (numLines - lno - 1));
+ --numLines;
+ }
+ }
+ else /* insert */
+ {
+ int lno= findLine(n, 1);
+ if (lines[lno].text) free(lines[lno].text);
+ lines[lno].length= strlen(s);
+ lines[lno].text= strdup(s);
+ }
+}
+
+char *extend(char *name)
+{
+ static char path[1024];
+ int len= strlen(name);
+ sprintf(path, "%s%s", name, (((len > 4) && !strcasecmp(".bas", name + len - 4)) ? "" : ".bas"));
+ return path;
+}
+
+void save(char *name)
+{
+ FILE *f= fopen(name= extend(name), "w");
+ if (!f)
+ perror(name);
+ else
+ {
+ int i;
+ for (i= 0; i < numLines; ++i)
+ fprintf(f, "%d %s", lines[i].number, lines[i].text);
+ fclose(f);
+ }
+}
+
+void load(char *name)
+{
+ FILE *f= fopen(name= extend(name), "r");
+ if (!f)
+ perror(name);
+ else
+ {
+ int lineNumber;
+ char lineText[1024];
+ while ((1 == fscanf(f, " %d ", &lineNumber)) && fgets(lineText, sizeof(lineText), f))
+ accept(lineNumber, lineText);
+ fclose(f);
+ }
+}
+
+void type(char *name)
+{
+ FILE *f= fopen(name= extend(name), "r");
+ if (!f)
+ perror(name);
+ else
+ {
+ int c, d;
+ while ((c= getc(f)) >= 0)
+ putchar(d= c);
+ fclose(f);
+ if ('\n' != d && '\r' != d) putchar('\n');
+ }
+}
+
+int input(void)
+{
+ char line[32];
+ fgets(line, sizeof(line), stdin);
+ return atoi(line);
+}
+
+int main(int argc, char **argv)
+{
+ lines= malloc(sizeof(line) * (maxLines= 32));
+ numLines= 0;
+
+ if (argc > 1)
+ {
+ batch= 1;
+ while (argc-- > 1)
+ load(*++argv);
+ pc= 0;
+ }
+
+ while (!feof(stdin))
+ yyparse();
+
+ return 0;
+}
View
10 examples/basic.ref
@@ -0,0 +1,10 @@
+ 1
+ 2 4
+ 3 6 9
+ 4 8 12 16
+ 5 10 15 20 25
+ 6 12 18 24 30 36
+ 7 14 21 28 35 42 49
+ 8 16 24 32 40 48 56 64
+ 9 18 27 36 45 54 63 72 81
+ 10 20 30 40 50 60 70 80 90 100
View
8 examples/bench.bas
@@ -0,0 +1,8 @@
+100 let n=100000
+120 let m=0
+110 let s=0
+130 let m=m+1
+140 let s=s+m
+150 if m<n then goto 130
+160 print "interpreted ", n*3, " lines of code; answer is ", s
+170 end
View
46 examples/calc.leg
@@ -0,0 +1,46 @@
+%{
+#include <stdio.h>
+int vars[26];
+%}
+
+Stmt = - e:Expr EOL { printf("%d\n", e); }
+ | ( !EOL . )* EOL { printf("error\n"); }
+
+Expr = i:ID ASSIGN s:Sum { $$= vars[i]= s; }
+ | s:Sum { $$= s; }
+
+Sum = l:Product
+ ( PLUS r:Product { l += r; }
+ | MINUS r:Product { l -= r; }
+ )* { $$= l; }
+
+Product = l:Value
+ ( TIMES r:Value { l *= r; }
+ | DIVIDE r:Value { l /= r; }
+ )* { $$= l; }
+
+Value = i:NUMBER { $$= atoi(yytext); }
+ | i:ID !ASSIGN { $$= vars[i]; }
+ | OPEN i:Expr CLOSE { $$= i; }
+
+NUMBER = < [0-9]+ > - { $$= atoi(yytext); }
+ID = < [a-z] > - { $$= yytext[0] - 'a'; }
+ASSIGN = '=' -
+PLUS = '+' -
+MINUS = '-' -
+TIMES = '*' -
+DIVIDE = '/' -
+OPEN = '(' -
+CLOSE = ')' -
+
+- = [ \t]*
+EOL = '\n' | '\r\n' | '\r' | ';'
+
+%%
+
+int main()
+{
+ while (yyparse());
+
+ return 0;
+}
View
3  examples/calc.ref
@@ -0,0 +1,3 @@
+6
+7
+42
View
17 examples/dc.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+int stack[1024];
+int stackp= -1;
+
+int push(int n) { return stack[++stackp]= n; }
+int pop(void) { return stack[stackp--]; }
+
+#include "dc.peg.c"
+
+int main()
+{
+ while (yyparse());
+
+ return 0;
+}
View
27 examples/dc.peg
@@ -0,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'
View
1  examples/dc.ref
@@ -0,0 +1 @@
+42
View
20 examples/dcv.c
@@ -0,0 +1,20 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+int stack[1024];
+int stackp= -1;
+int var= 0;
+int vars[26];
+
+int push(int n) { return stack[++stackp]= n; }
+int pop(void) { return stack[stackp--]; }
+int top(void) { return stack[stackp]; }
+
+#include "dcv.peg.c"
+
+int main()
+{
+ while (yyparse());
+
+ return 0;
+}
View
34 examples/dcv.peg
@@ -0,0 +1,34 @@
+# 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' / ';'
View
3  examples/dcv.ref
@@ -0,0 +1,3 @@
+6
+7
+42
View
17 examples/fibonacci.bas
@@ -0,0 +1,17 @@
+100 let n=32
+110 gosub 200
+120 print "fibonacci(",n,") = ", m
+130 end
+
+200 let c=n
+210 let b=1
+220 if c<2 then goto 400
+230 let c=c-1
+240 let a=1
+300 let c=c-1
+310 let d=a+b
+320 let a=b
+330 let b=d+1
+340 if c<>0 then goto 300
+400 let m=b
+410 return
View
17 examples/left.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+
+#define YY_INPUT(buf, result, max) \
+{ \
+ int c= getchar(); \
+ result= (EOF == c) ? 0 : (*(buf)= c, 1); \
+ if (EOF != c) printf("<%c>\n", c); \
+}
+
+#include "left.peg.c"
+
+int main()
+{
+ printf(yyparse() ? "success\n" : "failure\n");
+
+ return 0;
+}
View
3  examples/left.peg
@@ -0,0 +1,3 @@
+# Grammar
+
+S <- (S 'a' / 'a') !'a'
View
11 examples/rule.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "rule.peg.c"
+
+int main()
+{
+ while (yyparse());
+
+ return 0;
+}
View
8 examples/rule.peg
@@ -0,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); }
View
32 examples/rule.ref
@@ -0,0 +1,32 @@
+A 24
+B 24
+C 24
+ABC 24
+B 24
+C 24
+D 24
+BCD 24
+A 24
+B 24
+C 24
+ABC 24
+B 24
+C 24
+D 24
+BCD 24
+A 24
+B 24
+C 24
+ABC 24
+B 24
+C 24
+D 24
+BCD 24
+A 24
+B 24
+C 24
+ABC 24
+B 24
+C 24
+D 24
+BCD 24
View
12 examples/test.bas
@@ -0,0 +1,12 @@
+10 let i=1
+20 gosub 100
+30 let i=i+1
+40 if i<=10 then goto 20
+50 end
+
+100 let j=1
+110 print " ", i*j,
+120 let j=j+1
+130 if j<=i then goto 110
+140 print
+150 return
View
8 examples/test.c
@@ -0,0 +1,8 @@
+#include <stdio.h>
+#include "test.peg.c"
+
+int main()
+{
+ while (yyparse());
+ return 0;
+}
View
13 examples/test.peg
@@ -0,0 +1,13 @@
+start <- body '.' { printf(".\n"); }
+
+body <- 'a' { printf("a1 "); } 'b' { printf("ab1 "); }
+
+ / 'a' { printf("a2 "); } 'c' { printf("ac2 "); }
+
+ / 'a' { printf("a3 "); } ( 'd' { printf("ad3 "); } / 'e' { printf("ae3 "); } )
+
+ / 'a' { printf("a4 "); } ( 'f' { printf("af4 "); } 'g' { printf("afg4 "); }
+ / 'f' { printf("af5 "); } 'h' { printf("afh5 "); } )
+
+ / 'a' { printf("a6 "); } ( 'f' &{ printf("af6 ") } 'i' &{ printf("afi6 ") }
+ / 'f' &{ printf("af7 ") } 'j' &{ printf("afj7 ") } )
View
10 examples/test.ref
@@ -0,0 +1,10 @@
+a1 ab1 .
+a2 ac2 .
+a3 ad3 .
+a3 ae3 .
+a4 af4 afg4 .
+a4 af5 afh5 .
+a4 af4 afg4 .
+a4 af5 afh5 .
+af6 afi6 a6 .
+af6 af7 afj7 a6 .
View
14 examples/username.leg
@@ -0,0 +1,14 @@
+%{
+#include <unistd.h>
+%}
+
+start = "username" { printf("%s", getlogin()); }
+| < . > { putchar(yytext[0]); }
+
+%%
+
+int main()
+{
+ while (yyparse());
+ return 0;
+}
View
22 examples/wc.leg
@@ -0,0 +1,22 @@
+%{
+#include <stdio.h>
+int lines= 0, words= 0, chars= 0;
+%}
+
+start = (line | word | char)
+
+line = < (( '\n' '\r'* ) | ( '\r' '\n'* )) > { lines++; chars += yyleng; }
+word = < [a-zA-Z]+ > { words++; chars += yyleng; printf("<%s>\n", yytext); }
+char = . { chars++; }
+
+%%
+
+int main()
+{
+ while (yyparse())
+ ;
+ printf("%d lines\n", lines);
+ printf("%d chars\n", chars);
+ printf("%d words\n", words);
+ return 0;
+}
View
55 examples/wc.ref
@@ -0,0 +1,55 @@
+<include>
+<stdio>
+<h>
+<int>
+<lines>
+<words>
+<chars>
+<start>
+<line>
+<word>
+<char>
+<line>
+<n>
+<r>
+<r>
+<n>
+<lines>
+<chars>
+<yyleng>
+<word>
+<a>
+<zA>
+<Z>
+<words>
+<chars>
+<yyleng>
+<printf>
+<s>
+<n>
+<yytext>
+<char>
+<chars>
+<int>
+<main>
+<while>
+<yyparse>
+<printf>
+<d>
+<lines>
+<n>
+<lines>
+<printf>
+<d>
+<chars>
+<n>
+<chars>
+<printf>
+<d>
+<words>
+<n>
+<words>
+<return>
+22 lines
+425 chars
+52 words
Please sign in to comment.
Something went wrong with that request. Please try again.