Provide details on what you need help with along with a budget and time limit. Questions are posted anonymously and can be made 100% private.
Match with a Tutor
Studypool matches you to the best tutor to help you with your question. Our tutors are highly qualified and vetted.
Get Quality Help
Your matched tutor provides personalized help according to your question details. Payment is made only after you have completed your 1-on-1 session and are satisfied with your session.
CSC1-621 Programming Languages
Programming Assignment: A Recursive Descent Parser
Design and implement a Recursive Descent Parser (RDP) for the following grammar:
,
^ |
1
0111213141516171819
Where ^is an exponentiation operator (associate to right). This grammar generates
statements of the form 2^2^3, 15, 20^2 for which the parser outputs 256 15 400.
Solution:
In order to design this RDP, the grammar should satisfy two conditions:
(1). No left recursive non-terminals (in a production of the form ^ , is
a left recursive non-terminal). To remove left recursion:
,
>
will be changed to
> ,
→
(2). No two productions having the same LHS can start with the same symbol on the
RHS (This condition is not precisely stated, but it serves the purpose). To solve this
problem, you need to factorize the productions:
^
→
will be changed to
>^
>
Applying the same techniques to the productions, you get:
→
→
Now the grammar becomes:
>
> ,
→
^
→
>
-
>
→ 0111213141516171819
1
(3) The Parser
an RDP is a set of mutually recursive procedures, one for parsing each non-terminal,
together with some supporting procedures. In an algorithmic language, the RDP of
the above grammar would be:
procedure RDPARSER;
while NOT EOF do
SUCCEEDED = TRUE;
GET_INP_LINE; (/* reads in the next input line*/)
GET_NEXT_SYMBOL; (/* returns the next input symbol */)
ELIST;
if SUCCEEDED
then SUCCESS MESSAGE
else FAILURE MESSAGE endif
endwhile
end RDPARSER;
procedure ELIST;
E;
if SUCCEEDED
then ELIST_TAIL endif
end ELIST;
procedure ELIST_TAIL;
if EOL
then
print E_Value
else if next_inp_symbol = ";'
then print E_value;
GET_NEXT_SYMBOL;
ELIST;
else SUCCEEDED = FALSE endif
endif
end ELIST_TAIL;
procedure E;
N value = 0;
N;
if SUCCEEDED
then ETAIL endif
end E;
2
procedure ETAIL;
if (NOT ((next_inp_symbol = " .) OR EOL))
then if next_inp_symbol = "^>
then GET_NEXT_SYMBOL;
E;
E value=N_value ** E_value;
else SUCCEEDED = FALSE endif
else E_value=N_value endif
end ETAIL;
procedure N;
D;
if SUCCEEDED
then N_value=N_value * 10+D_value;
NTAIL endif
end N;
procedure NTAIL;
if (NOT ((next_inp_symbol = *^°|',') OR EOL))
then N endir
end NTAIL;
procedure D;
if next_inp_symbol is a digit
then
compute D_value;
GET NEXT-SYMBOL
else SUCCEEDED = FALSE endif
end d;
This is a simple example which explains the basic idea of RDP. There are many other
issues you need to think about them. In particular, how to impose the left associativity on
the binary + and - after removing the left recursion, when to evaluate the terms so that *
and / associate to right, and how to detect an integer overflow or an uninitialized
identifier whose value is needed to evaluate an expression. You also need to find a way
with which you can specify the position of syntax error; you don't have to specify the
error type.
Note:
Keep all variables declared globally as they are, except N_value.
Declare N_value locally to procedure E.
Make N_value a pass-by-value parameter to procedure ETAIL.
Make N_value a pass-by-reference parameter to both procedures N and NTAIL.
3
Purchase answer to see full
attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Would you like to help your fellow students? Are you in need of an additional source of income? Apply to become a tutor on Studypool! Our best tutors earn over $7,500 each month!