|
|
1.1 root 1: prologue {
2: #include <stdio.h>
3: #include <ctype.h>
4: typedef struct node * NODEPTR;
5: #define COST int
6: #define INFINITY 1000000
7: #define DEFAULT_COST 0
8: #define COSTLESS(x,y) ((x)<(y))
9:
10: NODEPTR Root;
11: struct node {
12: char op; /* '\0' if node is a leaf */
13: NODEPTR left, right;
14: char id;
15: int mark;
16: };
17:
18: }; /* end of prologue */
19:
20: node nOp nIdent;
21: label lExpr;
22:
23: lExpr: nIdent
24: = { putchar($$->id); };
25:
26: lExpr: nOp(lExpr,lExpr)
27: { TOPDOWN; }
28: = {
29: putchar($$->op);
30: tDO($%1$); tDO($%2$);
31: };
32:
33: insert {
34:
35: mtValue(root)
36: NODEPTR root;
37: {
38: if(root==NULL)
39: return(0);
40: if(root->op==0)
41: return(nIdent);
42: else
43: return(nOp);
44: }
45:
46: NODEPTR
47: mtGetNodes(r,n)
48: NODEPTR r;
49: int n;
50: {
51: if(r==NULL)
52: if(n==1)
53: return(Root);
54: else
55: return(NULL);
56: if(n==1)
57: return(r->left);
58: else if(n==2)
59: return(r->right);
60: else return(NULL);
61: }
62:
63: mtSetNodes(r,n,c)
64: NODEPTR r,c;
65: int n;
66: {
67: if(r==NULL && n==1) {
68: Root = c;
69: return;
70: }
71: if(n==1)
72: r->left = c;
73: else if(n==2)
74: r->right = c;
75: }
76:
77: syntax_error()
78: {
79: printf("syntax error\n");
80: exit(1);
81: }
82:
83: /* allocated a node */
84: NODEPTR
85: getnode(op,id)
86: char op,id;
87: {
88: NODEPTR n;
89: n = (NODEPTR) malloc(sizeof(struct node));
90: n->op = op;
91: n->left = n->right = NULL;
92: n->id = id;
93: return(n);
94: }
95:
96: NODEPTR
97: gettree()
98: {
99: char c;
100: NODEPTR n = NULL;
101:
102: c = getchar();
103: if(c==EOF)
104: syntax_error();
105:
106: n = getnode(0,0);
107:
108: /* build node for left operand */
109: if(c=='(')
110: n->left = gettree();
111: else
112: n->left = getnode(0,c);
113:
114: c = getchar();
115: if(strchr("+-*/",c)==NULL)
116: syntax_error();
117: n->op = c;
118:
119: c = getchar();
120: /* build node for right operand */
121: if(c=='(')
122: n->right = gettree();
123: else
124: n->right = getnode(0,c);
125:
126: c = getchar();
127: if(c!=')')
128: syntax_error();
129:
130: return(n);
131: }
132:
133: main()
134: {
135: char c;
136: _matchinit(); /* initialize the matcher */
137:
138: /* get the initial ( */
139: c = getchar();
140: if(c!='(')
141: syntax_error();
142:
143: Root = gettree();
144: _match();
145: putchar('\n');
146: }
147: printTree(x)
148: {}
149:
150:
151: }; /* of insert */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.