Compilers_Exam

Embed Size (px)

Citation preview

  • 8/7/2019 Compilers_Exam

    1/4

    1Carlos Gutirrez

    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 orHH: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 andsyntax analysis?

    lexical analysis syntax analysisRead the source program, groupthe characters in lexemes, andoutput them as tokens.

    Used token produced by lexicalanalysis to create syntax tree.

    4. Explain the main differences between dynamic binding andstatic binding.

  • 8/7/2019 Compilers_Exam

    2/4

    2Carlos Gutirrez

    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 asentence,

    it cannot translate it into a single definitive code sequence.

    6. Explain the main differences between dynamic binding andstatic 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 syntaxtrees?

    The main differences are:parse trees syntax trees

    Each interior node represents anonterminal and manynonterminals representprogramming construct.

    Each interior node represents anoperation, and children of thenode represent the operands ofthe operator.

    8. Explain the main differences or relationship among token,pattern, and lexeme.

    The relationship: a pattern is a description of the lexeme forms thattoken may take.

  • 8/7/2019 Compilers_Exam

    3/4

    3Carlos Gutirrez

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

    Predictive parsing Semantic actionsPredictive parsing is a recursive-descent parsing (a top-down

    method), in which the lookaheadsymbol unambiguously determinesthe flow of control through theprocedure body for eachnonterminal.

    Semantic actions are programfragments embedded within

    production bodies.

    10.Explain the advantages and disadvantages of NFA, when

    compared to DFA.

    advantages DisadvantagesNFA is suitable for the regularexpressions that are used forseveral times and for the small-size main-memory embeddedsystems.

    NFA is not suitable for the regularexpressions that are frequentlyused.

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

    NFA DFAAllows a symbol to label severaladges out of a state and allowsadges with

    DFA does not allow the above twosituations.

    12.Please use the following grammar to draw down the parse tree

    of the expression 1+2*3-4

  • 8/7/2019 Compilers_Exam

    4/4

    4Carlos Gutirrez

    Solution:

    13. Given an alphabet = {a, b}, please use McNaughton-Yamada-Thompson algorithm to construct the NFA of the regularexpression r = a(a|b)+bb (Hint: a(a|b)+bb = a(a|b)(a|b)*bb).

    Solution: