Lecture11

Lecture11 - Lecture 11 Stacks and Compilers Stack Is a...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 11 Stacks and Compilers
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Stack Is a collection with one accessible element - the most recent element "pushed" onto the stack. – top Supports Last-In, First-Out (LIFO) operations. – push , pop Is used often in computer systems compiler matches openers { [ ( and closers ) } ] evaluating expressions in computer languages maintain order of method calls.
Background image of page 2
Applications of Stacks in Compilers Examining programs to see if symbols balance properly. (Bracket matching) Performing “postfix” calculations. Performing “infix” to “postfix” conversions.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Balancing Symbols Compilers check your programs for syntax errors, but frequently a lack of one symbol (such as a missing { or }) may cause the compiler to spill out numerous lines of diagnostics without identifying the real error. A useful tools in this situation is a program that checks whether the symbols (),[], {} are balanced. A stack can be used to verify whether a program contains balanced brace. Push opening symbols onto a stack and see if closing symbols make sense.
Background image of page 4
Basic Algorithm 1. Make an empty stack 2. Read symbols until the end of the file (eof) a. if it's an opening symbol, push it onto the stack b. if it is a closing symbol i. if the stack is empty, report an error. ii. otherwise, pop the stack
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 19

Lecture11 - Lecture 11 Stacks and Compilers Stack Is a...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online