Skip to content

Semantic Analyzer

Vishal Mittal edited this page Mar 9, 2020 · 4 revisions

Issues

  • TRUE PLUS FALSE -> accepted by Syntax Analyzer, so resolve here
  • Resolve Arithmetic and Boolean Expr ambiguity

Problems that can't be addressed by a CFG:

  1. Is x scalar, an array, or a function?
  2. Is x declared before it is used?
  3. Is x defined before it is used?
  4. Are any names declared but not used?
  5. Which declaration of x is being referenced?
  6. Is an expression type-consistent?
  7. What is the meaning of res= a+b*c? When a is integer, b and c are of float type.
  8. Does the dimension of a reference match the declaration?
  9. Where can x be stored? (heap, stack, ...)
  10. Does *p reference the result of a malloc()?
  11. Is an array reference in bounds?
  12. Does function foo produce a constant value?

Some more issues we need to consider

  • Consider the following program:
  1. fie(a,b,c,d)
  2. int a, b, c, d;
  3. { … }
  4. fee()
  5. {
  6. int f[3], g[10], h, i, j, k;
  7. char *p;
  8. call fie(h, i, “ab”, j, k);
  9. k = f * i + j;
  10. h = g[17];
  11. printf(“<%s,%s>.\n”,p,q);
  12. p = 10 + ‘c’;
  13. If( i = i+4)
  14. }
  • Issues:
    • declared g[10], 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