summaryrefslogtreecommitdiff
path: root/graphics/tree/unpar.l
blob: 3bcbdfd7b00284a592c7db91958f57c828c8f565 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
%{
/*
 * unpar.l: substitute indentation for parenthesization.
 * Version 0.1
 * (unpar recognizes some \tree definitions as correct which tree
 *  rejects.)
 * 			-- Greg Lee, lee@uhccux.uhcc.hawaii.edu, 6/24/90
 */
#define TRUE 1
#define FALSE 0
int tex_opt = FALSE;
int level = 0;
int havename = 0;
int wait = 0;
int i;
%}

%s T X C

%%

^".tr" {
	printf("\\tree");
	havename = 1;
	BEGIN(T);
}

\\tree[ \t]*(-([tuvLTOIFER]+|[bg][0-9]+)[ \t]*)* {
	ECHO;
	havename = 1;
	if (tex_opt) BEGIN(X);
	else BEGIN(T);
}

<T,X>^[ \t]+	;
<T,X>[ \t]+$	;

<T,X>\n	;

<T,X>\\\( {
	havename = 1;
	ECHO;
}
<T,X>[^ \t\n\(\)]/\( {
	havename = 1;
	ECHO;
	putchar(' ');
}
<T,X>"("([^\)\(]|\\\)|\\\()+"("([^\)\(]|\\\)|\\\()+")"([^\)\(]|\\\)|\\\()*")" {
	wait = 2;
	REJECT;
}
<T,X>"("[ \t\n]* {
	level++;
	if (wait == 1) printf(" - ");
	else {
		if (!havename) printf("\\Z\n");
		else putchar('\n');
		indent();
	}
	havename = 0;
	if (wait) wait--;
}
<T,X>")"[^\)\(]+ {
	if (level == 1) {REJECT;}
	else level--;
}
<T,X>")" {
	level--;
	if (!level) {
		printf("\n");
		BEGIN(0);
	}
}
<X>\$_[0-9ijkxyz]\$ {
	havename = 1;
	printf("[%c]", yytext[2]);
}
<X>\$\\overline\{\\rm\ [NAVP]\}\$ {
	havename = 1;
	printf(" %c'", yytext[15]);
}
<T,X>\\[%\(\)] {
	havename = 1;
	ECHO;
}
<T,X>% {
	BEGIN(C);
}
<C>\n {
	if (tex_opt) BEGIN(X);
	else BEGIN(T);
}
<C>. ;
<T,X>. {
	havename = 1;
	ECHO;
}

%%

indent()
{	int i;

	for (i = 0; i < 2*(level-1); i++) putchar(' ');
}

extern char *optarg;		/* from getopt */
extern int  optind;

main(argc, argv)
int     argc;
char   *argv[];
{	int c;
	char *progname = NULL, *basename();

	progname = basename (argv[0]);
	while ((c = getopt (argc, argv, "ht")) != EOF)
		switch (c) {
			case 't': tex_opt = TRUE; break;
			case 'h':
	    		default: 
		   fprintf(stderr, "Usage: %s [options] [files]\n", progname);
		   fprintf(stderr, "options = -t\t(remove TeX code)\n");
		   fprintf(stderr, "          -h\t(print this information)\n");
		   exit(1);
		}

	if (optind >= argc) {
		(void) yylex ();
	}
	else for (; (optind < argc); optind++) {
		if (yyin == NULL) yyin = stdin;
		if (freopen (argv[optind], "r", stdin) != NULL) {
#ifdef FLEX_SCANNER	
			/* to get flex to look at > 1 file */
			yy_init = 1;
#endif
			(void) yylex ();
		}
		else {
			(void) fprintf (stderr,
			    "Couldn't open file: %s\n", argv[optind]);
			exit (1);
		}
	}
}


char   *basename (s)
char   *s;
{
	char   *p, *strrchr();

	if (p = strrchr(s, '/'))
		return(++p);
	else return(s);
}