Semantic Analysis Flashcards

1
Q

What are the two kinds of semantic (contextual) constraints?

A

Static and Dynamic

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

A static semantic constraint can be checked by…

A

a compiler performing static analysis.

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

How can a dynamic semantic (contextual) constraint be checked?

A

It can be checked by runtime code generated by a compiler.

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

What are some examples of static context constraints?

A
  • Variables must be declared before used.
  • Variables cannot be declared more than once in the same program unit.
  • In x = y, x and y must be compatible.
  • Every function must contain at least one return statement.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Static Analysis

Constraints can be described in terms of ______ or ______.

A
  • Annotation
  • Decoration of a Syntax Tree
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the name of the compiler component that checks for constraints?

A

Semantic Analyzer

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

Do context free grammars impose contextual constraints on the sentences?

A

No.

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

Symbols in the grammar are associated with…

A

attributes.

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

Production rules in a grammar are associated with…

A

semantic rules indicating how the attribute values are computed.

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

What do attributes in a grammar do?

A

Capture correspondence between characters and mathematical values.

Example

Attribute val: The mathematical value that corresponds to the digit.

digit = 0 (character)

digit.val = 0 (digit)

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

What do conditions in a grammar do?

A

Constrain the sentences to those whose legal values are in a fixed and finite range.

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

What can semantic rules do?

A

Tells us how to compute the values of the attributes from the characters and other attributes.

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

What are synthesized attributes?

A

Attibute values that are calculated in productions where the associated symbol appears on the left hand side.

Dependencies point from child to parent in the parse tree.

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

(T/F) In inherited attributes, information always flow from child to parent in the parse tree.

A

False

Information may flow to a node from the parent OR siblings.

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

Analogous with CFG, attribute grammars are declarative.

Explain.

A

AGs define a set of valid trees, but don’t specify how to build or decorate them just like CFG define valid sentences, but not the parsing algorithm.

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

What are some characteristics of a well-defined attribute grammar?

A

They have rules that determine a unique set of values for every possible parse tree.

17
Q

What are some characteristics of a non-circular attribute grammar?

A

A grammar that never yields a parse tree with cycles in the attribute flow graph.

18
Q

What is a translation scheme?

A

An algorithm that decorates a tree in an order that respects the AGs attribute flow.

19
Q

Describe Most Obvious Scheme

A
  • Works with any well-defined AG, even circular
    • Repeated passes over tree
    • Compute attribute at each pass
    • Terminate when values no longer change
  • Oblivious
    • Doesn’t use any knowledge of structure of tree or grammar
20
Q

How do dynamic scheme translators work?

A

They tailor the evaluation order to the structure of the given parse tree.

21
Q

The fastest translations schemes tend to be _______.

A

Static

22
Q

What are S-Attributed grammars?

A

All attributes are synthesized.

23
Q

How can S-attributed AGs be computed?

A

They can be computed by a bottom-up traversal of the syntax tree.

24
Q

What is an L-Attributed grammar?

A

This occurs when A.s depends on B.t if B.t is ever passed to a semantic function that returns a value for A.s.

25
Q

What are the two rules for L-Attributed AGs?

A
  • Each synthesized attribute of a left-hand side symbol depends only on that symbol’s inherited attributes, or on attributes( synthesized or inherited) of right hand side symbols.
  • Each inherited attribute of a right side symbol depends only on inherited attributes of the left side symbol, or on attributes of symbols to its left on the right hand side.
26
Q

(T/F) L-attributed grammars can be computed during an LL(1) parse.

A

True

27
Q

How is an abstract syntax tree different from a concrete syntax tree?

A

The AST represents the structure of a sentence without unnecessary details.

28
Q

In a parser, what is an action routine?

A

Semantic function that is executed at a particular point in a parse.

29
Q

What is a visitor pattern?

A

Separate code for what to do when traversing a data structure from the code for the data structure itself.

30
Q

How can a visitor pattern be implemented?

A

Define a visitor interface that can handle each type of node in the data structure with different types of signatures.

31
Q

All nodes in an AST are subclasses of a common…

A

Abstract base class

32
Q

In an AST, if there are multiple productions for a non-terminal, how can an AST be created?

A

Make the non-terminal class abstract, and introduce a subclass for each production.

Example of one subclass below:

33
Q

In project 3 (AST) the AST nodes consisted of the values of the productions created during parsing to display them. What else can the AST nodes be used for?

A

AST nodes can be used to store fields that hold attributes used in type checking and code generation.

34
Q

Why are visitor patterns useful in compilers?

A

It can traverse the AST multiple times with different purposes (type checking, code generation).

35
Q

The abstract AST class contains an abstract visit method to visit all of the concrete AST nodes created. Do all concrete AST node classes need to include an implementation of the visit method?

A

Yes. The visit method in the AST node will return the object’s information which could be a simple task of printing the values of its fields.