Docsity

Question with Solution for Exam 1 - Programming Languages | CS 401, Exams of Programming Languages

Toggle sidebar
bg1
CS 405/505 EXAM #1 SAMPLE QUESTIONS (SOLUTION)
1. The string representation of a token is called a(n) lexeme.
2. True or False. Context-sensitive syntax refers to that part of a context-free grammar that
requires nondeterminism to be parsed.
False. Context-sensitive syntax is the same as static semantics,
which means the correctness of the syntax with respect to context,
such as the symbol table, e.g. type compatibility of left and right
sides of an assignment statement.
3. List the phases of a compiler. Discuss in detail what each phase does and how the phases
interact with each other. (You may draw a diagram to assist in this discussion if you wish.)
Explain how an interpreter is different from a compiler.
Described in section 1.71-1.7.3 of the textbook.
4. A C++ class declaration is described in Extended Backus-Naur form as:
class-specifier ::=
class identifier [ :identifier ] {member-declaration {member-declaration} }
member-declaration ::= identifier identifier function-definition
|int identifier function-definition
Note that this syntax contains terminal symbols {and }which are different from the extended
BNF meta-symbols {and }, and distinguished by underlining. Write an equivalent syntax
graph and a recursive descent parser for this construction. Assume the existence of production
rules for function-definition and corresponding parsing procedure FUNCTION DEFINITION, and
a scanning procedure GET TOKEN to set a global string variable TOKEN to the token type of the
next token. Furthermore, assume that TOKEN is already set to the first token when parsing
begins.
The syntax graph is:
pf3
pf4
pf5

Partial preview of the text

Download Question with Solution for Exam 1 - Programming Languages | CS 401 and more Exams Programming Languages in PDF only on Docsity!

CS 405/505 EXAM #1 SAMPLE QUESTIONS (SOLUTION)

  1. The string representation of a token is called a(n) lexeme.
  2. True or False. Context-sensitive syntax refers to that part of a context-free grammar that requires nondeterminism to be parsed.

False. Context-sensitive syntax is the same as static semantics, which means the correctness of the syntax with respect to context, such as the symbol table, e.g. type compatibility of left and right sides of an assignment statement.

  1. List the phases of a compiler. Discuss in detail what each phase does and how the phases interact with each other. (You may draw a diagram to assist in this discussion if you wish.) Explain how an interpreter is different from a compiler. Described in section 1.71-1.7.3 of the textbook.
  2. A C++ class declaration is described in Extended Backus-Naur form as:

class-specifier ::= class identifier [ : identifier ] { member-declaration {member-declaration} } member-declaration ::= identifier identifier function-definition | int identifier function-definition

Note that this syntax contains terminal symbols { and } which are different from the extended BNF meta-symbols { and }, and distinguished by underlining. Write an equivalent syntax graph and a recursive descent parser for this construction. Assume the existence of production rules for function-definition and corresponding parsing procedure FUNCTION DEFINITION, and a scanning procedure GET TOKEN to set a global string variable TOKEN to the token type of the next token. Furthermore, assume that TOKEN is already set to the first token when parsing begins.

The syntax graph is:

class identifier : identifier {

member-

declaration }

class-specifier

member-

declaration

identifier identifier

int identifier

f unction-

definition

f unction-

definition

  1. Consider the attribute grammar below:

::= <a-n> <b-n> <c-n> <b-n>. count โ† <a-n>. count <c-n>. count โ† <a-n>. count <a-n> ::= a <a-n>. count โ† 1 | <a-n> 1 a <a-n>. count โ† <a-n> 1. count + 1 <b-n> ::= b Condition: <b-n>. count = 1 | <b-n> 1 b <b-n> 1. count โ† <b-n>. count โˆ’ 1 <c-n> ::= c Condition: <c-n>. count = 1 | <c-n> 1 c <c-n> 1. count โ† <c-n>. count โˆ’ 1

(a) Indicate which attributes are inherited, which are synthesized, and which are intrinsic.

inherited - count of <b-n> and <c-n> synthesized - count of <a-n> intrinsic - none

(b) Draw the attributed parse tree for the string โ€œaaabbbcccโ€, showing clearly the order of evaluation of all attributes.

/ |
/ |
/ |
/ |
/ |
/ |
/ |
/ |
/ |
/ |
/ |
/ |
/ |
/ |
/ |
/ |
/ |
/ |
/ |
/ |
<a-n> <b-n> <c-n>

  1. count: 3 4) count: 3 5) count: 3 /\ /\ /
    / \ / \ /
    / \ / \ /
    / \ / \ /
    <a-n> a <b-n> b <c-n> c
  2. count: 2 6) count: 2 9) count: 2 /\ /\ /
    / \ / \ /
    / \ / \ /
    / \ / \ /
    <a-n> a <b-n> b <c-n> c
  3. count: 1 7) count: 1 10) count: 1 | 8) Condition: true 11) Condition: true | | | | | | a b c