Context Sensitive Anal Flashcards

(10 cards)

1
Q

What is the difference between context free and context sensitive analysis?

A

Context sensitive analysis will detect if a variable is used before it is declared while context free analysis doesn’t care

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the input, precondition and output of context sensitive analysis

A

Input: A parse tree

Pre-condition: The program is syntactically valid

Output: 
if ( input is sematically valid) 
=> augmented parse tree & symbol table
else 
=> output ERROR
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

If a program is syntactically valid, what else could be wrong?

A
  • Variables or Procedures could be used before declaration or declared multiple times
  • The return value type may not match with what it should be
  • parameter list may not be well-typed
  • Operators may not be well-typed
  • Variables may be used out of scope
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do we solve variable declaration issues?

A

Create a symbol table that stores the name of the variable, location it was declared and type of the variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do we check for variable declaration issues when using a variable?

A

Check if the variable is in the symbol table, if it is then continue, else throw an error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do we check for variable declaration issues when declaring a variable?

A
  • Check to see if it is in the symbol table
  • If not, add it (name, location, type)
  • If it is in the symbol table, throw an error (multiple declarations)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When given a parse tree as input, how would you check for variable declaration issues?

A

First pass (check for multiple declarations):

  • recursively traverse the tree
  • search for dcl => TYPE ID rule
  • if the name is already in the table => throw an error
  • else, add the type and name to the symbol table

Second pass (check for undeclared variables):
- recursively traverse the parse tree
- search for rules:
factor => ID
lvalue => ID
- if the ID name is not in the symbol table yet, throw an error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do we check for scope in context sensitive analysis?

A

Build a global symbol table that keeps track of you procedure names and types

Each procedure in your global symbol table will have its own symbol table to track its paramaeters and local variables

When adding procedures to the global symbol table, check if the procedure name is in the table already, if it is throw an error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a procedure signature?

A

A procedure’s:

  • name
  • return type
  • parameter list and type
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why do types matter?

A
  • help us remember what a variable means
  • interpret its bits
  • limit how a value can be used
  • ## catch if we are using it improperly
How well did you know this?
1
Not at all
2
3
4
5
Perfectly