/* * listas.c * Recursive descencent Parser for integer lists */ #include #include "tokens.h" #include "listasGA.h" #include "listasSEM.h" extern int yylex(); static int prox_simb; int da_simbolo() { return yylex(); } rec_term(int simb) { if(prox_simb == simb) prox_simb = da_simbolo(); else{ fprintf(stderr, "ERROR: unexpexpected token = %d",prox_simb); exit(1); } } void error() { fprintf(stderr, "ERROR: Syntactic, near - %d", prox_simb); exit(2); } /* ---- Parser functions declaration ---- */ List rec_List(); List rec_List2(); List rec_ElemList(); List rec_ElemList2(); /* ------------------------------------------- */ /* ---- TOKENIZER - debug only ----- */ void tokenizer() { while(prox_simb) { printf("%d\n",prox_simb); prox_simb=da_simbolo(); } exit(0); } /* ------------------------------------------- */ /* ---- THE PARSER ----- */ main() { List res; prox_simb = da_simbolo(); res = rec_List(); showList(res); printf("\n\nSoma = %d \n\n", SumList(res)); } List rec_List() { rec_term(OLIST); return( rec_List2() ); } List rec_List2() { List aux; switch(prox_simb) { case CLIST: rec_term(CLIST); aux = cons_P0(); break; case NUM: aux = rec_ElemList(); rec_term(CLIST); break; default: error(); } return(aux); } List rec_ElemList() { List aux; int aux2; aux2 = lexval; rec_term(NUM); aux = rec_ElemList2(); return( cons_P1(aux2,aux) ); } List rec_ElemList2() { List aux; int aux2; switch(prox_simb) { case CLIST: aux = cons_P0(); break; case SEP: rec_term(SEP); aux2 = lexval; rec_term(NUM); aux = cons_P1( aux2, rec_ElemList2() ); break; default: error(); } return(aux); }