cs 211 – project 2 · cs 211 – project 2 author: troy created date: 9/16/2018 11:14:53 pm

21
CS 211 – Project 2 Discussion of Project

Upload: others

Post on 30-May-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

CS 211 – Project 2Discussion of Project

Page 2: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

Matching Symbol Pairs

Distinct Character for Opening Symbol and Closing Symbol in each pair• Parentheses:

• Opening: ( Closing: )

• Curly Braces: • Opening: { Closing: }

• Square Brackets: • Opening: [ Closing: ]

• Angle Brackets: • Opening: < Closing: >

Page 3: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

Matching Symbol Pairs

Must have same number of Opening Symbols as Closing Symbols of each pair type

• Valid: ( ( [ ] ) )• Invalid: ( ( )

Proper Nesting of symbol pairs is required• Valid: < { } >• Invalid: < { > }

Opening Symbol must occur before paired Closing Symbol• Valid: [ < > ]• Invalid: } > < {

Page 4: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

Three Errors to Report

Error 1: Expecting a different Closing Symbol • ( ( [ ] ] )

^ expecting )

Error 2: Missing an Open Symbol (i.e. extra closing symbol)• ( { } ) ]

^ missing [

Error 3: Missing a Closing Sysmbol (i.e. expression ends to early)• < { ( ) }

^ missing >

Page 5: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

Three Errors to Report

You must specify:

• The location of the error (assume a fixed-width font for output)• The “type” of error• The proper symbol that should solve the error

If a line contains multiple errors, ONLY REPORT THE FIRST ERROR• Stop processing that line once the first error is found

Page 6: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

General Algorithm

When you encounter an opening symbol:• Push the symbol onto the stack

When you encounter a closing symbol:• Verify the top of the stack contains the matching opening symbol• Pop/remove the opening symbol from the stack

When you encounter the end of the expression• Verify the stack is empty => Expression is Balanced

Ignore any other symbols contained in the expression

Page 7: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

General Algorithm Error Checks

When you encounter an opening symbol:• No error checking done here

When you encounter a closing symbol:• If the stack is empty => Error # 2• Non-matching opening symbol on top of stack => Error # 1

When you encounter the end of the expression• If stack is not empty => Error #3

Page 8: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

Example Valid Expression

Expression: < [ a ] >

Current Symbol: At start of Expression

Symbol Type: Nothing Yet

Task: Clear stack

Stack

Page 9: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

Example Valid Expression

Expression: < [ a ] >^

Current Symbol: <

Symbol Type: Opening

Task: Push onto stack

Stack

Page 10: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

Example Valid Expression

Expression: < [ a ] >^

Current Symbol: <

Symbol Type: Opening

Task: Push onto stack<

Stack

Page 11: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

Example Valid Expression

Expression: < [ a ] >^

Current Symbol: [

Symbol Type: Opening

Task: Push onto stack<

Stack

Page 12: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

Example Valid Expression

Expression: < [ a ] >^

Current Symbol: [

Symbol Type: Opening

Task: Push onto stack [

<

Stack

Page 13: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

Example Valid Expression

Expression: < [ a ] >^

Current Symbol: a

Symbol Type: None

Task: ignore [

<

Stack

Page 14: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

Example Valid Expression

Expression: < [ a ] >^

Current Symbol: ]

Symbol Type: Closing

Task: Verify top of stack has matchingopening symbol, if so pop the stack

[

<

Stack

Page 15: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

Example Valid Expression

Expression: < [ a ] >^

Current Symbol: ]

Symbol Type: Closing

Task: Verify top of stack has matchingopening symbol, if so pop the stack

[ matches ]

[

<

Stack

Page 16: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

Example Valid Expression

Expression: < [ a ] >^

Current Symbol: ]

Symbol Type: Closing

Task: Verify top of stack has matchingopening symbol, if so pop the stack <

Stack

Page 17: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

Example Valid Expression

Expression: < [ a ] >^

Current Symbol: >

Symbol Type: Closing

Task: Verify top of stack has matchingopening symbol, if so pop the stack <

Stack

Page 18: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

Example Valid Expression

Expression: < [ a ] >^

Current Symbol: >

Symbol Type: Closing

Task: Verify top of stack has matchingopening symbol, if so pop the stack

< matches >

<

Stack

Page 19: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

Example Valid Expression

Expression: < [ a ] >^

Current Symbol: >

Symbol Type: Closing

Task: Verify top of stack has matchingopening symbol, if so pop the stack

Stack

Page 20: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

Example Valid Expression

Expression: < [ a ] >^

Current Symbol: At End of Expression

Symbol Type: End of Expression

Task: Verify stack is Empty

Stack

Page 21: CS 211 – Project 2 · CS 211 – Project 2 Author: troy Created Date: 9/16/2018 11:14:53 PM

Example Valid Expression

Expression: < [ a ] >^

Current Symbol: At End of Expression

Symbol Type: End of Expression

Task: Verify stack is EmptyStack is EMPTY, so Expression is Balanced!

Stack