Question
Compiler Design Project - Semi-C interpreter I. Goal f of m term project The students will construct a so-called semi-C interpreter through this project. II.
Compiler Design Project - Semi-C interpreter
I. Goal f of m term project The students will construct a so-called semi-C interpreter through this project. II. The n construction f of e the interpreter In this project, the students are encouraged to build a semi-C interpreter based on the given semi-c language production rule.
1. Basic structure as an interpreter: The implemented interpreter should generate a flow graph and symbol table that reflect input source code. The flow graph contains the instructions of source code and represents the control flow of each instructions. The symbol table holds variables and their type and scope. 2. Command functions: The interpreter should provide interactive command prompt like Python or some other interpret languages. The implemented interpreter is required to include three basic commands: next command: This command executes a single or multiple line(s) of the source code. For example, next just executes current line of source code, and next 10 will execute 10 lines including current line. print command: This command prints the value contained in a variable at the moment. For example, if an integer variable a contains value 10, then print a will print 10 trace command: This command shows the history of a variable from beginning to the moment.
III. The e language grammar
Extended BNF Notation
In the lexical and syntax rules given below, BNF notation characters are written in green.
Alternatives are separated by vertical bars: i.e., 'a | b' stands for "a or b".
Square brackets indicate optionality: '[ a ]' stands for an optional a, i.e., "a | epsilon" (here, epsilon refers to the empty sequence).
Curly braces indicate repetition: '{ a }' stands for "epsilon | a | aa | aaa | ..."
1. Lexical Rules
letter | ::= | a | b | ... | z | A | B | ... | Z |
digit | ::= | 0 | 1 | ... | 9 |
id | ::= | letter { letter | digit | _ } |
intcon | ::= | digit { digit } |
charcon | ::= | 'ch' | ' ' | '\0', where ch denotes any printable ASCII character, as specified byisprint(), other than \ (backslash) and ' (single quote). |
stringcon | ::= | "{ch}", where ch denotes any printable ASCII character (as specified by isprint()) other than " (double quotes) and the newline character. |
Comments | Comments are as in C, i.e. a sequence of characters preceded by /* and followed by */, and not containing any occurrence of */. |
2. Syntax Rules
Nonterminals are shown in italics; terminals are shown in boldface, and sometimes enclosed within quotes for clarity.
2.1 Grammar Productions
prog | : | { dcl ';' | func } |
dcl | : | type var_decl { ',' var_decl } |
| | [ extern ] type id '(' parm_types ')' { ',' id '(' parm_types ')' } | |
| | [ extern ] void id '(' parm_types ')' { ',' id '(' parm_types ')' } | |
var_decl | : | id [ '[' intcon ']' ] |
type | : | char |
| | int | |
parm_types | : | void |
| | type id [ '[' ']' ] { ',' type id [ '[' ']' ] } | |
func | : | type id '(' parm_types ')' '{' { type var_decl { ',' var_decl } ';' } { stmt } '}' |
| | void id '(' parm_types ')' '{' { type var_decl { ',' var_decl } ';' } { stmt } '}' | |
stmt | : | if '(' expr ')' stmt [ else stmt ] |
| | while '(' expr ')' stmt | |
| | for '(' [ assg ] ';' [ expr ] ';' [ assg ] ')' stmt | |
| | return [ expr ] ';' | |
| | assg ';' | |
| | id '(' [expr { ',' expr } ] ')' ';' | |
| | '{' { stmt } '}' | |
| | ';' | |
assg | : | id [ '[' expr ']' ] = expr |
expr | : | '' expr |
| | '!' expr | |
| | expr binop expr | |
| | expr relop expr | |
| | expr logical_op expr | |
| | id [ '(' [expr { ',' expr } ] ')' | '[' expr ']' ] | |
| | '(' expr ')' | |
| | intcon | |
| | charcon | |
| | stringcon | |
binop | : | + |
| | ||
| | * | |
| | / | |
relop | : | == |
| | != | |
| | ||
| | ||
| | >= | |
| | > | |
logical_op | : | && |
| | || |
2.2. Operator Associativities and Precedences
The following table gives the associativities of various operators and their relative precedences. An operator with a higher precedence binds "tighter" than one with lower precedence. Precedences decrease as we go down the table.
Operator | Associativity |
!, (unary) | right to left |
*, / | left to right |
+, (binary) | left to right |
, >= | left to right |
==, != | left to right |
&& | left to right |
|| | left to right |
More details please visit
https://www2.cs.arizona.edu/~debray/Teaching/CSc453/DOCS/cminusminusspec.html
Help me with this project in Java/C/C++. (Java preferable)
Expected Result Interpreter input commands and results (In this example, the interpreter starts at the top of main function, line 12) Example input code 1 int avg(int count, int *value) 2 int i, total; 3 int sum 0; for (i = 1; i > next >next 2 >> print count 6 8 return (total / count); 9 10 11 int main(void) 12 int studentNumber, count, i, sum; 13 int mark[4]; 14 float average 15 16 count 4; 17 sum o; 18 19 for (i = 0; i 40) 1 24 25 26 27 28 0 >> next >print count 4 >next 1000 End of Program trace i i O at line 19 i 1 at line 19 i2 at line 19 i3 at line 19 printf("%f", average); S > Expected Result Interpreter input commands and results (In this example, the interpreter starts at the top of main function, line 12) Example input code 1 int avg(int count, int *value) 2 int i, total; 3 int sum 0; for (i = 1; i > next >next 2 >> print count 6 8 return (total / count); 9 10 11 int main(void) 12 int studentNumber, count, i, sum; 13 int mark[4]; 14 float average 15 16 count 4; 17 sum o; 18 19 for (i = 0; i 40) 1 24 25 26 27 28 0 >> next >print count 4 >next 1000 End of Program trace i i O at line 19 i 1 at line 19 i2 at line 19 i3 at line 19 printf("%f", average); S >
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