Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

2009/2010

Uploaded on 04/12/2010

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

10 documents

1 / 4

Toggle sidebar

Related documents


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. 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%) B ::= spring BB BB ::= A | num C 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%) 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
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved