Docsity

CS 401/501 Exam: Tokens vs Lexemes, RegEx, BNF Grammar, Parsing & Attribute Grammar, Exams of Programming Languages

Toggle sidebar
bg1
CS 401/501 EXAM #1 SAMPLE QUESTIONS
1. Explain the difference between a token and a lexeme. (5%)
2. For each regular expression below provide a brief description what kind of lexemes
are going to match with a pattern and provide some examples of lexemes (at least 3).
[pr]?ink
[a-z][a-z0-9]*
[1-9][0-9][0-9]
/\*[^\*]+\*/
([a-c][0-9])+ (10%)
3. Write a BNF grammar for a language, in which programs always start with the
keyword “print” followed by arithmetic expression (+ or -). The later is optionally followed
by a declaration part, starting with the keyword “where” and a list of pairs “id=num”
which are separated with “,”. Few examples:
print 10
print a+10 where a=1
print a+b-c where a=2, b=4, c=21 (20%)
4. Perform left factoring on the production
B ::= spring A | spring num C (5%)
5. Write a recursive descent parser for the following LL(1) grammar. Assume the
existence of a scanning procedure GET_TOKEN which sets a global string variable
TOKEN to the token type of the next token.
P ::= D begin S end
D ::= var V
V::= T V | ε
T ::= int id ; | real id ;
S ::= id := num S | ε
Draw a parse tree for the sentence: var int i; int j; begin end (25+5%)
6. Write an attribute grammar for a following grammar:
Eq ::= Sum = Sum
Sum ::= Prod AddOp Sum | Prod
Prod ::= Fact MulOp Prod | Fact
Fact ::= ( Sum ) | num
AddOp ::= + | -
MulOp ::= * | /
pf2

Partial preview of the text

Download CS 401/501 Exam: Tokens vs Lexemes, RegEx, BNF Grammar, Parsing & Attribute Grammar and more Exams Programming Languages in PDF only on Docsity!

CS 401/501 EXAM #1 SAMPLE QUESTIONS

  1. Explain the difference between a token and a lexeme. (5%)
  2. For each regular expression below provide a brief description what kind of lexemes are going to match with a pattern and provide some examples of lexemes (at least 3).

[pr]?ink [a-z][a-z0-9]* [1-9][0-9][0-9] /*[^*]+*/ ([a-c][0-9])+ (10%)

  1. Write a BNF grammar for a language, in which programs always start with the keyword “print” followed by arithmetic expression (+ or -). The later is optionally followed by a declaration part, starting with the keyword “where” and a list of pairs “id=num” which are separated with “,”. Few examples:

print 10 print a+10 where a= print a+b-c where a=2, b=4, c=21 (20%)

  1. Perform left factoring on the production

B ::= spring A | spring num C (5%)

  1. Write a recursive descent parser for the following LL(1) grammar. Assume the existence of a scanning procedure GET_TOKEN which sets a global string variable TOKEN to the token type of the next token.

P ::= D begin S end D ::= var V V::= T V | ε T ::= int id ; | real id ; S ::= id := num S | ε

Draw a parse tree for the sentence: var int i; int j; begin end (25+5%)

  1. Write an attribute grammar for a following grammar: Eq ::= Sum = Sum Sum ::= Prod AddOp Sum | Prod Prod ::= Fact MulOp Prod | Fact Fact ::= ( Sum ) | num AddOp ::= + | - MulOp ::= * | /

Use the following inherited and synthesized attributes: X I(X) S(X) Type Eq equal boolean Sum val int Prod val int Fact val Int AddOp left_op, right_op sum Int MulOp left_op, right_op prod Int

The meaning of the expression is true if the value of the left expression is equal to the value of right expression. Hence, the meaning of the sentence: 3*2 = 5+1 is true, while the meaning of the sentence: 1+1+1 = 4 is false. (30%)