From 0384c411107672aeb4bd1134d101b5dc62cc41bb Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Thu, 5 Jan 2012 09:03:42 +1000 Subject: Clean up the parser. --- LuaSL/src/LuaSL_LSL_tree.c | 112 +++++++++++++++++++++++++++------------------ LuaSL/src/LuaSL_LSL_tree.h | 38 ++++++++------- LuaSL/src/LuaSL_parser.c | 92 +++++-------------------------------- 3 files changed, 100 insertions(+), 142 deletions(-) diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c index 78da8e7..7ea5d67 100644 --- a/LuaSL/src/LuaSL_LSL_tree.c +++ b/LuaSL/src/LuaSL_LSL_tree.c @@ -1,63 +1,85 @@ /* * Implementation of functions used to build the abstract syntax tree. */ - -#include "LuaSL_LSL_tree.h" + +#include "LuaSL_parser_param.h" +#include "LuaSL_yaccer.tab.h" #include - + /** * @brief Allocates space for expression * @return The expression or NULL if not enough memory */ -static SExpression* allocateExpression() +static SExpression* newExpression(EOperationType type, SExpression *left, SExpression *right, int value) { - SExpression* b = malloc(sizeof *b); - - if( b == NULL ) return NULL; - - b->type = eVALUE; - b->value = 0; - - b->left = NULL; - b->right = NULL; - - return b; + SExpression* b = malloc(sizeof *b); + + if (b == NULL) return NULL; + + b->type = type; + b->value = value; + b->left = left; + b->right = right; + + return b; } - + SExpression* createNumber(int value) { - SExpression* b = allocateExpression(); - - if( b == NULL ) return NULL; - - b->type = eVALUE; - b->value = value; - - return b; + return newExpression(eVALUE, NULL, NULL, value); } - -SExpression *createOperation( - EOperationType type, - SExpression *left, - SExpression *right) + +SExpression *createOperation(EOperationType type, SExpression *left, SExpression *right) { - SExpression* b = allocateExpression(); - - if( b == NULL ) return NULL; - - b->type = type; - b->left = left; - b->right = right; - - return b; + return newExpression(type, left, right, 0); } - + +int evaluate(SExpression *e) +{ + switch(e->type) + { + case eVALUE: + return e->value; + case eMULTIPLY: + return evaluate(e->left) * evaluate(e->right); + case ePLUS: + return evaluate(e->left) + evaluate(e->right); + default: + // shouldn't be here + return 0; + } +} + void deleteExpression(SExpression *b) { - if (b == NULL) return; - - deleteExpression(b->left); - deleteExpression(b->right); - - free(b); + if (b == NULL) return; + + deleteExpression(b->left); + deleteExpression(b->right); + + free(b); +} + +SExpression *getAST(const char *expr) +{ + SParserParam p; + YY_BUFFER_STATE state; + + p.expression = NULL; + if (yylex_init(&(p.scanner))) + return NULL; + + state = yy_scan_string(expr, p.scanner); + if (yyparse(&p)) + return NULL; + + yy_delete_buffer(state, p.scanner); + yylex_destroy(p.scanner); + return p.expression; +} + +int yyerror(const char *msg) +{ + fprintf(stderr,"Error:%s\n",msg); return 0; } + diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h index 6d89672..71d372e 100644 --- a/LuaSL/src/LuaSL_LSL_tree.h +++ b/LuaSL/src/LuaSL_LSL_tree.h @@ -3,7 +3,7 @@ */ #ifndef __EXPRESSION_H__ #define __EXPRESSION_H__ - + /** * @brief The operation type */ @@ -12,27 +12,27 @@ typedef enum tagEOperationType eVALUE, eMULTIPLY, ePLUS -}EOperationType; - +} EOperationType; + /** * @brief The expression structure */ typedef struct tagSExpression { - EOperationType type;///< type of operation - - int value;///< valid only when type is eVALUE - struct tagSExpression* left; ///< left side of the tree - struct tagSExpression* right;///< right side of the tree -}SExpression; - + EOperationType type;///< type of operation + + int value;///< valid only when type is eVALUE + struct tagSExpression* left; ///< left side of the tree + struct tagSExpression* right;///< right side of the tree +} SExpression; + /** * @brief It creates an identifier * @param value The number value * @return The expression or NULL in case of no memory */ SExpression* createNumber(int value); - + /** * @brief It creates an operation * @param type The operation type @@ -40,16 +40,20 @@ SExpression* createNumber(int value); * @param right The right operand * @return The expression or NULL in case of no memory */ -SExpression* createOperation( - EOperationType type, - SExpression *left, - SExpression *right); - +SExpression* createOperation(EOperationType type, SExpression *left, SExpression *right); + /** * @brief Deletes a expression * @param b The expression */ void deleteExpression(SExpression *b); - + +SExpression *getAST(const char *expr); + +int evaluate(SExpression *e); + +int yyerror(const char *msg); +int yyparse(void *param); + #endif // __EXPRESSION_H__ diff --git a/LuaSL/src/LuaSL_parser.c b/LuaSL/src/LuaSL_parser.c index 9e1d3ca..c6026e2 100644 --- a/LuaSL/src/LuaSL_parser.c +++ b/LuaSL/src/LuaSL_parser.c @@ -1,86 +1,18 @@ -#include "LuaSL_parser_param.h" -#include "LuaSL_yaccer.tab.h" - - -int yyparse(void *param); - -static int initParserParam(SParserParam* param) -{ - int ret = 0; - - ret = yylex_init(¶m->scanner); - param->expression = NULL; - - return ret; -} - -static int destroyParserParam(SParserParam* param) -{ - return yylex_destroy(param->scanner); -} - -SExpression *getAST(const char *expr) -{ - SParserParam p; - YY_BUFFER_STATE state; - - if ( initParserParam(&p) ) - { - // couldn't initialize - return NULL; - } - - state = yy_scan_string(expr, p.scanner); - - if ( yyparse(&p) ) - { - // error parsing - return NULL; - } - - yy_delete_buffer(state, p.scanner); - - destroyParserParam(&p); - - return p.expression; -} - -int evaluate(SExpression *e) -{ - switch(e->type) - { - case eVALUE: - return e->value; - case eMULTIPLY: - return evaluate(e->left) * evaluate(e->right); - case ePLUS: - return evaluate(e->left) + evaluate(e->right); - default: - // shouldn't be here - return 0; - } -} +#include "LuaSL_LSL_tree.h" +#include -int yyerror(const char *msg) -{ - fprintf(stderr,"Error:%s\n",msg); return 0; -} int main(void) { - SExpression *e = NULL; - char test[]=" 4 + 2*10 + 3*( 5 + 1 )"; - int result = 0; - - e = getAST(test); - - result = evaluate(e); - - printf("Result of '%s' is %d\n", test, result); - - deleteExpression(e); - - return 0; -} + char test[]=" 4 + 2*10 + 3*( 5 + 1 )"; + SExpression *e = NULL; + int result = 0; + + e = getAST(test); + result = evaluate(e); + printf("Result of '%s' is %d\n", test, result); + deleteExpression(e); + return 0; +} -- cgit v1.1