84 % (924 Review)
There are a few changes that need to be made in the transition from project 1 to project 2.
The fact that bison generates a header file needed by flex, means bison must run before flex. The fact that this project has several levels of cascading dependencies should help you understand why having amakefileis so valuable. I have once again attached a samplemakefilefor this project to help you see how to define these dependencies. In the remaining projects, you should be able to update thismakefileas needed.
In the requirements for project 2, I have outlined the transformations that you must make to the grammar I have provided, but let me just elaborate on this topic a bit. The grammar I provided is written in EBNF, which means it includes metasymbols not in standard BNF. Also, the grammar is ambiguous and allows a wide variety of parse trees. Finally, it not exactly in the format that bison expects. So you will need to make the following changes:
The most challenging task of any parser is to detect and recover from syntax errors and provide good error messages. As you should know from having used compilers, not all error messages are as good as one might like. bison provides several methods of handling errors. Here is the approach that I recommend.
First, include theerror-verbosedirective just before your%tokendeclarations, as follows:%error-verbose
By using this directive, bison will provide descriptive error messages. When an error occurs, the functionyyerror, which you must provide, will be called. It is passed the error message. Its prototype is the following:void yyerror(const char* str);
It should just call theappendErrorfunction and pass it that message.
Once bison encounters an error, it will stop parsing unless you provide it a way to recover. To implement a recovery mechanism, you need to use theerrortoken. My suggestion is to use it in parts of the grammar that are terminated by semicolons. Because semicolons are frequently occurring tokens, they act as a good synchronizing tokens. Here is an example of how theerrortoken is used:function_header_recovery: function_header ';' | error ';' ; function_header: FUNCTION IDENTIFIER optional_parameters RETURNS type ;
In the above example, if errors occur while parsing thefunction_header, the parser will choose RHS of thefunction_header_recoveryproduction that contains theerror token instead, and discard tokens until it finds a semicolon. You should use a similar approach in other parts of the grammar that are terminated by semicolons.
I have attached the test cases that contain syntax errors that I will use to test your parser. I have also attached several program that should compile with any errors. Be sure that your parser correctly parses the programs that don't contain any errors before implementing the error checking part of this project.
UML Class Diagram.
Your answer will be ready within 2-4 hrs. Meanwhile, check out other millions of Q&As and Solutions Manual we have in our catalog.