Concepts of Programming Languages

(Sean Pound) #1
Programming Exercises 201


  1. Show a complete parse, including the parse stack contents, input string,
    and action for the string id * (id + id), using the grammar and parse
    table in Section 4.5.3.

  2. Show a complete parse, including the parse stack contents, input string,
    and action for the string (id + id) * id, using the grammar and parse
    table in Section 4.5.3.

  3. Write an EBNF rule that describes the while statement of Java or C++.
    Write the recursive-descent subprogram in Java or C++ for this rule.

  4. Write an EBNF rule that describes the for statement of Java or C++.
    Write the recursive-descent subprogram in Java or C++ for this rule.

  5. Get the algorithm to remove the indirect left recursion from a
    grammar from Aho et al. (2006). Use this algorithm to remove all
    left recursion from the following grammar:
    S→Aa  Bb A→Aa  Abc  c  Sb B→bb


PROGRAMMING EXERCISES



  1. Design a state diagram to recognize one form of the comments of the
    C-based programming languages, those that begin with / and end with /.

  2. Design a state diagram to recognize the floating-point literals of your
    favorite programming language.

  3. Write and test the code to implement the state diagram of Problem 1.

  4. Write and test the code to implement the state diagram of Problem 2.

  5. Modify the lexical analyzer given in Section 4.2 to recognize the follow-
    ing list of reserved words and return their respective token codes:
    for (FOR_CODE, 30 ), if (IF_CODE, 31 ), else (ELSE_CODE, 32 ), while
    (WHILE_CODE, 33 ), do (DO_CODE, 34 ), int (INT_CODE, 35 ), float
    (FLOAT_CODE, 36 ), switch (SWITCH_CODE, 37 ).

  6. Convert the lexical analyzer (which is written in C) given in Section 4.2
    to Java.

  7. Convert the recursive descent parser routines for , , and given in Section 4.4.1 to Java.

  8. For those rules that pass the test in Problem 1, write a recursive-descent
    parsing subprogram that parses the language generated by the rules.
    Assume you have a lexical analyzer named lex and an error-handling sub-
    program named error, which is called whenever a syntax error is detected.

  9. For those rules that pass the test in Problem 2, write a recursive-descent
    parsing subprogram that parses the language generated by the rules.
    Assume you have a lexical analyzer named lex and an error-handling sub-
    program named error, which is called whenever a syntax error is detected.

  10. Implement and test the LR parsing algorithm given in Section 4.5.3.

Free download pdf