CS 401/501 Exam: Token vs Lexeme, Regex, BNF Grammar, Parser, Attribute Grammar, Exams of Programming Languages

Sample questions and solutions for an exam covering various topics in computer science, including the difference between tokens and lexemes, regular expressions, bnf grammar, recursive descent parser, and attribute grammar.

Typology: Exams

Pre 2010

Uploaded on 04/12/2010

koofers-user-hd0
koofers-user-hd0 🇺🇸

9 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 401/501 EXAM #1 SAMPLE QUESTIONS WITH SOLUTIONS
1. Explain the difference between a token and a lexeme. (5%)
The lowest-level syntactic units are called lexemes. Lexemes are partitioned into
groups (identifiers, keywords, operators, separators, literals). Each lexeme group
is represented by a token.
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%)
Keywords: pink, rink, ink
Identifiers, which consist of letters (only alphabetic characters) and digits with
the restrictions that it must begin with a letter: b counter i20
3-digit numbers with the restriction that first digit is not 0: 100 999 123
Multiline comment: /* abc */ /* this is comment */ /* */
Sequence of letters a-c and digits: a1 b0c3 c9c8c7b6
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%)
P ::= print E D
E ::= E + T | E - T | T
T ::= ( E ) | num | id
D ::= where F | ε
F ::= id = num G
G ::= , F | ε
4. Perform left factoring on the production
B ::= spring A | spring num C (5%)
pf3
pf4

Partial preview of the text

Download CS 401/501 Exam: Token vs Lexeme, Regex, BNF Grammar, Parser, Attribute Grammar and more Exams Programming Languages in PDF only on Docsity!

CS 401/501 EXAM #1 SAMPLE QUESTIONS WITH SOLUTIONS

  1. Explain the difference between a token and a lexeme. (5%)

The lowest-level syntactic units are called lexemes. Lexemes are partitioned into groups (identifiers, keywords, operators, separators, literals). Each lexeme group is represented by a token.

  1. 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%)

Keywords: pink, rink, ink Identifiers, which consist of letters (only alphabetic characters) and digits with the restrictions that it must begin with a letter: b counter i 3-digit numbers with the restriction that first digit is not 0: 100 999 123 Multiline comment: /* abc / / this is comment / / */ Sequence of letters a-c and digits: a1 b0c3 c9c8c7b

  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%)

P ::= print E D E ::= E + T | E - T | T T ::= ( E ) | num | id D ::= where F | ε F ::= id = num G G ::= , F | ε

  1. Perform left factoring on the production

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

B ::= spring BB BB ::= A | num C

  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%)

procedure main GET_TOKEN call P if TOKEN <> EOF then ERROR endif end

procedure P call D if TOKEN=begin then GET_TOKEN else ERROR endif call S if TOKEN=end the GET_TOKEN else ERROR endif end

procedure D if TOKEN=var then GET_TOKEN else ERROR endif call V end

procedure V if TOKEN=int || TOKEN=real then call T call V endif end

procedure T if TOKEN=int then GET_TOKEN if TOKEN=id then GET_TOKEN else ERROR endif if TOKEN=; then GET_TOKEN else ERROR endif else if TOKEN=real then GET_TOKEN if TOKEN=id then GET_TOKEN else ERROR endif

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%)

Eq ::= Sum = Sum {Eq.equal=Sum[0].val==Sum[1].val;}

Sum ::= Prod AddOp Sum {Sum[0].val=AddOp.sum; AddOp.left_op= Prod.val; AddOp.right_op=Sum[1].val;}

Sum ::= Prod {Sum.val=Prod.val;}

Prod ::= Fact MulOp Prod {Prod[0].val= MulOp.prod; MulOp.left_op=Fact.val; MulOp.right_op=Prod[1].val;}

Prod ::= Fact {Prod.val=Fact.val;}

Fact ::= ( Sum ) {Fact.val=Sum.val;}

Fact ::= num {Fact.val=Str2Int(num.lexem();}

AddOp ::= + {AddOp.sum = AddOp.left_op + AddOp.right_op;}

AddOp ::= - {AddOp.sum = AddOp.left_op - AddOp.right_op;}

MulOp ::= * {MulOp.prod = MulOp.left_op * MulOp.right_op;}

MulOp ::= / {MulOp.prod = MulOp.left_op / MulOp.right_op;}