Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem 4 : SLR ( 1 ) Parsing ( filename: slr . cc OR slr . rkt ) [ 2 marks, 1 release + secret
Problem : SLR Parsing filename: slrcc OR slrrkt marks, releasesecret
In this problem, you will modify your solution to the previous problem so that it determines whether to shift or reduce using an SLR DFA.
The input to your program will consist of a CFG component, an INPUT component, a TRANSITIONS component, and a REDUCTIONS component, terminated by a END line. The latter two components encode the transitions and reducible items of an SLR DFA.
The CFG and the inputtobeparsed will be augmented.
This means:
The first rule in the CFG component will have the form:
augmentedstartsymbol BOF originalstartsymbol EOF
The augmentedstartsymbol and the originalstartsymbol may differ between test grammars, but all test grammars will use the symbols BOF and EOF to mark the beginning and end of input.
The augmentedstartsymbol will never appear on the right hand side of a CFG rule.
The input sequence will start with BOF and end with EOF.
Your program should work the same way as in the previous problem, except that it uses the SLR DFA to decide whether to shift or reduce at each parsing step. Your program should perform the "print" action once at the start before shifting or reducing and also after every shift or reduce. In other words, your output should be a full trace of the parse.
Additionally, in this problem, it is possible that the parse will be unsuccessful. This happens if there is no transition on the next symbol of input when the DFA tells you to shift. If this occurs, output a single line consisting of the string ERROR at k terminated with a line feed to standard error, where k is one greater than the number of terminal symbols that were successfully shifted prior to the error.
Unlike in some earlier assignments, where your error message simply had to contain "ERROR", in this problem your error message must exactly match the required message. Extraneous output such as debugging output on standard error will cause you to fail some Marmoset tests.
Example
For the following input:
CFG
S BOF expr EOF
expr expr term
expr term
term id
term expr
INPUT
BOF id id id id EOF
TRANSITIONS
BOF
id
term
id
term
expr
id
term
expr
EOF
REDUCTIONS
EOF
EOF
ACCEPT
EOF
EOF
END
The output should be:
BOF id id id id EOF
BOF id id id id EOF
BOF id id id id EOF
BOF term id id id EOF
BOF expr id id id EOF
BOF expr id id id EOF
BOF expr id id id EOF
BOF expr id id id EOF
BOF expr term id id EOF
BOF expr expr id id EOF
BOF expr expr id id EOF
BOF expr expr id id EOF
BOF expr expr term id EOF
BOF expr expr id EOF
BOF expr expr id EOF
BOF expr term id EOF
BOF expr id EOF
BOF expr id EOF
BOF expr id EOF
BOF expr term EOF
BOF expr EOF
BOF expr EOF
S
If the INPUT component was replaced with the following:
INPUT
BOF id id id EOF
Then your program should print ERROR at to standard error. This signifies that the error occurred after symbols were read successfully and therefore the th symbol is the one that caused the error. It does not matter what is printed to standard output; for example, it is fine if you produce a partial trace of the parse before the error.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started