I need some help making this code more simple.  2) Algorithm tokenList = "{}()[]" initialize stack to empty Get file name from user: fileName Open the file (fileName) read line from fileName into strLine while not EOF (end of file) find tokenList items in strLine for each token in strLine if token = "[" then push "]" into stack else if token = "(" then push ")" into stack else if token = "{" then push "}" into stack else if token = "]" OR token = ")" OR token = "}" then pop stack into poppedToken if poppedToken not equal token print "Incorrect Grouping Pairs" exit end if end if next token end while if stack is empty print "Correct Grouping Pairs" else print "Incorrect Grouping Pairs" end if

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

I need some help making this code more simple. 

2) Algorithm
tokenList = "{}()[]"
initialize stack to empty
Get file name from user: fileName
Open the file (fileName)
read line from fileName into strLine
while not EOF (end of file)
find tokenList items in strLine

for each token in strLine
if token = "[" then
push "]" into stack
else if token = "(" then
push ")" into stack
else if token = "{" then
push "}" into stack
else if token = "]" OR token = ")" OR token = "}" then
pop stack into poppedToken
if poppedToken not equal token
print "Incorrect Grouping Pairs"
exit
end if
end if
next token
end while
if stack is empty
print "Correct Grouping Pairs"
else
print "Incorrect Grouping Pairs"
end if

6) Let's pop one more time
poppeditem = "]"
Design Strategy
We have 3 types of tokens we're looking for:
1) Parenthesis ( )
2) Brackets []
3) Curly Brackets {}
There's an open and closed version of each token.
Look at this line: x = [a, b(1,2,3], c] This is an unmatched pair, because the closed bracket after the
number 3, should have been a closed parenthesis, to complete the open parenthesis before the number
1. Here's what the correct version looks like: x = [a, b(1,2,3), c]
A stack is a perfect data structure to keep track of the tokens in the order they were encountered. For
the tokens to be matched, there always has to be a closed token the directly precedes the same version
of an open token. Anytime we come across a closed token we can just pop off the latest item in a stack
to see if it matches.
Algorithm
Let's read a file a line at a time and scan for tokens on each line. As we encounter an open token, push
it into the stack. When we encounter a closed token we pop an item from the stack and see if it
matches the open token. If it doesn't we print a message saying "Unmatched Pair". When we finish
reading the file we check to see if the queue is empty. If it is "Success" or else it's an unmatched pair.
You can use the partial algorithm below and fill in the rest of it if you'd like. Iľ'm not expecting you to
provide details for managing push and pop functions. You can assume that there's a pop and push
function built in that you can utilize.
Transcribed Image Text:6) Let's pop one more time poppeditem = "]" Design Strategy We have 3 types of tokens we're looking for: 1) Parenthesis ( ) 2) Brackets [] 3) Curly Brackets {} There's an open and closed version of each token. Look at this line: x = [a, b(1,2,3], c] This is an unmatched pair, because the closed bracket after the number 3, should have been a closed parenthesis, to complete the open parenthesis before the number 1. Here's what the correct version looks like: x = [a, b(1,2,3), c] A stack is a perfect data structure to keep track of the tokens in the order they were encountered. For the tokens to be matched, there always has to be a closed token the directly precedes the same version of an open token. Anytime we come across a closed token we can just pop off the latest item in a stack to see if it matches. Algorithm Let's read a file a line at a time and scan for tokens on each line. As we encounter an open token, push it into the stack. When we encounter a closed token we pop an item from the stack and see if it matches the open token. If it doesn't we print a message saying "Unmatched Pair". When we finish reading the file we check to see if the queue is empty. If it is "Success" or else it's an unmatched pair. You can use the partial algorithm below and fill in the rest of it if you'd like. Iľ'm not expecting you to provide details for managing push and pop functions. You can assume that there's a pop and push function built in that you can utilize.
Here is a stack data structure called tokenStack. Initially it is empty. Here is a sequence of tokens that
we are going to push into tokenStack ")", "I", "I", ")"
tokenStack (Empty state)
1) Now let's push the first item in the sequence a closed parenthesis ")" into the stack. The latest item
pushed always goes into position 0. A stack is Last In First Out (LIFO)
|0 )
2) Next item in sequence is a closed bracket "J". Push it into tokenStack. The closed parenthesis that
was in position 0 gets pushed down into position 1 and the closed bracket moves into position 0.
tokenStack looks like this:
|1 |)
3) Let's push another closed bracket "J". Everything in tokenStack gets pushed down 1 position and
the closed bracket gets pushed into position 0.
1
4) One more push. This time another closed parenthesis “)". tokenStack looks like this:
2
| 1
3
5) Now let's pop an item from tokenStack. Popping an item removes the latest element that was
pushed (position 0) into a variable (let's call it poppedltem) and reconfigures the stack so that
element is gone. Here's what tokenStack looks like after popping:
|0 1
poppeditem = ")"
Transcribed Image Text:Here is a stack data structure called tokenStack. Initially it is empty. Here is a sequence of tokens that we are going to push into tokenStack ")", "I", "I", ")" tokenStack (Empty state) 1) Now let's push the first item in the sequence a closed parenthesis ")" into the stack. The latest item pushed always goes into position 0. A stack is Last In First Out (LIFO) |0 ) 2) Next item in sequence is a closed bracket "J". Push it into tokenStack. The closed parenthesis that was in position 0 gets pushed down into position 1 and the closed bracket moves into position 0. tokenStack looks like this: |1 |) 3) Let's push another closed bracket "J". Everything in tokenStack gets pushed down 1 position and the closed bracket gets pushed into position 0. 1 4) One more push. This time another closed parenthesis “)". tokenStack looks like this: 2 | 1 3 5) Now let's pop an item from tokenStack. Popping an item removes the latest element that was pushed (position 0) into a variable (let's call it poppedltem) and reconfigures the stack so that element is gone. Here's what tokenStack looks like after popping: |0 1 poppeditem = ")"
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY