Question: General This assignment involves implementing a scanner and parser for ! using a scanner generator such as lex or flex and a parser generator as
General
This assignment involves implementing a scanner and parser for
using a scanner generator such as lex or flex and a parser generator as such as yacc or bison for this purpose.
For this assignment, your code should deal with only the lexical and syntax rules of In other words, anything that requires semantic informationie information involving declarationsshould be ignored.
The Scanner
General
The scanner should be implemented as a function that returns, each time it is called, either a positive integer indicating what kind of token was found on the input stream, or the value end of file" indicating that no further input is available. Note that keywords cannot be used as identifiers.
The values of different kinds of tokens should be defined as macros to simplify the interface between the scanner and parser. For this purpose, it is simplest to define singlecharacter tokens such as and ; to have the value of the corresponding character constant, eg the value of a "leftparenthesis" token will be that of the character constant The simplest way to do this is to use yacc d to generate a file tab.h that contains the macro definitions, then #include this file into the scanner. Your make file will have to be set up carefully to make this work right.
Comments and Whitespace
Comments and whitespace are to be skipped silently. It is an error to encounter an endoffile inside a comment.
Errors
The simplest way to deal with lexical errors is to let the parser worry about them. This can be done by simply returning the value of any unrecognized character to the parser.
The Parser
General
You should transform your grammar, as necessary, to eliminate conflicts. The "dangling else" shiftreduce conflict will be tolerated, as will shiftreduce conflicts between error productions, but you will be penalized for any other conflicts. If you encounter conflicts, you may consult the file youtput generated by yacc invoked with the v option for more information.
Errors
You are to implement error handling and recovery for syntax errors. This does not include errors involving semantic checking ie anything that demands information from declarations which will be dealt with in the next assignment.
Your program will be expected to deal with errors in a "reasonable" way. Error messages should be specific and should contain enough information with at least a line number to allow the user to locate syntax problems. Error recovery should allow your parser to recover gracefully and continue processing the input even after syntax errors are encountered.
Invoking Your Program
Your program will be called compile. It will read from stdin and send all output to stdout. Error messages will be sent to stderr. Eg:
cat foo.c compile
At this point, your program will act simply as a syntax checker: syntactically correct input will be accepted silently, while syntax errors will give rise to error messages that will be reported to stderr.
Sometimes students find it difficult to resist the urge to "improve" on this spec by having their compilers read from a file rather than from stdin. Please resist this urge or if you can't, be prepared tosacrifice points off your score I know you can write code to do file IO; show me that you can read, understand, and follow a spec.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
