Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem 5 : WLP 4 Parser ( filename: wlp 4 parse.cc OR wlp 4 parse.rkt ) [ 3 marks, 1 release + secret, 1 secret
Problem : WLP Parser filename: wlpparse.cc OR wlpparse.rkt marks, releasesecret, secret
In this problem, you will modify your solution to the previous problem to parse WLP programs, and to produce a representation of a parse tree as output.
The input no longer contains a CFG or parsing DFA, and is no longer formatted into components. Instead, the input format is simply the output format of the WLP scanner.
The output format of the parser is also different. You should construct a parse tree for the program, in which the value stored at each node in the tree is either the production rule used to expand the nonterminal at the node if it is an internal node or the token kind and lexeme if it is a leaf node If the parse is successful, the print the node values, one per line, using a lefttoright preorder traversal of the tree:
Print the value at the root of the tree that is print the production rule or the token kind and lexeme, depending on which kind of node
Visit the children in order from left to right. For each child subtree, recursively print the values in the subtree using a lefttoright preorder traversal.
The result of this preorder traversal is a wlpi WLP Intermediate file.
In this problem, it is possible that the parse will be unsuccessful. This happens if there is no transition on the kind of the next token when the parsing 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 WLP tokens that were successfully read from the input prior to the error.
Note that BOF and EOF are not part of the input, so they do not contribute to the value of k in this problem. Note also that "one token" means a kind and lexeme pair; so each line of the input contains one token.
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.
WLP Grammar and Parsing Data
You may find the following files useful:
wlpdata.h is a C header file that provides four string constants:
WLPCFG is a CFG component for the augmented WLP grammar.
WLPTRANSITIONS is a TRANSITIONS component for the WLP SLR DFA.
WLPREDUCTIONS is a REDUCTIONS component for the WLP SLR DFA.
WLPCOMBINED is a single string containing the above three strings followed by a END line.
wlpdata.rkt is a Racket module that provides four string constants:
wlpcfg is a CFG component for the augmented WLP grammar.
wlptransitions is a TRANSITIONS component for the WLP SLR DFA.
wlpreductions is a REDUCTIONS component for the WLP SLR DFA.
wlpcombined is a single string containing the above three strings followed by a END line.
Example
For the following input:
INT int
WAIN wain
LPAREN
INT int
ID a
COMMA
INT int
ID b RPAREN
LBRACE
RETURN return
NUM
SEMI ;
RBRACE
The output should be:
start BOF procedures EOF
BOF BOF
procedures main
main INT WAIN LPAREN dcl COMMA dcl RPAREN LBRACE dcls statements RETURN expr SEMI RBRACE
INT int
WAIN wain
LPAREN
dcl type ID
type INT
INT int
ID a COMMA
dcl type ID
type INT
INT int
ID b
RPAREN
LBRACE dcls EMPTY
statements EMPTY
RETURN return
expr term
term factor
factor NUM
NUM
SEMI ;
RBRACE
EOF EOF
Testing
You can compare your output with the course tool wlpparse to see if it is correct.
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