cs160-lec10 - CMPSC 160 Translation of Programming...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: CMPSC 160 Translation of Programming Languages Lecture 10: Introduction to Context Sensitive Analysis Context-Sensitive Analysis There is a level of correctness that is deeper than the grammar fie(a,b,c,d) int a, b, c, d; { } fee() { int f[3], g[4], h, i, j, k; char *p; call fie(h, i, ab, j, k); k = f * i + j; h = g[17]; printf(<%s,%s>.\n,p,q); p = 10; } What is wrong with this program? declared g[4], used g[17] wrong number of args to fie() ab is not an int wrong dimension on use of f undeclared variable q 10 is not a character string All of these are deeper than syntax Beyond Syntax To generate code, the compiler needs to answer many questions Is the identifier x a scalar, an array, or a function? Is x declared? Are there names that are not declared? Declared but not used? Which declaration of x does each use reference? Is the expression x * y + z type-consistent? In a[i,j,k], does a have three dimensions? Where can variable z be stored? (register, local, global, heap, static) How many arguments does fie() take? Does *p reference the result of a malloc() ? Do p and q refer to the same memory location? Is x defined before it is used? These questions are beyond what can be specified using a Context Free Grammar Beyond Syntax These questions are part of context-sensitive analysis Answers depend on values, not just tokens answers depend on attributes of tokens Questions and answers involve non-local information variable declarations, procedures Answers may involve computation How can we answer these questions? Use formal methods Context-sensitive grammars Attribute grammars (semantic rules do not have side effects) Use ad-hoc techniques Symbol tables Ad-hoc code (use semantic rules that can have side effects) Context-Sensitive Analysis Context-Sensitive Analysis Properties we want to check involve non-local information type-checking, variable declarations, procedure declarations Can be implemented as a traversal on the abstract syntax tree Can be integrated to parsing phase using ad-hoc translation schemes Formalisms such as attribute grammars can be used Ad-hoc techniques are more common than the formal techniques Source code Scanner IR Parser IR Context- Sensitive Analysis Errors token get next token Context Sensitive Analysis One way of implementing context sensitive analysis is to first construct an intermediate representation of the program While constructing the intermediate representation we remove the syntax related details since we do not need them once the parsing is done After we construct the intermediate representation, we can check context sensitive properties by traversing the intermediate representation Intermediate Representations: Overview There is more than one way to represent code as it is being...
View Full Document

Page1 / 43

cs160-lec10 - CMPSC 160 Translation of Programming...

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

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