Skip to content

Commit

Permalink
adding the rest of vern's files
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Estes committed May 1, 2001
1 parent 26e7846 commit 2eae880
Show file tree
Hide file tree
Showing 52 changed files with 19,026 additions and 0 deletions.
15 changes: 15 additions & 0 deletions examples/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
This directory contains some examples of what you can do with
flex. These files are not tested regularly so you might have to tinker
a bit before they work for you. Updates, new files and patches are welcome.

- debflex.awk, an awk script for anotating flex debug output.
It presently only works with gawk and mawk, not with "old"
or "new" awk.

- testxxLexer.l, a sample C++ program that uses flex's scanner
class option ("-+").

- fastwc/, a subdirectory containing examples of how to use flex
to write progressively higher-performance versions of the Unix
"wc" utility. This certainly should work with 2.5, but hasn't
been tested.
119 changes: 119 additions & 0 deletions examples/debflex.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Clarify the flex debug trace by substituting first line of each rule.
# Francois Pinard <[email protected]>, July 1990.
#
# Rewritten to process correctly \n's in scanner input.
# BEGIN section modified to correct a collection of rules.
# Michal Jaegermann <[email protected]>, December 1993
#
# Sample usage:
# flex -d PROGRAM.l
# gcc -o PROGRAM PROGRAM.c -lfl
# PROGRAM 2>&1 | gawk -f debflex.awk PROGRAM.l
#
# (VP's note: this script presently does not work with either "old" or
# "new" awk; fixes so it does will be welcome)

BEGIN {
# Insure proper usage.

if (ARGC != 2) {
print "usage: gawk -f debflex.awk FLEX_SOURCE <DEBUG_OUTPUT";
exit (1);
}

# Remove and save the name of flex source.

source = ARGV[1];
ARGC--;

# Swallow the flex source file.

line = 0;
section = 1;
while (getline <source) {

# Count the lines.

line++;

# Count the sections. When encountering section 3,
# break out of the awk BEGIN block.

if (match ($0, /^%%/)) {
section++;
if (section == 3) {
break;
}
}
else {
# Only the lines in section 2 which do not begin in a
# tab or space might be referred to by the flex debug
# trace. Save only those lines.

if (section == 2 && match ($0, /^[^ \t]/)) {
rules[line] = $0;
}
}
}
dashes = "-----------------------------------------------------------";
collect = "";
line = 0;
}

# collect complete rule output from a scanner
$0 !~ /^--/ {
collect = collect "\n" $0;
next;
}
# otherwise we have a new rule - process what we got so far
{
process();
}
# and the same thing if we hit EOF
END {
process();
}

