Programming Exercises 201
- 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.
- 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.
- 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.
- 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.
- 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
- Design a state diagram to recognize one form of the comments of the
C-based programming languages, those that begin with / and end with /.
- Design a state diagram to recognize the floating-point literals of your
favorite programming language.
- Write and test the code to implement the state diagram of Problem 1.
- Write and test the code to implement the state diagram of Problem 2.
- 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 ).
- Convert the lexical analyzer (which is written in C) given in Section 4.2
to Java.
- Convert the recursive descent parser routines for , , and given in Section 4.4.1 to Java.
- 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.
- 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.
- Implement and test the LR parsing algorithm given in Section 4.5.3.