Write a program in the C/C++ programming language, to understand the lexical ana
ID: 3816805 • Letter: W
Question
Write a program in the C/C++ programming language, to understand the lexical analysis phase of program compilation The first phase of compilation is called scanning or lexical analysis. This phase interprets the input program as a sequence of characters and produces a sequence of tokens, which will be used by the parser. Write a C/C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Your program should build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a list of all the tokens that appeared in the input, the number of times each token appears in the input and the class of each token. Your program should also list how many times tokens of each class appeared in the input. The token format: keyword rightarrow if | then | else | begin | end identifier rightarrow character | character identifier integer rightarrow digit | digit integer real rightarrow integer.integer special rightarrow (|) | [] | + | - | = |, |; digit rightarrow 0|1|2|3|4|5|6|7|8|9 character rightarrow a|b|c... |z|A|B|C... |Z The delimiters are space, tab. newline, and the special characters. The token classes that should be recognized are keyword, identifier, integer, real and special.Explanation / Answer
We have to develop a C scanner program file which will implement the lexical analysis phase of a compiler for the given source file in command line.
The tokens which need to be identified are: of the following classes,
keyword-if | then | else | begin | end
identifier-character | character identifier
integer-digit | digit integer
real- integer.integer
special-( | ) | [ | ] | + | - | = | , | ;
A summary report should display the token, frequency and class.
This is our main program, scanner.c
splitWords();
Next, we define our struct and enum definitions in token.h file.
We create another file symboldefine.h to store other tokens found in the source file.
We run all the files scanner.c token.h symdef.h from the command line to get the scanner running.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.