%{ #include extern yylineno; struct descMed { char* nome; int codigo; float preco; }; void imprimeLinha(struct descMed *m) { printf("%s%d%.2f\n", m->nome, m->codigo, m->preco); } %} %token SYMPOSIUM %token STRING %token INTEIRO %token REAL %type Medicamento %type LstMedicamentos LstMedicamentos2 %union { char* str; int inteiro; float real; struct descMed *med; } %start Raiz %% Raiz: SYMPOSIUM ':' INTEIRO '[' LstClasses ']' { printf("\n"); } '[' LstMedicamentos {printf("
%.2f",$9);}']' { printf("\n");} ; LstClasses: STRING | LstClasses ',' STRING ; LstMedicamentos: {$$ = 0;} | LstMedicamentos2 { $$ = $1; } ; LstMedicamentos2: Medicamento {$$ = $1->preco; imprimeLinha($1); } | LstMedicamentos2 ';' Medicamento { $$ = $1+$3->preco; imprimeLinha($3); } ; Medicamento: '(' STRING ',' INTEIRO ',' STRING ',' STRING ',' REAL ',' '{' LstFabs '}' ',' '{' LstMedicEq '}' ')' { $$->nome = strdup($2); $$->codigo = $4; $$->preco = $10; } ; LstFabs: STRING |LstFabs ',' STRING ; LstMedicEq : | LstMedicEq2 ; LstMedicEq2 : MedEq | LstMedicEq2 ',' MedEq ; MedEq : STRING '-' STRING ; %% void yyerror(char* s) { fprintf(stderr,"Syntax error on line %d",yylineno); } int main() { yyparse(); return 0; }