Arithmetic evaluation/C: Difference between revisions

no edit summary
(Copy from main task page)
 
No edit summary
 
(5 intermediate revisions by 4 users not shown)
Line 2:
 
This is a LL(1) recursive descent parser. Only performs integer division. There is a function for every non-terminal in the grammar, save add_op and mult_op, which were lumped into term_tail and factor_tail respectively.
<syntaxhighlight lang="c">
<lang c>#include "stdlib.h"
#include "stdio<stdlib.h">
#include "ctype<stdio.h">
<lang c>#include "stdlib<ctype.h">
 
unsigned int G_STRING_ITERATOR = 0;
Line 40 ⟶ 41:
}
 
/* Will "consume" a character from the input,
* (such as +, -, *, etc.) and return it.
* By consume, I'm really just moving the pointer
* forward and disregarding the character for
* future purposes.
*/
char consume_char(const char* string, char c) {
if(string[G_STRING_ITERATOR] != c) {
Line 48 ⟶ 55:
}
 
/* Same as consume_char, except for integers.
*/
int consume_int(const char* string) {
int i;
Line 55 ⟶ 64:
}
 
/* I don't have to pass in the start of the string
* into atoi, but only where I want it to start
* scanning for an integer.
*/
i = atoi(string + G_STRING_ITERATOR);
while(isdigit(string[G_STRING_ITERATOR])) {
Line 156 ⟶ 169:
}
 
/* Runs through the AST, evaluating and freeing
* the tree as it goes.
*/
int evaluate(Expr* expr) {
int ret;
Line 211 ⟶ 227:
int main(int argc, char** argv) {
Expr* expr = NULL;
G_STRING_ITERATOR = 0;
 
if(argc > 1) {
Line 219 ⟶ 236:
return 0;
}
</syntaxhighlight>
</lang>
404

edits