cs160-lec11

Cs160-lec11 - CMPSC 160 Translation of Programming Languages Lecture 11 Introduction to Context Sensitive Analysis Context-Sensitive Analysis There

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

View Full Document Right Arrow Icon
1 CMPSC 160 Translation of Programming Languages Lecture 11: Introduction to Context Sensitive Analysis Context-Sensitive Analysis There is a level of correctness that is deeper than 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”
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Beyond Syntax To generate code, the compiler needs to answer many questions Is “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 “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 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)
Background image of page 2
3 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 Intermediate Representations: Overview There is more than one way to represent code as it is being generated, analyzed, and optimized. Abstract Syntax Trees (AST) Directed Acyclic Graphs (DAG) Control Flow Graphs (CFG) Static Single Assignment Form (SSA) Stack Machine Code Three Address Code high-level low-level Graphical IRs Linear IRs
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Abstract Syntax Trees: Overview A compiler must do more than to recognize whether a sentence belongs to the language of a grammar in the form of syntax parse trees (in other words, generation of parse tree top-down or bottom-
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/23/2010 for the course MATH 104b taught by Professor Ceniceros,h during the Spring '08 term at UCSB.

Page1 / 21

Cs160-lec11 - CMPSC 160 Translation of Programming Languages Lecture 11 Introduction to Context Sensitive Analysis Context-Sensitive Analysis There

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

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