function process() {

# splitting this way we loose some double dashes and
# left parentheses from echoed input - a small price to pay
n = split(collect, field, "\n--|[(]");

# this loop kicks in only when we already collected something
for (i = 1; i <= n; i++) {
if (0 != line) {
# we do not care for traces of newlines.
if (0 == match(field[i], /\"\n+\"[)]/)) {
if (rules[line]) {
text = field[i];
while ( ++i <= n) {
text = text field[i];
}
printf("%s:%d: %-8s -- %s\n",
source, line, text, rules[line]);
}
else {
print;
printf "%s:%d: *** No such rule.\n", source, line;
}
}
line = 0;
break;
}
if ("" != field[i]) {
if ("end of buffer or a NUL)" == field[i]) {
print dashes; # Simplify trace of buffer reloads
continue;
}
if (match(field[i], /accepting rule at line /)) {
# force interpretation of line as a number
line = 0 + substr(field[i], RLENGTH);
continue;
}
# echo everything else
printf("--%s\n", field[i]);
}
}
collect = "\n" $0; # ... and start next trace
}
24 changes: 24 additions & 0 deletions examples/manual/ChangeLog
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Tue Oct 5 21:51:59 1993 Vern Paxson

* Removed FILTER/ subdirectory.

* Removed alloca.c.

* Changed Makefile definition of CC to just "gcc -g", removed
assumption of alloca being present.

* Added pointer to MISC/fastwc/ to wc.lex.

Tue Jun 8 15:47:39 1993 Gavin Thomas Nicol (nick at sillybugs)

* Changed main() in wc.lex extensively. The old version would not
work correctly without the YY_NEW_FILE. (lex handles the older
version OK though).

* Added a rule to expr.lex to handle whitespace. The old version
reported an illegal character.

* Added -traditional to the gcc flags because the flex definition
for free() clashes with some systems that have old header files.


88 changes: 88 additions & 0 deletions examples/manual/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#############################################################
#
# Makefile : Makefile for Flex examples.
# Author : G.T.Nicol
# Last Updated : 1993/10/05
#
# If you use bison, you may have to supply an alloca
#
#############################################################

CC = gcc -g
LEX = flex -i -I
YACC = bison -d -y
ALLOCA =

############################################################
#
# DO NOT CHANGE ANYTHING FROM HERE ON !!!!!!!!!
#
############################################################

all: expr front myname eof wc replace user_act string1\
string2 yymore numbers dates cat

expr: expr.y expr.lex
$(YACC) expr.y
$(LEX) expr.lex
$(CC) -o expr lex.yy.c y.tab.c $(ALLOCA) -ll -lm

front: front.y front.lex
$(YACC) front.y
$(LEX) front.lex
$(CC) -o front lex.yy.c y.tab.c $(ALLOCA) -ll -lm

numbers: numbers.lex
$(LEX) numbers.lex
$(CC) lex.yy.c -o numbers

dates: dates.lex
$(LEX) dates.lex
$(CC) lex.yy.c -o dates -ll

yymore: yymore.lex
$(LEX) yymore.lex
$(CC) lex.yy.c -o yymore -ll

string1: string1.lex
$(LEX) string1.lex
$(CC) lex.yy.c -o string1 -ll

string2: string2.lex
$(LEX) string2.lex
$(CC) lex.yy.c -o string2 -ll

myname: myname.lex
$(LEX) myname.lex
$(CC) lex.yy.c -o myname -ll

myname2: myname2.lex
$(LEX) myname2.lex
$(CC) lex.yy.c -o myname2 -ll

eof: eof_rules.lex
$(LEX) eof_rules.lex
$(CC) lex.yy.c -o eof -ll

wc: wc.lex
$(LEX) wc.lex
$(CC) lex.yy.c -o wc -ll

cat: cat.lex
$(LEX) cat.lex
$(CC) lex.yy.c -o cat -ll

replace: replace.lex
$(LEX) replace.lex
$(CC) lex.yy.c -o replace -ll

user_act: expr.y expr.lex
$(LEX) user_act.lex
$(CC) -o user_act lex.yy.c -ll

clean:
rm -f *.BAK *.o core *~* *.a
rm -f *.tab.h *.tab.c
rm -f myname expr lex.yy.c *.out eof wc yymore
rm -f replace front user_act string1 string2
rm -f dates numbers cat
17 changes: 17 additions & 0 deletions examples/manual/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
This directory contains the example programs from the manual, and a few
other things as well. To make all the programs, simply type "make",
and assuming you have flex and gcc, all will be well.

To build the programs individually, type

make program_name

For example:

make expr


The subdirectory FILTER contains a collection of the silly filters
that have appeared on the Internet over the years. The author of the
flex manual has included them for fun, but does not guarantee they will
work with flex, or even work at all.
45 changes: 45 additions & 0 deletions examples/manual/cat.lex
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* cat.lex: A demonstration of YY_NEW_FILE.
*/

%{
#include <stdio.h>

char **names = NULL;
int current = 1;
%}

%%
<<EOF>> {
current += 1;
if(names[current] != NULL){
yyin = fopen(names[current],"r");
if(yyin == NULL){
fprintf(stderr,"cat: unable to open %s\n",
names[current]);
yyterminate();
}
YY_NEW_FILE;
} else {
yyterminate();
}
}
%%

int main(int argc, char **argv)
{
if(argc < 2){
fprintf(stderr,"Usage: cat files....\n");
exit(1);
}
names = argv;

yyin = fopen(names[current],"r");
if(yyin == NULL){
fprintf(stderr,"cat: unable to open %s\n",
names[current]);
yyterminate();
}

yylex();
}
Loading

0 comments on commit 2eae880

Please sign in to comment.