Question: Problem 1 (5 pts): Given the following grammar, show a parse tree and a leftmost derivation for A = ( A + B ) *
Problem 1 (5 pts):
Given the following grammar, show a parse tree and a leftmost derivation for A = ( A + B ) * C assignment statement.
=
|
|
(
|
Problem 2 (15 pts):
The first phase of compilation is called lexical analysis or scanner. This phase interprets the input program source code as a sequence of characters and produces a sequence of tokens, which will be used by the syntax analyzer or parser.
Create a state machine diagram (state diagram) that clearly show all the needed states, including the final/end state, and the transitions between states. Make sure our handwriting is legible.
Write a Python or C++ program that implements a simple scanner for a source file listed below. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table 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.
Sample token format: (you may change this accordingly)
keyword -> if | else | void | break | while |true
predirective -> #include
identifier -> character | character identifier
integer -> digit | digit integer
real -> integer.integer
special -> ( | ) | [ | ] | + | - | = | , | ;| { | } digit -> 0|1|2|3|4|5|6|7|8|9
character -> a|b|c ... |z|A|B|C ... |Z
- The delimiters can be space, tab, newline, and special characters. You only need to use one delimiter in your implementation.
- Case is not used to distinguish tokens.
- Choice at least 5 token classes or use the sample ones above.
- Your program output can be ordered in any way you want.
Sample Source File:
int main()
{
int a,b,c;
flag = true;
while (flag)
{
c = a+b;
if (c > 100) {break;}
}
return 0;
}
You need to submit 3 files to Blackboard for this assignment: your source code, your input file, and a .docx file showing your state machine and the screenshot of your output
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
