Вы находитесь на странице: 1из 4

Carlos Gutiérrez 1

COMPILERS EXAM

1. Define a regular expression for date strings and then code it in


thestyle accepted by Jlex. Your language should cope with the
sort of thing generated by the default output fromthe unix
date command:
$ date
Fri Jan 22 11:31:22 GMT 2010
$

Regular expressions for:

Date
/^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/ mm/dd/yyyy

Time
/^([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$/ HH:MM or HH:MM:SS or
HH:MM:SS.mmm

Months

^((31(?!\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\b|t)t?|Nov)(ember)?)))|
((30|29)(?!\ Feb(ruary)?))|(29(?=\ Feb(ruary)?\ (((1[6-9]|[2- 9]\d)(0[48]|
[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|
1\d|2[0-8])\ (Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|
Aug(ust)?|Oct(ober)?|(Sep(?=\b|t)t?|Nov|Dec)(ember)?)\ ((1[6-9]|[2-
9]\d)\d{2})$

2. Think of an example where a languagewould treat some


adjacentcharacters to sometimes be thought of as a single lexical
token andat other times to be treated as more than one token.

3. What are the main roles and functions of lexical analysis and
syntax analysis?

lexical analysis syntax analysis


Read the source program, group Used token produced by lexical
the characters in lexemes, and analysis to create syntax tree.
output them as tokens.

4. Explain the main differences between dynamic binding and


static binding.
Carlos Gutiérrez 2

The main differences are: dynamic binding is to determinate the


mapping at run time and static binding is to determinate the mapping
at compile time.
5. What does it mean for a grammar to be ambiguous? Givean
example and say why such problems can often be fixed by
adding anextra non-terminal Does such an extra production
have to appear in theabstract syntax tree?

A grammar G is ambiguous if and only if there exists a sentence in L(G)


that has multiple rightmost (or leftmost) derivations. In general,
grammatical
structure is related to the underlying meaning of the sentence.
Ambiguity is
often undesirable; if the compiler cannot be sure of the meaning of a
sentence,
it cannot translate it into a single definitive code sequence.

6. Explain the main differences between dynamic binding and


static binding.

The main differences are: dynamic binding is to determinate the


mapping at run time and static binding is to determinate the mapping
at compile time.

7. What are the main differences between parse trees and syntax
trees?

The main differences are:


parse trees syntax trees
Each interior node represents a Each interior node represents an
nonterminal and many operation, and children of the
nonterminals represent node represent the operands of
programming construct. the operator.

8. Explain the main differences or relationship among token,


pattern, and lexeme.

The relationship: a pattern is a description of the lexeme forms that


token may take.
Carlos Gutiérrez 3

9. What is predictive parsing and what is semantic action?

Predictive parsing Semantic actions


Predictive parsing is a recursive- Semantic actions are program
descent parsing (a top-down fragments embedded within
method), in which the lookahead production bodies.
symbol unambiguously determines
the flow of control through the
procedure body for each
nonterminal.

10.Explain the advantages and disadvantages of NFA, when


compared to DFA.

advantages Disadvantages
NFA is suitable for the regular NFA is not suitable for the regular
expressions that are used for expressions that are frequently
several times and for the small- used.
size main-memory embedded
systems.

11.What are the main differences between NFA and DFA?

NFA DFA
Allows a symbol to label several DFA does not allow the above two
adges out of a state and allows situations.
adges with ε

12. Please use the following grammar to draw down the parse tree
of the expression 1+2*3-4
Carlos Gutiérrez 4

Solution:

13. Given an alphabet Σ = {a, b}, please use McNaughton-Yamada-


Thompson algorithm to construct the NFA of the regular
expression r = a(a|b)+bb (Hint: a(a|b)+bb = a(a|b)(a|b)*bb).

Solution:

Вам также может понравиться