Skip to content
This repository was archived by the owner on Nov 10, 2022. It is now read-only.

Commit 012278d

Browse files
committed
Initial commit
0 parents  commit 012278d

20 files changed

+2973
-0
lines changed

Makefile

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# File: Makefile
3+
# Creator: Adam Kaplan
4+
# Time-stamp: October 19, 2016
5+
#
6+
7+
PROGRAMS = main
8+
CFLAGS = -g -lm
9+
10+
programs: $(PROGRAMS)
11+
12+
13+
main: parse_tree.o stack.o bool_parser.o table_parser.o recursive_parser.o evaluator.o scanner.o
14+
15+
clean:
16+
-rm $(PROGRAMS) *.o
17+
-rm -r *.dSYM

README.md

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#Recursive Descent Parser in C
2+
3+
##General Information
4+
5+
This project is a recursive descent parser for arithmetic expressions. We had to implement a straight-forward arithmetic descent parser as well as a table-driven one. Furthermore, we also had to create an evaluator for the parse trees created.
6+
7+
##How to get it running
8+
9+
Before anything you have to run ```make```. This will create all of the executables for you. Now you get to customize how you want to run it.
10+
11+
```
12+
./main -t -p -e -f test.txt
13+
14+
```
15+
Here ```-t``` is the flag for activating the table-driven parser. By default the straightforward recursive descent parser will be used.
16+
17+
If you want to see the trees to be printed as well, make sure you include the ```-p``` flag.
18+
19+
The next flag is the ```-e``` one. This one activates the evaluator, which means you will also see the result to each of the expressions in the file.
20+
21+
The last flag is ```-f```, which lets you specify which file to read in and evaluate the expressions from. (Make sure to keep the file in the same folder)
22+
23+
###Warnings
24+
So since I am using the ``math.h`` library, I need to include the ```-lm``` flag in my makefile. Now on macs GCC will say it is useless, but if you want to compile it on the linux machines, it is actually necessary to do that, as otherwise it would just not include the library by default.
25+
26+
###Example runs
27+
For some quick examples you can run any of these commands after you ran the ```make``` command.
28+
29+
```
30+
./main -t -p -e -f test.txt
31+
./main -p -e -f test.txt
32+
./main -t -e -f test.txt
33+
./main -e -f test.txt
34+
./main -p -f test.txt
35+
./main -t -p -f test.txt
36+
37+
```
38+
39+
##File Layout
40+
The layout of a file such as ``test.txt`` should be as follows:
41+
42+
```
43+
1+1
44+
2*2-(5+22)
45+
...
46+
47+
```
48+
The important thing here is that the **expressions do not include spaces**.
49+
50+
##Supported Functions
51+
My grammar supports the following arithmetic expressions:
52+
53+
* Addition
54+
* Subtraction
55+
* Multiplication
56+
* Division
57+
* Parentheses: (), [], {}
58+
* Trigonometric functions: sin(), cos(), tan()
59+
* Logarithm function: log()
60+
* Factorial: 5! or 5.2! (yup it also calculates factorials for decimal numbers)
61+
* Power: 5^2 or 5.2^2.5
62+
63+
My calculator works for all decimals up to 6 decimal places (because I decided more are not usually necessary).
64+
65+
If you were wondering, it uses the gamma function to calculate the factorials, that way I can also do factorials of decimal numbers.
66+
67+
###Extra credit
68+
You can see that I implemented functions such as sin() and factorial. I also added support for decimal numbers in all aspects.
69+
70+
##Mentions
71+
I had some issues with the scanner, so I looked through StackOverflow, sine we never covered how to make a file reader in C. I, of course, wrote down the two threads in Stack Overflow that I used in my code.
72+
73+
I also used an article from StackOverflow, because I noticed that I was wrongly concatenating strings, since an empty char pointer does not end with ```'\0'``` and therefore I cannot concatenate them together. I also commented the source to my code
74+
75+
##File Information
76+
77+
* ``declarations.h``: This file does all of the necessary library inclusions as well as type declarations.
78+
* ``parse_tree.h``: This file is the header file for my parse tree.
79+
* ``parse_tree.c``: This is the actual parse tree.
80+
* ``stack.h``: Header file for my stack.
81+
* ``stack.c``: Actual stack file.
82+
* ``bool_parser.h``: Header file for a very simple implementation of a recursive descent parser which only tells you if an input is parseable or not.
83+
* ``bool_parser.c``: The actual bool parser.
84+
* ``recursive_parser.h``: Header file for my straightforward recursive descent parser.
85+
* ``recursive_parser.c``: The actual straightforward recursive descent parser.
86+
* ``table_parser.h``: Header file for my table-driven parser.
87+
* ``table_parser.c``: Actual table-driven parser.
88+
* ``evaluator.h``: Header file for my evaluator.
89+
* ``evaluator.c``: My actual evaluator.
90+
* ``scanner.h``: Header file for my file scanner.
91+
* ``scanner.c``: My actual scanner file.
92+
* ``main.c``: The main executable where everything comes together.
93+
* ``test.txt``: A testing file for my grammar with some random expressions, feel free to modify/use your own.

