https://github.com/s1998/compiler
https://github.com/s1998/compiler
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/s1998/compiler
- Owner: s1998
- Created: 2018-02-05T19:27:26.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-04-21T05:38:34.000Z (about 7 years ago)
- Last Synced: 2025-02-01T18:27:25.011Z (4 months ago)
- Language: C
- Size: 1.23 MB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# COMPILER
Compiler for a small-C like language.
## Getting Started
Compiler for a small-C like language, will generate intermediate and assembly code (8085). Supports following grammar :
```
stmts -> stmt stmts | epsilon
stmt -> id := expr | if expr then stmt | while expr do stmt | begin stmts end
expression -> term_prime relop expression | term_prime
term_prime -> term + term_prime | term - term_prime | term
term -> factor * term | factor / term | factor
factor -> num | id | ( expression )
```## Running the sample code
To run code, write in samplecode file and execute code_gen_advamced.
The generated intermediate code is saved in intermediate_code.
Sample test cases are present in sample_test.
Code :
```
z := a + b * (c + d);
```Sample of parse tree generated is :
```
statements
statement
ID :=
expression
term_prime
term
factor
Num or ID
arithmetic_op +
term
factor
Num or ID
arithmetic_op *
factor
(
expression
term_prime
term
factor
Num or ID
arithmetic_op +
term
factor
Num or ID
)
```### Output printed
Output printed contains symbol table, parse tree and assembly code.
Sample code used is :
```
a := 1;
```Output is :
```
statements
statement
ID :=
expression
term_prime
term
factor
Num or ID
parse tree endsSymbol table :
_1 15
_a 14LDA _a
MOV B A
MOV C _1
MOV B C
MOV A B
SDA _a
```## Implementation details
Symbol table is stored using linked list.
Hashmap is used to store the ending pairs of if-fi, while-do-done and begin-end.
Files :
lex.h and lex.c : Used for lexical analysis.
code_gen_advanced.c : Used to generate the assembly code.
hashmap.c and list.c : Used for hashmap and list implementation.## References
For hashmap, source : http://www.kaushikbaruah.com/posts/data-structure-in-c-hashmap/
Initial (basic) source code was given as part of assignment of cs347.## Contributors
Ankit Kumar Singh, Shubhanshu Verma, Sudhanshu Ranjan.