


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 4
This page cannot be seen from the preview
Don't miss anything!



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.
[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
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 | ε
B ::= spring A | spring num C (5%)
B ::= spring BB BB ::= A | num C
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;}