README.txt

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* File: README.txt
3+
* Creator: Adam Kaplan
4+
* Time-stamp: October 19, 2016
5+
* Project 2
6+
*/
7+
8+
== General Information ==
9+
10+
This project is a recursive descent parser for arithmetic expressions. We had to implement a straight-forward arithmetic descent parser as well as a table-driven one. Furthermore, we also had to create an evaluator for the parse trees created.
11+
12+
13+
== How to get it running ==
14+
15+
Before anything you have to run
16+
make
17+
This will create all of the executables for you. Now you get to customize how you want to run it.
18+
19+
./main -t -p -e -f test.txt
20+
21+
Here -t is the flag for activating the table-driven parser. By default the straightforward recursive descent parser will be used.
22+
23+
If you want to see the trees to be printed as well, make sure you include the -p flag.
24+
25+
The next flag is the -e one. This one activates the evaluator, which means you will also see the result to each of the expressions in the file.
26+
27+
The last flag is -f, which lets you specify which file to read in and evaluate the expressions from. (Make sure to keep the file in the same folder)
28+
29+
30+
== Warnings ==
31+
32+
So since I am using the math.h library, I need to include the -lm flag in my makefile. Now on macs CC will say it is useless, but if you want to compile it on the linux machines, it is actually necessary to do that, as otherwise it would just not include the library by default.
33+
34+
35+
== Example runs ==
36+
37+
For some quick examples you can run any of these commands after you ran the
38+
make
39+
command.
40+
41+
./main -t -p -e -f test.txt
42+
./main -p -e -f test.txt
43+
./main -t -e -f test.txt
44+
./main -e -f test.txt
45+
./main -p -f test.txt
46+
./main -t -p -f test.txt
47+
48+
49+
== File Layout ==
50+
51+
The layout of a file such as test.txt should be as follows:
52+
53+
1+1
54+
2*2-(5+22)
55+
...
56+
57+
The important thing here is that the expressions DO NOT INCLUDE SPACES.
58+
59+
60+
== Supported Functions ==
61+
62+
My grammar supports the following arithmetic expressions:
63+
64+
* Addition
65+
* Subtraction
66+
* Multiplication
67+
* Division
68+
* Parentheses: (), [], {}
69+
* Trigonometric functions: sin(), cos(), tan()
70+
* Logarithm function: log()
71+
* Factorial: 5! or 5.2! (yup it also calculates factorials for decimal numbers)
72+
* Power: 5^2 or 5.2^2.5
73+
74+
My calculator works for all decimals up to 6 decimal places (because I decided more are not usually necessary).
75+
76+
If you were wondering, it uses the gamma function to calculate the factorials, that way I can also do factorials of decimal numbers.
77+
78+
79+
== Extra credit ==
80+
81+
You can see that I implemented functions such as sin() and factorial. I also added support for decimal numbers in all aspects.
82+
83+
84+
== Mentions ==
85+
86+
I had some issues with the scanner, so I looked through StackOverflow, sine we never covered how to make a file reader in C. I wrote down the two threads in Stack Overflow that I used in my code.
87+
88+
I also used an article from StackOverflow, because I noticed that I was wrongly concatenating strings, since an empty char pointer does not end with '\0' and therefore I cannot concatenate them together. I also commented the source to my code
89+
90+
91+
== File Information ==
92+
93+
* declarations.h: This file does all of the necessary library inclusions as well as type declarations.
94+
* parse_tree.h: This file is the header file for my parse tree.
95+
* parse_tree.c: This is the actual parse tree.
96+
* stack.h: Header file for my stack.
97+
* stack.c: Actual stack file.
98+
* bool_parser.h: Header file for a very simple implementation of a recursive descent parser which only tells you if an input is parseable or not.
99+
* bool_parser.c: The actual bool parser.
100+
* recursive_parser.h: Header file for my straightforward recursive descent parser.
101+
* recursive_parser.c: The actual straightforward recursive descent parser.
102+
* table_parser.h: Header file for my table-driven parser.
103+
* table_parser.c: Actual table-driven parser.
104+
* evaluator.h: Header file for my evaluator.
105+
* evaluator.c: My actual evaluator.
106+
* scanner.h: Header file for my file scanner.
107+
* scanner.c: My actual scanner file.
108+
* main.c: The main executable where everything comes together.
109+
* test.txt: A testing file for my grammar with some random expressions, feel free to modify/use your own.

0 commit comments

Comments
 (0)