Consider a programming language, (Mini-C) with the following features:
- Type : int, float, char
- Basic statements: expression statement, assignment statements, and compound statements, declaration statement.
- Control and looping statements: if statement, for statement, while statement, and switch statement. (note that nesting of these statements is also possible.)
- System functions: printf and scanf statements.
- Other: Variables, arrays (1-D and 2-D only), and function
The syntax for the above is same as in C language.
The structure of the input program is also same as a C program ignoring headers and macros.
- Lexical Analysis: Tokenizes the input program into tokens (identifiers, operators, numbers, etc). Further reports if invalid scanf or printf statements are present in the program.
A valid printf statement has one of the following structure:
- printf(“text”) where text is a non-empty string of symbols in the language alphabet.
- printf(“text”, list_of_variables) where text is a non-empty string of symbols in
the language alphabet and list_of_variables is a nonempty sequence of variables
separated by a comma (,).
Further, the number of variables in list_of_variables is must be the same as the number of %d, %c, and %f terms in text.
- Syntax Analysis: Checking whether the input program is syntactically correct or not.
- Semantic Analysis:
- Printing an Abstract Syntax Tree for the program.
- Verifying Semantic errors like type checking, undeclared variables, multiple declarations of variables, functional arguments, etc.
- Code Generation: Intermediate Code generation ( Three Address Code )
- Simulation: Simulate the program
- Compile files for all stages by running
./compile.sh
in main directory. - Run the corresponding executables for each phase with the input file
for example:./semantic_analyzer.out < input.txt
or./intermediate.out < input.txt