-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreading_algorithm_psuedocode.txt
More file actions
64 lines (54 loc) · 2.31 KB
/
reading_algorithm_psuedocode.txt
File metadata and controls
64 lines (54 loc) · 2.31 KB
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
Our command parsing approach:
if encounters new command:
put on command_stack
if encounters new operator:
if operator stack == NULL:
push new operator to operator_stack
else:
if precedence(new operator) > precedence(operator_stack.top):
push new operator to operator_stack
else:
while(operator_stack.top !='(' && precedence(new operator) <= precedence(operator_stack.top)):
operator = operator_stack.pop
second_command = command_stack.pop
first_command = command_stack.pop
new_command = combine(first_command, second_command, new operator)
push new_command to command_stack
top_operator = operator_stack.peek
if top_operator == NULL:
break;
push new operator to operator_stack
//process whater is left in the stacks...
//TA NOTE: End condition: your algorithm will be done when:
1) The operator stack is empty
2) There is only one (fully complete) Node in the command stack (this node is your result)
Some important discussion notes:
pipeline precedence is higher than semicolon
a;b|c|d;e
1) b|c
2) (b|c)|d
3)a((b|c)|d)
4)a;b|c|d;e
Rules: for syntax parsing
a)A single new line is treated as a semicolon
b) 2 or more new lines result in two different command trees (nodes in linked list)
c)Special case of incomplete command where the right operand is missing: 2 or more '\n' have no effect
a && \n
\n
\n
b
-->a&&b (This is an error)
TUANS NEW ALGO (gonna implement this one)
a)If a simple command, push to a command stack
b)If it is a "(", push it onto an operator-stack
c)If it is an Operator and operator stack is empty
1)push the operator onto the operator stack
d)If it is an operator and the operator stack is NOT empty
1)Pop all operators with greator or equal precedence off the operator stack
For each popped off Operator, Pop 2 commands off command stack
combine them into a new command
2)Stop when you reach an operator with lower precedence or a "("
3)Push the operator onto the stack
e)If encounter ")", pop operators off the stack (for each operator, pop two commands, combine, push back on command stack) until you find a matching "(". then create a subshell command by popping out 1 command from command stack.
f)Advance to next word (simple command, and, or) go to a)
g)When all words are gone, pop each operator and combine them with 2 commands similar to d)