--- /dev/null
+\r
+/* ******************************** */\r
+/* PER UNA CORRETTA VISUALIZZAZIONE */\r
+/* tab size=4 */\r
+/* ******************************** */\r
+\r
+%{\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include <sys/types.h>\r
+\r
+/* ************************************************************************* */\r
+\r
+#define YYDEBUG 1 /* debug interno di bison */\r
+\r
+#define inline /* compatibilita' con l'opzione %glr-parser */\r
+#define YYLTYPE_IS_DECLARED 1 /* compatibilita' con VISUAL C */\r
+\r
+#define SEP '\n' /* formattazione in input */\r
+\r
+#define YYSTYPE valueLeaf * /* struttura puntata dai nodi/foglie */\r
+\r
+#define ERR_EXIT -1\r
+\r
+/* manipolazione della posizione nel sorgente (bison) */\r
+#define YYLLOC_DEFAULT(Current,Rhs,N) \\r
+ do \\r
+ if(N) \\r
+ { \\r
+ (Current).first_line=YYRHSLOC(Rhs,1).first_line; \\r
+ (Current).first_column=YYRHSLOC(Rhs,1).first_column; \\r
+ (Current).last_line=YYRHSLOC(Rhs,N).last_line; \\r
+ (Current).last_column=YYRHSLOC(Rhs,N).last_column; \\r
+ (Current).source=YYRHSLOC(Rhs,1).source; \\r
+ } \\r
+ else \\r
+ { \\r
+ (Current).first_line=(Current).last_line=YYRHSLOC(Rhs,0).last_line; \\r
+ (Current).first_column=(Current).last_column=YYRHSLOC(Rhs,0).last_column; \\r
+ (Current).source=YYRHSLOC(Rhs,0).source; \\r
+ } \\r
+ while(0)\r
+\r
+#ifndef __STDC__\r
+#define __STDC__\r
+\r
+#define myMalloc(x) malloc((x<512) ? 512 : x)\r
+typedef unsigned __int32 _DWORD_; /* definizione univoca DWORD */\r
+typedef unsigned __int16 _WORD_; /* definizione univoca WORD */\r
+typedef unsigned __int8 _BYTE_; /* definizione univoca BYTE */\r
+\r
+#else\r
+\r
+#define myMalloc(x) malloc((x<512) ? 512 : x)\r
+typedef __uint32_t _DWORD_; /* definizione univoca DWORD */\r
+typedef __uint16_t _WORD_; /* definizione univoca WORD */\r
+typedef __uint8_t _BYTE_; /* definizione univoca BYTE */\r
+\r
+#endif\r
+\r
+/* ************************************************************************* */\r
+\r
+/* struttura per la posizione manipolata direttamente da YYLOC_DEFAULT (bison) */\r
+typedef struct YYLTYPE\r
+ {\r
+ int first_line;\r
+ int first_column;\r
+ int last_line;\r
+ int last_column;\r
+ char *source;\r
+ } YYLTYPE;\r
+\r
+/* enumerazione delle etichette dei nodi/foglie */\r
+typedef enum {\r
+ E_VALUE_ARRAY,\r
+ E_VALUE_STRUCT,\r
+ E_VALUE_DWORD,\r
+ E_VALUE_WORD,\r
+ E_VALUE_BYTE,\r
+ E_VALUE_INUM,\r
+ E_VALUE_STRING,\r
+\r
+ E_DECL_CONST,\r
+ E_DECL_VAR,\r
+\r
+ E_STM_ASG,\r
+ E_STM_WHILE,\r
+ E_STM_IF,\r
+ E_STM_IFEXPRBODY,\r
+\r
+ E_EXPR_NEG,\r
+ E_EXPR_NOT,\r
+ E_EXPR_COM,\r
+ E_EXPR_MUL,\r
+ E_EXPR_DIV,\r
+ E_EXPR_ADD,\r
+ E_EXPR_SUB,\r
+ E_EXPR_SHL,\r
+ E_EXPR_SHR,\r
+ E_EXPR_LT,\r
+ E_EXPR_LTE,\r
+ E_EXPR_GT,\r
+ E_EXPR_GTE,\r
+ E_EXPR_NEQ,\r
+ E_EXPR_EQ,\r
+ E_EXPR_AND,\r
+ E_EXPR_OR,\r
+ E_EXPR_XOR,\r
+ E_EXPR_B2W,\r
+ E_EXPR_B2DW,\r
+ E_EXPR_W2B,\r
+ E_EXPR_W2DW,\r
+ E_EXPR_DW2B,\r
+ E_EXPR_DW2W,\r
+ E_EXPR_VAR,\r
+\r
+ E_VAR_ROOT,\r
+ E_VAR_STRUCT,\r
+ E_VAR_ARRAY,\r
+\r
+ E_TYPE_ARRAY,\r
+ E_TYPE_STRUCT,\r
+ E_TYPE_DWORD,\r
+ E_TYPE_WORD,\r
+ E_TYPE_BYTE,\r
+\r
+ } vEnum;\r
+\r
+/* struttura polimorfa dei nodi/foglie */\r
+typedef struct _valueLeaf_\r
+ {\r
+ /* etichetta */\r
+ vEnum id;\r
+\r
+ _DWORD_ dwordValue;\r
+ _WORD_ wordValue;\r
+ _BYTE_ byteValue;\r
+ unsigned long int inumValue;\r
+ char *stringValue;\r
+\r
+ struct _valueLeaf_ *param1;\r
+ struct _valueLeaf_ *param2;\r
+ struct _valueLeaf_ *param3;\r
+ struct _valueLeaf_ *param4;\r
+\r
+ /* per tutti */\r
+ struct _valueLeaf_ *next;\r
+ struct _valueLeaf_ *last;\r
+\r
+ /* posizione nel sorgente */\r
+ int fileLine;\r
+ int fileRow;\r
+ char *source;\r
+ } valueLeaf;\r
+\r
+/* ************************************************************************* */\r
+\r
+valueLeaf root; /* radice dell'AST */\r
+\r
+int numErr=0;\r
+\r
+/* ************************************************************************* */\r
+\r
+int yylex(YYSTYPE *lvalp,YYLTYPE *llocp,void *p);\r
+void yyerror(const YYLTYPE *locp,void *p,char const *msg);\r
+valueLeaf *treeAlloc(\\r
+ vEnum id,\\r
+ _DWORD_ dwordValue,\\r
+ _WORD_ wordValue,\\r
+ _BYTE_ byteValue,\\r
+ unsigned long int inumValue,\\r
+ char *stringValue,\\r
+ valueLeaf *param1,\\r
+ valueLeaf *param2,\\r
+ valueLeaf *param3,\\r
+ const YYLTYPE *locp);\r
+\r
+valueLeaf *treeDwordAlloc(_DWORD_ dwordValue,const YYLTYPE *locp);\r
+valueLeaf *treeWordAlloc(_WORD_ wordValue,const YYLTYPE *locp);\r
+valueLeaf *treeByteAlloc(_BYTE_ byteValue,const YYLTYPE *locp);\r
+valueLeaf *treeInumAlloc(unsigned long int inumValue,const YYLTYPE *locp);\r
+valueLeaf *treeStringAlloc(char *stringValue,const YYLTYPE *locp);\r
+valueLeaf *treeId0Alloc(vEnum id,const YYLTYPE *locp);\r
+valueLeaf *treeId1Alloc(vEnum id,valueLeaf *param1,const YYLTYPE *locp);\r
+valueLeaf *treeId2Alloc(vEnum id,valueLeaf *param1,valueLeaf *param2,const YYLTYPE *locp);\r
+valueLeaf *treeId3Alloc(vEnum id,valueLeaf *param1,valueLeaf *param2,valueLeaf *param3,const YYLTYPE *locp);\r
+\r
+void prettyPrint(valueLeaf *cur,int indent);\r
+\r
+/* ************************************************************************* */\r
+\r
+%}\r
+\r
+%locations\r
+%pure-parser\r
+%lex-param {void *p}\r
+%parse-param {void *p}\r
+%error-verbose\r
+\r
+/* ************************************************************************* */\r
+\r
+/* keyword */\r
+%token Keyword_STRUCT Keyword_ARRAY Keyword_OF \r
+%token Keyword_IF Keyword_ELSIF Keyword_ELSE Keyword_WHILE Keyword_CONST\r
+%token Keyword_B2W Keyword_B2DW Keyword_W2B Keyword_W2DW Keyword_DW2B Keyword_DW2W\r
+\r
+/* tipi nativi */\r
+%token Type_BYTE Type_WORD Type_DWORD\r
+\r
+/* parentesi */\r
+%token OpenBracket_BODY CloseBracket_BODY\r
+%token OpenBracket_SQUARE CloseBracket_SQUARE\r
+%token OpenBracket_ROUND CloseBracket_ROUND\r
+\r
+/* operatori unari */\r
+%token UnaryOperator_NOT UnaryOperator_COM\r
+\r
+/* operatori duali */\r
+%token DualOperator_AND DualOperator_OR DualOperator_XOR\r
+%token DualOperator_ADD DualOperator_SUB DualOperator_MUL DualOperator_DIV\r
+%token DualOperator_NEQ DualOperator_EQ DualOperator_LT DualOperator_GT DualOperator_LTE DualOperator_GTE\r
+%token DualOperator_ASG DualOperator_SHR DualOperator_SHL\r
+\r
+/* separatori */\r
+%token Separator_COLUMN Separator_SELECT Separator_COMMA\r
+\r
+/* valori */\r
+%token Value_DWORD Value_WORD Value_BYTE Value_INUM Value_STRING\r
+\r
+/* ************************************************************************* */\r
+\r
+%start _start_\r
+\r
+/* priorita' minima */\r
+\r
+%right Separator_COMMA\r
+\r
+%nonassoc DualOperator_ASG\r
+\r
+%nonassoc Keyword_B2W Keyword_B2DW Keyword_W2B Keyword_W2DW Keyword_DW2B Keyword_DW2W\r
+\r
+%left DualOperator_OR\r
+%left DualOperator_XOR\r
+%left DualOperator_AND\r
+\r
+%nonassoc DualOperator_NEQ\r
+%nonassoc DualOperator_EQ\r
+%nonassoc DualOperator_GTE\r
+%nonassoc DualOperator_GT\r
+%nonassoc DualOperator_LTE\r
+%nonassoc DualOperator_LT\r
+\r
+%left DualOperator_SHR\r
+%left DualOperator_SHL\r
+%left DualOperator_SUB\r
+%left DualOperator_ADD\r
+%left DualOperator_DIV\r
+%left DualOperator_MUL\r
+\r
+%nonassoc UnaryOperator_SUB\r
+%nonassoc UnaryOperator_COM\r
+%nonassoc UnaryOperator_NOT\r
+\r
+%right Separator_SELECT\r
+%right Separator_COLUMN\r
+\r
+%nonassoc OpenBracket_SQUARE CloseBracket_SQUARE\r
+%nonassoc OpenBracket_ROUND CloseBracket_ROUND \r
+%nonassoc OpenBracket_BODY CloseBracket_BODY\r
+\r
+/* priorita' massima */ \r
+\r
+/* ************************************************************************* */\r
+\r
+%%\r
+\r
+/* ** FUNZIONE PRINCIPALE ** */\r
+_start_ : OpenBracket_BODY _decl_lst_ CloseBracket_BODY\r
+ { memcpy(&root,$2,sizeof(valueLeaf)); };\r
+\r
+/* ** GESTIONE DICHIARAZIONI ** */\r
+_decl_lst_ : Keyword_CONST _type_ Value_STRING DualOperator_ASG _init_choice_ Separator_COLUMN _decl_lst_\r
+ {\r
+ $$=treeId3Alloc(E_DECL_CONST,$2,$3,$5,&@1);\r
+ $$->next=$7;\r
+ }|\r
+ _type_ Value_STRING _init_ Separator_COLUMN _decl_lst_\r
+ {\r
+ $$=treeId3Alloc(E_DECL_VAR,$1,$2,$3,&@1);\r
+ $$->next=$5;\r
+ }|\r
+ _stm_lst_\r
+ { $$=$1; };\r
+\r
+_type_ : Keyword_ARRAY OpenBracket_SQUARE Value_INUM CloseBracket_SQUARE Keyword_OF _type_\r
+ {\r
+ if(!($3->inumValue))\r
+ { yyerror(&@1,p,"unallowed empty array"); }\r
+ else\r
+ {\r
+ $$=treeId2Alloc(E_TYPE_ARRAY,$3,$6,&@1);\r
+ $3->inumValue--;\r
+ }\r
+ }|\r
+ Keyword_STRUCT OpenBracket_BODY _type_ Separator_COLUMN _type_lst_ CloseBracket_BODY\r
+ {\r
+ $$=treeId1Alloc(E_TYPE_STRUCT,$3,&@1);\r
+ $3->next=$5;\r
+ }|\r
+ Type_DWORD\r
+ { $$=treeId0Alloc(E_TYPE_DWORD,&@1); }|\r
+ Type_WORD\r
+ { $$=treeId0Alloc(E_TYPE_WORD,&@1); }|\r
+ Type_BYTE\r
+ { $$=treeId0Alloc(E_TYPE_BYTE,&@1); };\r
+\r
+_type_lst_ : _type_ Separator_COLUMN _type_lst_\r
+ {\r
+ $1->next=$3;\r
+ $$=$1;\r
+ }|\r
+ /* epsilon */\r
+ { $$=NULL; };\r
+\r
+_init_ : DualOperator_ASG _init_choice_\r
+ { $$=$2; }|\r
+ /* epsilon */\r
+ { $$=NULL; };\r
+\r
+_init_choice_ : _init_val_\r
+ { $$=$1; }|\r
+ _var_\r
+ { $$=$1; };\r
+\r
+_init_val_ : OpenBracket_SQUARE _init_val_ _init_val_lst_ CloseBracket_SQUARE\r
+ {\r
+ $$=treeId1Alloc(E_VALUE_ARRAY,$2,&@1);\r
+ $2->next=$3;\r
+ }|\r
+ OpenBracket_BODY _init_val_ _init_val_lst_ CloseBracket_BODY\r
+ {\r
+ $$=treeId1Alloc(E_VALUE_STRUCT,$2,&@1);\r
+ $2->next=$3;\r
+ }|\r
+ Value_DWORD\r
+ { $$=$1; }|\r
+ Value_WORD\r
+ { $$=$1; }|\r
+ Value_BYTE\r
+ { $$=$1; };\r
+\r
+_init_val_lst_ : Separator_COMMA _init_val_ _init_val_lst_\r
+ {\r
+ $2->next=$3;\r
+ $$=$2;\r
+ }|\r
+ /* epsilon */\r
+ { $$=NULL; };\r
+\r
+/* ** GESTIONE STATEMENT ** */\r
+_stm_lst_ : _var_ DualOperator_ASG _expr_ Separator_COLUMN _stm_lst_\r
+ {\r
+ $$=treeId2Alloc(E_STM_ASG,$1,$3,&@1);\r
+ $$->next=$5;\r
+ }|\r
+ Keyword_WHILE OpenBracket_ROUND _expr_ CloseBracket_ROUND OpenBracket_BODY _decl_lst_ CloseBracket_BODY _stm_lst_\r
+ {\r
+ $$=treeId2Alloc(E_STM_WHILE,$3,$6,&@1);\r
+ $$->next=$8;\r
+ }|\r
+ Keyword_IF OpenBracket_ROUND _expr_ CloseBracket_ROUND OpenBracket_BODY _decl_lst_ CloseBracket_BODY _elsif_lst_ _else_ _stm_lst_\r
+ {\r
+ $$=treeId2Alloc(E_STM_IF,treeId2Alloc(E_STM_IFEXPRBODY,$3,$6,&@1),$9,&@1);\r
+ $$->param1->next=$8;\r
+ $$->next=$10;\r
+ }|\r
+ /* epsilon */\r
+ { $$=NULL; };\r
+\r
+_elsif_lst_ : Keyword_ELSIF OpenBracket_ROUND _expr_ CloseBracket_ROUND OpenBracket_BODY _decl_lst_ CloseBracket_BODY _elsif_lst_\r
+ {\r
+ $$=treeId2Alloc(E_STM_IFEXPRBODY,$3,$6,&@1);\r
+ $$->next=$8;\r
+ }|\r
+ /* epsilon */\r
+ { $$=NULL; };\r
+\r
+_else_ : Keyword_ELSE OpenBracket_BODY _decl_lst_ CloseBracket_BODY\r
+ { $$=$3; }|\r
+ /* epsilon */\r
+ { $$=NULL; }\r
+\r
+_expr_ : OpenBracket_ROUND _expr_ CloseBracket_ROUND\r
+ { $$=$2; }|\r
+ UnaryOperator_NOT _expr_\r
+ { $$=treeId1Alloc(E_EXPR_NOT,$2,&@1); }|\r
+ UnaryOperator_COM _expr_\r
+ { $$=treeId1Alloc(E_EXPR_COM,$2,&@1); }|\r
+ DualOperator_SUB _expr_ %prec UnaryOperator_SUB\r
+ { $$=treeId1Alloc(E_EXPR_NEG,$2,&@1); }|\r
+ Keyword_B2W OpenBracket_ROUND _expr_ CloseBracket_ROUND\r
+ { $$=treeId1Alloc(E_EXPR_B2W,$3,&@1); }|\r
+ Keyword_B2DW OpenBracket_ROUND _expr_ CloseBracket_ROUND\r
+ { $$=treeId1Alloc(E_EXPR_B2DW,$3,&@1); }|\r
+ Keyword_W2B OpenBracket_ROUND _expr_ CloseBracket_ROUND\r
+ { $$=treeId1Alloc(E_EXPR_W2B,$3,&@1); }|\r
+ Keyword_W2DW OpenBracket_ROUND _expr_ CloseBracket_ROUND\r
+ { $$=treeId1Alloc(E_EXPR_W2DW,$3,&@1); }|\r
+ Keyword_DW2B OpenBracket_ROUND _expr_ CloseBracket_ROUND\r
+ { $$=treeId1Alloc(E_EXPR_DW2B,$3,&@1); }|\r
+ Keyword_DW2W OpenBracket_ROUND _expr_ CloseBracket_ROUND\r
+ { $$=treeId1Alloc(E_EXPR_DW2W,$3,&@1); }|\r
+ _expr_ DualOperator_MUL _expr_\r
+ { $$=treeId2Alloc(E_EXPR_MUL,$1,$3,&@1); }|\r
+ _expr_ DualOperator_DIV _expr_\r
+ { $$=treeId2Alloc(E_EXPR_DIV,$1,$3,&@1); }|\r
+ _expr_ DualOperator_ADD _expr_\r
+ { $$=treeId2Alloc(E_EXPR_ADD,$1,$3,&@1); }|\r
+ _expr_ DualOperator_SUB _expr_\r
+ { $$=treeId2Alloc(E_EXPR_SUB,$1,$3,&@1); }|\r
+ _expr_ DualOperator_SHL _expr_\r
+ { $$=treeId2Alloc(E_EXPR_SHL,$1,$3,&@1); }|\r
+ _expr_ DualOperator_SHR _expr_\r
+ { $$=treeId2Alloc(E_EXPR_SHR,$1,$3,&@1); }|\r
+ _expr_ DualOperator_LT _expr_\r
+ { $$=treeId2Alloc(E_EXPR_LT,$1,$3,&@1); }|\r
+ _expr_ DualOperator_LTE _expr_\r
+ { $$=treeId2Alloc(E_EXPR_LTE,$1,$3,&@1); }|\r
+ _expr_ DualOperator_GT _expr_\r
+ { $$=treeId2Alloc(E_EXPR_GT,$1,$3,&@1); }|\r
+ _expr_ DualOperator_GTE _expr_\r
+ { $$=treeId2Alloc(E_EXPR_GTE,$1,$3,&@1); }|\r
+ _expr_ DualOperator_NEQ _expr_\r
+ { $$=treeId2Alloc(E_EXPR_NEQ,$1,$3,&@1); }|\r
+ _expr_ DualOperator_EQ _expr_\r
+ { $$=treeId2Alloc(E_EXPR_EQ,$1,$3,&@1); }|\r
+ _expr_ DualOperator_AND _expr_\r
+ { $$=treeId2Alloc(E_EXPR_AND,$1,$3,&@1); }|\r
+ _expr_ DualOperator_OR _expr_\r
+ { $$=treeId2Alloc(E_EXPR_OR,$1,$3,&@1); }|\r
+ _expr_ DualOperator_XOR _expr_\r
+ { $$=treeId2Alloc(E_EXPR_XOR,$1,$3,&@1); }|\r
+ Value_DWORD\r
+ { $$=$1; }|\r
+ Value_WORD\r
+ { $$=$1; }|\r
+ Value_BYTE\r
+ { $$=$1; }|\r
+ _var_\r
+ { $$=treeId1Alloc(E_EXPR_VAR,$1,&@1); };\r
+\r
+_var_ : Value_STRING _var_sub_\r
+ {\r
+ if($2)\r
+ {\r
+ $$=$2;\r
+ $$->last->next=treeId1Alloc(E_VAR_ROOT,$1,&@1);\r
+ }\r
+ else\r
+ { $$=treeId1Alloc(E_VAR_ROOT,$1,&@1); }\r
+ };\r
+\r
+_var_sub_ : Separator_SELECT Value_INUM _var_sub_\r
+ {\r
+ if($3)\r
+ {\r
+ valueLeaf *tmp=treeId1Alloc(E_VAR_STRUCT,$2,&@1);\r
+\r
+ $$=$3;\r
+ $$->last->next=tmp;\r
+ $$->last=tmp;\r
+ }\r
+ else\r
+ {\r
+ $$=treeId1Alloc(E_VAR_STRUCT,$2,&@1);\r
+ $$->last=$$;\r
+ }\r
+ }|\r
+ OpenBracket_SQUARE _expr_ CloseBracket_SQUARE _var_sub_\r
+ {\r
+ if($4)\r
+ {\r
+ valueLeaf *tmp=treeId1Alloc(E_VAR_ARRAY,$2,&@1);\r
+\r
+ $$=$4;\r
+ $$->last->next=tmp;\r
+ $$->last=tmp;\r
+ }\r
+ else\r
+ {\r
+ $$=treeId1Alloc(E_VAR_ARRAY,$2,&@1);\r
+ $$->last=$$;\r
+ }\r
+ }|\r
+ /* epsilon */\r
+ { $$=NULL; };\r
+\r
+/* ************************************************************************* */\r
+\r
+%%\r
+\r
+void yyerror(const YYLTYPE *locp,void *p,char const *msg)\r
+{\r
+ if(locp->first_line>=0)\r
+ { printf("%s:%d,%d\tParsError: %s%c",locp->source,locp->first_line,locp->first_column,msg,SEP); }\r
+ else\r
+ { printf(":0,0\tParsError: %s%c",msg,SEP); }\r
+\r
+ numErr++;\r
+\r
+ return;\r
+}\r
+\r
+valueLeaf *treeAlloc(\\r
+ vEnum id,\\r
+ _DWORD_ dwordValue,\\r
+ _WORD_ wordValue,\\r
+ _BYTE_ byteValue,\\r
+ unsigned long int inumValue,\\r
+ char *stringValue,\\r
+ valueLeaf *param1,\\r
+ valueLeaf *param2,\\r
+ valueLeaf *param3,\\r
+ const YYLTYPE *locp)\r
+{\r
+ valueLeaf *tmp;\r
+\r
+ if(!(tmp=(valueLeaf *) myMalloc(sizeof(valueLeaf))))\r
+ {\r
+ printf("[%s:%d] ParsError:OutOfMemory%c",__FILE__,__LINE__,SEP);\r
+ exit(ERR_EXIT);\r
+ }\r
+\r
+ memset(tmp,0,sizeof(valueLeaf));\r
+\r
+ tmp->id=id;\r
+ tmp->dwordValue=dwordValue;\r
+ tmp->wordValue=wordValue;\r
+ tmp->byteValue=byteValue;\r
+ tmp->inumValue=inumValue;\r
+ tmp->stringValue=stringValue;\r
+ tmp->param1=param1;\r
+ tmp->param2=param2;\r
+ tmp->param3=param3;\r
+\r
+ return(tmp);\r
+}\r
+\r
+valueLeaf *treeDwordAlloc(_DWORD_ dwordValue,const YYLTYPE *locp)\r
+{ return(treeAlloc(E_VALUE_DWORD,dwordValue,0,0,0,NULL,NULL,NULL,NULL,locp)); }\r
+\r
+valueLeaf *treeWordAlloc(_WORD_ wordValue,const YYLTYPE *locp)\r
+{ return(treeAlloc(E_VALUE_WORD,0,wordValue,0,0,NULL,NULL,NULL,NULL,locp)); }\r
+\r
+valueLeaf *treeByteAlloc(_BYTE_ byteValue,const YYLTYPE *locp)\r
+{ return(treeAlloc(E_VALUE_BYTE,0,0,byteValue,0,NULL,NULL,NULL,NULL,locp)); }\r
+\r
+valueLeaf *treeInumAlloc(unsigned long int inumValue,const YYLTYPE *locp)\r
+{ return(treeAlloc(E_VALUE_INUM,0,0,0,inumValue,NULL,NULL,NULL,NULL,locp)); }\r
+\r
+valueLeaf *treeStringAlloc(char *stringValue,const YYLTYPE *locp)\r
+{ return(treeAlloc(E_VALUE_STRING,0,0,0,0,stringValue,NULL,NULL,NULL,locp)); }\r
+\r
+valueLeaf *treeId0Alloc(vEnum id,const YYLTYPE *locp)\r
+{ return(treeAlloc(id,0,0,0,0,NULL,NULL,NULL,NULL,locp)); }\r
+\r
+valueLeaf *treeId1Alloc(vEnum id,valueLeaf *param1,const YYLTYPE *locp)\r
+{ return(treeAlloc(id,0,0,0,0,NULL,param1,NULL,NULL,locp)); }\r
+\r
+valueLeaf *treeId2Alloc(vEnum id,valueLeaf *param1,valueLeaf *param2,const YYLTYPE *locp)\r
+{ return(treeAlloc(id,0,0,0,0,NULL,param1,param2,NULL,locp)); }\r
+\r
+valueLeaf *treeId3Alloc(vEnum id,valueLeaf *param1,valueLeaf *param2,valueLeaf *param3,const YYLTYPE *locp)\r
+{ return(treeAlloc(id,0,0,0,0,NULL,param1,param2,param3,locp)); }\r
+\r
+/* riconoscimento token da input */\r
+int yylex(YYSTYPE *lvalp,YYLTYPE *llocp,void *p)\r
+{\r
+ char bufLine[256];\r
+ char bufToken[256];\r
+ char bufString[256];\r
+ char *tmp1,*tmp2;\r
+\r
+ /* riconoscimento di source:linea,colonna */\r
+ fscanf(stdin,"%s%s%s\n",bufLine,bufToken,bufString,SEP);\r
+\r
+ if(!strcmp(bufToken,"ERR"))\r
+ {\r
+ printf("%s\t%s%c",bufLine,bufString,SEP);\r
+ exit(ERR_EXIT);\r
+ }\r
+\r
+ for(tmp1=bufLine;*tmp1!=':';tmp1++);\r
+ *tmp1=0;\r
+\r
+ for(tmp2=tmp1+1;*tmp2!=',';tmp2++);\r
+ *tmp2=0;\r
+\r
+ llocp->first_line=llocp->last_line=atoi(tmp1+1);\r
+ llocp->first_column=llocp->last_column=atoi(tmp2+1);\r
+\r
+ if(!(llocp->source=(char *) myMalloc(strlen(bufLine)+1)))\r
+ {\r
+ printf("[%s:%d] ParsError:OutOfMemory%c",__FILE__,__LINE__,SEP);\r
+ exit(ERR_EXIT);\r
+ }\r
+\r
+ *llocp->source=0;\r
+ strcpy(llocp->source,bufLine);\r
+\r
+ *tmp1=':';\r
+ *tmp2=',';\r
+\r
+ /* analisi */\r
+ if(!strcmp(bufToken,"OK"))\r
+ {\r
+ root.source=llocp->source;\r
+ root.fileLine=llocp->first_line;\r
+ root.fileRow=llocp->first_column;\r
+\r
+ return yylex(lvalp,llocp,p);\r
+ }\r
+ else if(!strcmp(bufToken,"EOF"))\r
+ { return(0); }\r
+\r
+ /* categoria keyword */\r
+ if(!strcmp(bufToken,"KEW"))\r
+ {\r
+ if(!strcmp(bufString,"struct"))\r
+ { return(Keyword_STRUCT); }\r
+ else if(!strcmp(bufString,"array"))\r
+ { return(Keyword_ARRAY); }\r
+ else if(!strcmp(bufString,"of"))\r
+ { return(Keyword_OF); }\r
+ else if(!strcmp(bufString,"if"))\r
+ { return(Keyword_IF); }\r
+ else if(!strcmp(bufString,"elsif"))\r
+ { return(Keyword_ELSIF); }\r
+ else if(!strcmp(bufString,"else"))\r
+ { return(Keyword_ELSE); }\r
+ else if(!strcmp(bufString,"while"))\r
+ { return(Keyword_WHILE); }\r
+ else if(!strcmp(bufString,"const"))\r
+ { return(Keyword_CONST); }\r
+ else if(!strcmp(bufString,"b2w"))\r
+ { return(Keyword_B2W); }\r
+ else if(!strcmp(bufString,"b2dw"))\r
+ { return(Keyword_B2DW); }\r
+ else if(!strcmp(bufString,"w2b"))\r
+ { return(Keyword_W2B); }\r
+ else if(!strcmp(bufString,"w2dw"))\r
+ { return(Keyword_W2DW); }\r
+ else if(!strcmp(bufString,"dw2b"))\r
+ { return(Keyword_DW2B); }\r
+ else if(!strcmp(bufString,"dw2w"))\r
+ { return(Keyword_DW2W); }\r
+ else\r
+ {\r
+ printf("%s\tParsError:UnknownKeywordToken(%s)%c",bufLine,bufString,SEP);\r
+ exit(ERR_EXIT);\r
+ }\r
+ }\r
+ /* categoria tipo nativo */\r
+ else if(!strcmp(bufToken,"TYP"))\r
+ {\r
+ if(!strcmp(bufString,"dword"))\r
+ { return(Type_DWORD); }\r
+ else if(!strcmp(bufString,"word"))\r
+ { return(Type_WORD); }\r
+ else if(!strcmp(bufString,"byte"))\r
+ { return(Type_BYTE); }\r
+ else\r
+ {\r
+ printf("%s\tParsError:UnknownTypeToken(%s)%c",bufLine,bufString,SEP);\r
+ exit(ERR_EXIT);\r
+ }\r
+ }\r
+ /* categoria parentesi */\r
+ else if(!strcmp(bufToken,"BRK"))\r
+ {\r
+ if(!strcmp(bufString,"{"))\r
+ { return(OpenBracket_BODY); }\r
+ else if(!strcmp(bufString,"}"))\r
+ { return(CloseBracket_BODY); }\r
+ else if(!strcmp(bufString,"["))\r
+ { return(OpenBracket_SQUARE); }\r
+ else if(!strcmp(bufString,"]"))\r
+ { return(CloseBracket_SQUARE); }\r
+ else if(!strcmp(bufString,"("))\r
+ { return(OpenBracket_ROUND); }\r
+ else if(!strcmp(bufString,")"))\r
+ { return(CloseBracket_ROUND); }\r
+ else\r
+ {\r
+ printf("%s\tParsError:UnknownBracketToken(%s)%c",bufLine,bufString,SEP);\r
+ exit(ERR_EXIT);\r
+ }\r
+ }\r
+ /* categoria operatore unario */\r
+ else if(!strcmp(bufToken,"MOP"))\r
+ {\r
+ if(!strcmp(bufString,"!"))\r
+ { return(UnaryOperator_NOT); }\r
+ else if(!strcmp(bufString,"~"))\r
+ { return(UnaryOperator_COM); }\r
+ else\r
+ {\r
+ printf("%s\tParsError:UnknownUnaryOperatorToken(%s)%c",bufLine,bufString,SEP);\r
+ exit(ERR_EXIT);\r
+ }\r
+ }\r
+ /* categoria operatore duale */\r
+ else if(!strcmp(bufToken,"DOP"))\r
+ {\r
+ if(!strcmp(bufString,"&"))\r
+ { return(DualOperator_AND); }\r
+ else if(!strcmp(bufString,"|"))\r
+ { return(DualOperator_OR); }\r
+ else if(!strcmp(bufString,"^"))\r
+ { return(DualOperator_XOR); }\r
+ else if(!strcmp(bufString,"+"))\r
+ { return(DualOperator_ADD); }\r
+ else if(!strcmp(bufString,"-"))\r
+ { return(DualOperator_SUB); }\r
+ else if(!strcmp(bufString,"*"))\r
+ { return(DualOperator_MUL); }\r
+ else if(!strcmp(bufString,"/"))\r
+ { return(DualOperator_DIV); }\r
+ else if(!strcmp(bufString,"!="))\r
+ { return(DualOperator_NEQ); }\r
+ else if(!strcmp(bufString,"=="))\r
+ { return(DualOperator_EQ); }\r
+ else if(!strcmp(bufString,"<"))\r
+ { return(DualOperator_LT); }\r
+ else if(!strcmp(bufString,">"))\r
+ { return(DualOperator_GT); }\r
+ else if(!strcmp(bufString,"<="))\r
+ { return(DualOperator_LTE); }\r
+ else if(!strcmp(bufString,">="))\r
+ { return(DualOperator_GTE); }\r
+ else if(!strcmp(bufString,">>"))\r
+ { return(DualOperator_SHR); }\r
+ else if(!strcmp(bufString,"<<"))\r
+ { return(DualOperator_SHL); }\r
+ else if(!strcmp(bufString,"="))\r
+ { return(DualOperator_ASG); }\r
+ else\r
+ {\r
+ printf("%s\tParsError:UnknownDualOperatorToken(%s)%c",bufLine,bufString,SEP);\r
+ exit(ERR_EXIT);\r
+ }\r
+ }\r
+ /* categoria separatore */\r
+ else if(!strcmp(bufToken,"SEP"))\r
+ {\r
+ if(!strcmp(bufString,";"))\r
+ { return(Separator_COLUMN); }\r
+ else if(!strcmp(bufString,"."))\r
+ { return(Separator_SELECT); }\r
+ else if(!strcmp(bufString,","))\r
+ { return(Separator_COMMA); }\r
+ else\r
+ {\r
+ printf("%s\tParsError:UnknownSeparatorToken(%s)%c",bufLine,bufString,SEP);\r
+ exit(ERR_EXIT);\r
+ }\r
+ }\r
+ /* categoria dword */\r
+ else if(!strcmp(bufToken,"W32"))\r
+ {\r
+ unsigned long int tmpI;\r
+\r
+ if(sscanf(bufString,"0x%lx",&tmpI)==1)\r
+ {\r
+ *lvalp=treeDwordAlloc((_DWORD_) (tmpI&0xFFFFFFFF),llocp);\r
+ return(Value_DWORD);\r
+ }\r
+ else\r
+ {\r
+ printf("%s\tParsError:UnknownIntToken(%s)%c",bufLine,bufString,SEP);\r
+ exit(ERR_EXIT);\r
+ }\r
+ }\r
+ /* categoria word */\r
+ else if(!strcmp(bufToken,"W16"))\r
+ {\r
+ unsigned long int tmpI;\r
+\r
+ if(sscanf(bufString,"0x%lx",&tmpI)==1)\r
+ {\r
+ *lvalp=treeWordAlloc((_WORD_) (tmpI&0xFFFF),llocp);\r
+ return(Value_WORD);\r
+ }\r
+ else\r
+ {\r
+ printf("%s\tParsError:UnknownIntToken(%s)%c",bufLine,bufString,SEP);\r
+ exit(ERR_EXIT);\r
+ }\r
+ }\r
+ /* categoria byte */\r
+ else if(!strcmp(bufToken,"BY8"))\r
+ {\r
+ unsigned long int tmpI;\r
+\r
+ if(sscanf(bufString,"0x%lx",&tmpI)==1)\r
+ {\r
+ *lvalp=treeByteAlloc((_BYTE_) (tmpI&0xFF),llocp);\r
+ return(Value_BYTE);\r
+ }\r
+ else\r
+ {\r
+ printf("%s\tParsError:UnknownIntToken(%s)%c",bufLine,bufString,SEP);\r
+ exit(ERR_EXIT);\r
+ }\r
+ }\r
+ /* categoria numero intero */\r
+ else if(!strcmp(bufToken,"INU"))\r
+ {\r
+ unsigned long int tmpI;\r
+\r
+ if(sscanf(bufString,"%lu",&tmpI)==1)\r
+ {\r
+ *lvalp=treeInumAlloc(tmpI,llocp);\r
+ return(Value_INUM);\r
+ }\r
+ else\r
+ {\r
+ printf("%s\tParsError:UnknownIntToken(%s)%c",bufLine,bufString,SEP);\r
+ exit(ERR_EXIT);\r
+ }\r
+ }\r
+ /* categoria ID == stringa */\r
+ else if(!strcmp(bufToken,"IID"))\r
+ {\r
+ char *tmpS;\r
+\r
+ if(!(tmpS=(char *) myMalloc(strlen(bufString)+1)))\r
+ {\r
+ printf("[%s:%d] ParsError:OutOfMemory%c",__FILE__,__LINE__,SEP);\r
+ exit(ERR_EXIT);\r
+ }\r
+\r
+ *tmpS=0;\r
+ strcpy(tmpS,bufString);\r
+\r
+ *lvalp=treeStringAlloc(tmpS,llocp);\r
+ return(Value_STRING);\r
+ }\r
+ else\r
+ {\r
+ printf("%s\tParsError:UnknownToken(%s)%c",bufLine,bufString,SEP);\r
+ exit(ERR_EXIT);\r
+ }\r
+}\r
+\r
+void usage(char *name)\r
+{\r
+ printf("\nC Parser - Cosimo Oliboni\n\n");\r
+ printf("\"%s\" parsing da stdin\n\n",name);\r
+\r
+ return;\r
+}\r
+\r
+void prettyPrintString(char *str)\r
+{\r
+ printf("([");\r
+\r
+ while(*str)\r
+ {\r
+ printf("ch_%c",*str);\r
+\r
+ if(*(str+1))\r
+ { printf(";"); }\r
+\r
+ str++;\r
+ }\r
+\r
+ printf("])");\r
+}\r
+\r
+void prettyPrintByte(_BYTE_ num)\r
+{\r
+ printf("(\\langle x%1X,x%1X \\rangle)",(unsigned int) (num>>4),(unsigned int) (num&0xF));\r
+}\r
+\r
+void prettyPrintWord(_WORD_ num)\r
+{\r
+ printf("(\\langle \\langle x%1X,x%1X \\rangle : \\langle x%1X,x%1X \\rangle \\rangle)",\\r
+ (unsigned int) (num>>12),(unsigned int) ((num>>8)&0xF),\\r
+ (unsigned int) ((num>>4)&0xF),(unsigned int) (num&0xF));\r
+}\r
+\r
+void prettyPrintDword(_DWORD_ num)\r
+{\r
+ printf("(\\langle \\langle \\langle x%1X,x%1X \\rangle : \\langle x%1X,x%1X \\rangle \\rangle . \\langle \\langle x%1X,x%1X \\rangle : \\langle x%1X,x%1X \\rangle \\rangle \\rangle)",\\r
+ (unsigned int) (num>>28),(unsigned int) ((num>>24)&0xF),\\r
+ (unsigned int) ((num>>20)&0xF),(unsigned int) ((num>>16)&0xF),\\r
+ (unsigned int) ((num>>12)&0xF),(unsigned int) ((num>>8)&0xF),\\r
+ (unsigned int) ((num>>4)&0xF),(unsigned int) (num&0xF));\r
+}\r
+\r
+void prettyPrintIndent(int indent)\r
+{\r
+ int index;\r
+\r
+ for(index=0;index<indent;index++)\r
+ { printf(" "); }\r
+}\r
+\r
+void prettyPrintInum(unsigned long int num)\r
+{\r
+ if(!num)\r
+ { printf("O\n"); }\r
+ else\r
+ { printf("%lu\n",num); }\r
+}\r
+\r
+void prettyPrintInit(valueLeaf *cur,int indent)\r
+{\r
+ if(cur->id==E_VALUE_BYTE)\r
+ { prettyPrintIndent(indent); printf("(PREAST_INIT_VAL_BYTE8 "); prettyPrintByte(cur->byteValue); printf(")\n"); }\r
+ else if(cur->id==E_VALUE_WORD)\r
+ { prettyPrintIndent(indent); printf("(PREAST_INIT_VAL_WORD16 "); prettyPrintWord(cur->wordValue); printf(")\n"); }\r
+ else if(cur->id==E_VALUE_DWORD)\r
+ { prettyPrintIndent(indent); printf("(PREAST_INIT_VAL_WORD32 "); prettyPrintDword(cur->dwordValue); printf(")\n"); }\r
+ else if(cur->id==E_VALUE_ARRAY)\r
+ {\r
+ valueLeaf *tmp=cur->param1;\r
+\r
+ prettyPrintIndent(indent); printf("(PREAST_INIT_VAL_ARRAY\n");\r
+\r
+ prettyPrintIndent(indent+1); printf("(\\laquo\n");\r
+ if(!tmp->next)\r
+ { prettyPrintIndent(indent+2); printf("£\n"); }\r
+ while(tmp)\r
+ {\r
+ prettyPrintInit(tmp,indent+2);\r
+\r
+ if(tmp->next)\r
+ {\r
+ if(tmp->next->next)\r
+ { prettyPrintIndent(indent+2); printf(";\n"); }\r
+ else\r
+ { prettyPrintIndent(indent+2); printf("£\n"); }\r
+ }\r
+\r
+ tmp=tmp->next;\r
+ }\r
+ prettyPrintIndent(indent+1); printf("\\raquo)\n");\r
+\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_VALUE_STRUCT)\r
+ {\r
+ valueLeaf *tmp=cur->param1;\r
+\r
+ prettyPrintIndent(indent); printf("(PREAST_INIT_VAL_STRUCT\n");\r
+\r
+ prettyPrintIndent(indent+1); printf("(\\laquo\n");\r
+ if(!tmp->next)\r
+ { prettyPrintIndent(indent+2); printf("£\n"); }\r
+ while(tmp)\r
+ {\r
+ prettyPrintInit(tmp,indent+2);\r
+\r
+ if(tmp->next)\r
+ {\r
+ if(tmp->next->next)\r
+ { prettyPrintIndent(indent+2); printf(";\n"); }\r
+ else\r
+ { prettyPrintIndent(indent+2); printf("£\n"); }\r
+ }\r
+\r
+ tmp=tmp->next;\r
+ }\r
+ prettyPrintIndent(indent+1); printf("\\raquo)\n");\r
+\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+}\r
+\r
+void prettyPrintBody(valueLeaf *cur,int indent)\r
+{\r
+ if(!cur)\r
+ { prettyPrintIndent(indent); printf("(PREAST_NO_DECL [])\n"); }\r
+ else if((cur->id==E_DECL_CONST)||(cur->id==E_DECL_VAR))\r
+ { prettyPrint(cur,indent); }\r
+ else\r
+ {\r
+ valueLeaf *tmp=cur;\r
+\r
+ prettyPrintIndent(indent); printf("(PREAST_NO_DECL\n");\r
+\r
+ prettyPrintIndent(indent+1); printf("[\n");\r
+ while(tmp)\r
+ {\r
+ prettyPrint(tmp,indent+2);\r
+\r
+ if(tmp->next)\r
+ { prettyPrintIndent(indent+2); printf(";\n"); }\r
+\r
+ tmp=tmp->next;\r
+ }\r
+ prettyPrintIndent(indent+1); printf("]\n");\r
+\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+\r
+}\r
+\r
+void prettyPrint(valueLeaf *cur,int indent)\r
+{\r
+ /* ** misc ** */\r
+ if(cur->id==E_VALUE_STRING)\r
+ { prettyPrintIndent(indent); prettyPrintString(cur->stringValue); printf("\n"); }\r
+ else if(cur->id==E_VALUE_INUM)\r
+ { prettyPrintIndent(indent); prettyPrintInum(cur->inumValue); }\r
+\r
+ /* ** tipi ** */\r
+ else if(cur->id==E_TYPE_BYTE)\r
+ { prettyPrintIndent(indent); printf("(AST_TYPE_BASE AST_BASE_TYPE_BYTE8)\n"); }\r
+ else if(cur->id==E_TYPE_WORD)\r
+ { prettyPrintIndent(indent); printf("(AST_TYPE_BASE AST_BASE_TYPE_WORD16)\n"); }\r
+ else if(cur->id==E_TYPE_DWORD)\r
+ { prettyPrintIndent(indent); printf("(AST_TYPE_BASE AST_BASE_TYPE_WORD32)\n"); }\r
+ else if(cur->id==E_TYPE_ARRAY)\r
+ {\r
+ prettyPrintIndent(indent); printf("(AST_TYPE_ARRAY\n");\r
+ prettyPrint(cur->param2,indent+1); //type\r
+ prettyPrint(cur->param1,indent+1); //size\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_TYPE_STRUCT)\r
+ {\r
+ valueLeaf *tmp=cur->param1;\r
+\r
+ prettyPrintIndent(indent); printf("(AST_TYPE_STRUCT\n");\r
+\r
+ prettyPrintIndent(indent+1); printf("(\\laquo\n");\r
+ if(!tmp->next)\r
+ { prettyPrintIndent(indent+2); printf("£\n"); }\r
+ while(tmp)\r
+ {\r
+ prettyPrint(tmp,indent+2);\r
+\r
+ if(tmp->next)\r
+ {\r
+ if(tmp->next->next)\r
+ { prettyPrintIndent(indent+2); printf(";\n"); }\r
+ else\r
+ { prettyPrintIndent(indent+2); printf("£\n"); }\r
+ }\r
+\r
+ tmp=tmp->next;\r
+ }\r
+ prettyPrintIndent(indent+1); printf("\\raquo)\n");\r
+\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ \r
+ /* ** valori come espressioni ** */\r
+ else if(cur->id==E_VALUE_BYTE)\r
+ { prettyPrintIndent(indent); printf("(PREAST_EXPR_BYTE8 "); prettyPrintByte(cur->byteValue); printf(")\n"); }\r
+ else if(cur->id==E_VALUE_WORD)\r
+ { prettyPrintIndent(indent); printf("(PREAST_EXPR_WORD16 "); prettyPrintWord(cur->wordValue); printf(")\n"); }\r
+ else if(cur->id==E_VALUE_DWORD)\r
+ { prettyPrintIndent(indent); printf("(PREAST_EXPR_WORD32 "); prettyPrintDword(cur->dwordValue); printf(")\n"); }\r
+\r
+ /* ** variabili */\r
+ else if(cur->id==E_VAR_ROOT)\r
+ { prettyPrintIndent(indent); printf("(PREAST_VAR_ID "); prettyPrintString(cur->param1->stringValue); printf(")\n"); }\r
+ else if(cur->id==E_VAR_STRUCT)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_VAR_STRUCT\n");\r
+ prettyPrint(cur->next,indent+1);\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_VAR_ARRAY)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_VAR_ARRAY\n");\r
+ prettyPrint(cur->next,indent+1);\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+\r
+ /* ** espressioni ** */\r
+ else if(cur->id==E_EXPR_VAR)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_ID\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_NOT)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_NOT\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_COM)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_COM\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_NEG)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_NEG\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_B2W)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_B8toW16\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_B2DW)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_B8toW32\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_W2B)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_W16toB8\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_W2DW)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_W16toW32\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_DW2B)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_W32toB8\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_DW2W)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_W32toW16\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_ADD)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_ADD\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrint(cur->param2,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_SUB)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_SUB\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrint(cur->param2,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_MUL)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_MUL\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrint(cur->param2,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_DIV)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_DIV\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrint(cur->param2,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_SHL)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_SHL\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrint(cur->param2,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_SHR)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_SHR\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrint(cur->param2,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_AND)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_AND\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrint(cur->param2,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_OR)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_OR\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrint(cur->param2,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_XOR)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_XOR\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrint(cur->param2,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_LT)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_LT\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrint(cur->param2,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_LTE)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_LTE\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrint(cur->param2,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_GT)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_GT\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrint(cur->param2,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_GTE)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_GTE\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrint(cur->param2,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_EQ)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_EQ\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrint(cur->param2,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_EXPR_NEQ)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_EXPR_NEQ\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrint(cur->param2,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+\r
+ /* ** dichiarazioni ** */\r
+ else if(cur->id==E_DECL_CONST)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_CONST_DECL\n");\r
+ prettyPrint(cur->param2,indent+1); //string\r
+ prettyPrint(cur->param1,indent+1); //type\r
+ \r
+ if((cur->param3->id==E_VAR_ROOT)||(cur->param3->id==E_VAR_STRUCT)||(cur->param3->id==E_VAR_ARRAY))\r
+ {\r
+ prettyPrintIndent(indent+1); printf("(PREAST_INIT_VAR\n");\r
+ prettyPrint(cur->param3,indent+2); //init\r
+ prettyPrintIndent(indent+1); printf(")\n");\r
+ }\r
+ else\r
+ {\r
+ prettyPrintIndent(indent+1); printf("(PREAST_INIT_VAL\n");\r
+ prettyPrintInit(cur->param3,indent+2); //init\r
+ prettyPrintIndent(indent+1); printf(")\n");\r
+ }\r
+\r
+ if(cur->next)\r
+ {\r
+ if((cur->next->id==E_DECL_CONST)||(cur->next->id==E_DECL_VAR))\r
+ { prettyPrint(cur->next,indent+1); } //next decl\r
+ else\r
+ {\r
+ valueLeaf *tmp=cur->next;\r
+\r
+ prettyPrintIndent(indent+1); printf("(PREAST_NO_DECL\n");\r
+\r
+ prettyPrintIndent(indent+2); printf("[\n");\r
+ while(tmp)\r
+ {\r
+ prettyPrint(tmp,indent+3);\r
+\r
+ if(tmp->next)\r
+ { prettyPrintIndent(indent+3); printf(";\n"); }\r
+\r
+ tmp=tmp->next;\r
+ }\r
+ prettyPrintIndent(indent+2); printf("]\n");\r
+\r
+ prettyPrintIndent(indent+1); printf(")\n");\r
+ }\r
+ }\r
+ else\r
+ { prettyPrintIndent(indent+1); printf("(PREAST_NO_DECL [])\n"); }\r
+ \r
+\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_DECL_VAR)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_VAR_DECL\n");\r
+ prettyPrint(cur->param2,indent+1); //string\r
+ prettyPrint(cur->param1,indent+1); //type\r
+ \r
+ if(cur->param3)\r
+ {\r
+ prettyPrintIndent(indent+1); printf("(Some ?\n");\r
+\r
+ if((cur->param3->id==E_VAR_ROOT)||(cur->param3->id==E_VAR_STRUCT)||(cur->param3->id==E_VAR_ARRAY))\r
+ {\r
+ prettyPrintIndent(indent+2); printf("(PREAST_INIT_VAR\n");\r
+ prettyPrint(cur->param3,indent+3); //init\r
+ prettyPrintIndent(indent+2); printf(")\n");\r
+ }\r
+ else\r
+ {\r
+ prettyPrintIndent(indent+2); printf("(PREAST_INIT_VAL\n");\r
+ prettyPrintInit(cur->param3,indent+3); //init\r
+ prettyPrintIndent(indent+2); printf(")\n");\r
+ }\r
+\r
+ prettyPrintIndent(indent+1); printf(")\n");\r
+ }\r
+ else\r
+ { prettyPrintIndent(indent+1); printf("(None ?)\n"); }\r
+\r
+ if(cur->next)\r
+ {\r
+ if((cur->next->id==E_DECL_CONST)||(cur->next->id==E_DECL_VAR))\r
+ { prettyPrint(cur->next,indent+1); } //next decl\r
+ else\r
+ {\r
+ valueLeaf *tmp=cur->next;\r
+\r
+ prettyPrintIndent(indent+1); printf("(PREAST_NO_DECL\n");\r
+\r
+ prettyPrintIndent(indent+2); printf("[\n");\r
+ while(tmp)\r
+ {\r
+ prettyPrint(tmp,indent+3);\r
+\r
+ if(tmp->next)\r
+ { prettyPrintIndent(indent+3); printf(";\n"); }\r
+\r
+ tmp=tmp->next;\r
+ }\r
+ prettyPrintIndent(indent+2); printf("]\n");\r
+\r
+ prettyPrintIndent(indent+1); printf(")\n");\r
+ }\r
+ }\r
+ else\r
+ { prettyPrintIndent(indent+1); printf("(PREAST_NO_DECL [])\n"); }\r
+ \r
+\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+\r
+ /* ** statement ** */\r
+ else if(cur->id==E_STM_ASG)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_STM_ASG\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrint(cur->param2,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_STM_WHILE)\r
+ {\r
+ prettyPrintIndent(indent); printf("(PREAST_STM_WHILE\n");\r
+ prettyPrint(cur->param1,indent+1);\r
+ prettyPrintBody(cur->param2,indent+1);\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+ else if(cur->id==E_STM_IF)\r
+ {\r
+ valueLeaf *tmp=cur->param1;\r
+\r
+ prettyPrintIndent(indent); printf("(PREAST_STM_IF\n");\r
+\r
+ prettyPrintIndent(indent+1); printf("(\\laquo\n");\r
+ if(!tmp->next)\r
+ { prettyPrintIndent(indent+2); printf("£\n"); }\r
+ while(tmp)\r
+ {\r
+ prettyPrintIndent(indent+2); printf("(pair ??\n");\r
+ prettyPrint(tmp->param1,indent+3);\r
+ prettyPrintBody(tmp->param2,indent+3);\r
+ prettyPrintIndent(indent+2); printf(")\n");\r
+\r
+ if(tmp->next)\r
+ {\r
+ if(tmp->next->next)\r
+ { prettyPrintIndent(indent+2); printf(";\n"); }\r
+ else\r
+ { prettyPrintIndent(indent+2); printf("£\n"); }\r
+ }\r
+\r
+ tmp=tmp->next;\r
+ }\r
+ prettyPrintIndent(indent+1); printf("\\raquo)\n");\r
+\r
+ // equivalenza: Some ? (PREAST_NO_DECL []) = None ?\r
+ if(cur->param2)\r
+ {\r
+ prettyPrintIndent(indent+1); printf("(Some ?\n");\r
+ prettyPrintBody(cur->param2,indent+2); //body\r
+ prettyPrintIndent(indent+1); printf(")\n");\r
+ }\r
+ else\r
+ { prettyPrintIndent(indent+1); printf("(None ?)\n"); }\r
+\r
+ prettyPrintIndent(indent); printf(")\n");\r
+ }\r
+}\r
+\r
+int main(int argc,char **argv)\r
+{\r
+ int ret;\r
+\r
+ /* ignora eventuali errori di parametri */\r
+ if(argc>1)\r
+ { usage(argv[0]); }\r
+\r
+ ret=yyparse(NULL);\r
+\r
+ if((!ret)&&(!numErr))\r
+ {\r
+ printf("definition parsingResult \\def\n");\r
+ printf(" (PREAST_ROOT\n");\r
+ prettyPrint(&root,2);\r
+ printf(" ).");\r
+ }\r
+\r
+ return(ret);\r
+}\r
--- /dev/null
+/* ******************************** */\r
+/* PER UNA CORRETTA VISUALIZZAZIONE */\r
+/* tab size=4 */\r
+/* ******************************** */\r
+\r
+/* - stati del preprocessore */\r
+%x defbegin\r
+%x defend\r
+\r
+/* - discard dei commenti / * ... * / */\r
+%x combegin\r
+\r
+%{\r
+\r
+/* ************************************************************************* */\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+\r
+/* ************************************************************************* */\r
+\r
+#define MAX_DEPTH 1000 /* numero massimo di annidamenti */\r
+\r
+#define ERR_EXIT 0 /* uscita ok */\r
+#define OK_EXIT 1 /* uscita errore */\r
+\r
+#define SEP '\n' /* separatore dei campi in output */\r
+\r
+#define EXP_OPT "-e" /* opzione di solo preprocessing */\r
+\r
+/* stringhe di errore */\r
+#define ERR_UNK "ScanError:UnknownToken"\r
+#define ERR_OPT "ScanError:UnknownOption"\r
+#define ERR_NL "ScanError:UnexptedNewline"\r
+#define ERR_STR "ScanError:EmptyString"\r
+#define ERR_CHR "ScanError:EmptyChar"\r
+#define ERR_ALR "ScanError:RepeatedDefine"\r
+#define ERR_MANYCHR "ScanError:TooManyChars"\r
+#define ERR_INC "ScanError:TooManyExpansionsOrNestings"\r
+#define ERR_MIS "ScanError:MissingIncludeFile"\r
+#define ERR_MEM "ScanError:OutOfMemory"\r
+#define ERR_ARG "ScanError:IncorrectArgumentNumber"\r
+#define ERR_OPEN "ScanError:FileNotFound"\r
+#define ERR_UNA "ScanError:UnallowedExpansion"\r
+#define ERR_UNE "ScanError:Unexpected(#endif)"\r
+#define ERR_UNS "ScanError:Unexpected(#switch)"\r
+#define ERR_END "ScanError:Missing(#endif)"\r
+#define ERR_LOP "ScanError:ExpansionLoopDetected"\r
+#define ERR_RES "ScanError:ReservedToken"\r
+\r
+/* ************************************************************************* */\r
+\r
+/* elemento lista dei token */\r
+typedef struct listElem\r
+ {\r
+ char *scanTxt;\r
+ struct listElem *next;\r
+ } listELEM;\r
+\r
+/* elemento lista delle espansioni */\r
+typedef struct defElem\r
+ {\r
+ char *defId;\r
+ char *defTxt;\r
+ struct defElem *next;\r
+ } defELEM;\r
+\r
+/* ************************************************************************* */\r
+\r
+YY_BUFFER_STATE inStream[MAX_DEPTH]; /* buffer: input stream buffer */\r
+int inNesting=0; /* contatore: livello di annidamento */\r
+char inName[MAX_DEPTH][257]; /* buffer: nomi dei sorgenti */\r
+int inLine[MAX_DEPTH]; /* buffer: linea corrente */\r
+int inRow[MAX_DEPTH]; /* buffer: colonna corrente */\r
+\r
+listELEM *rootElem=NULL,*curElem=NULL; /* lista dei token */\r
+defELEM *rootDef=NULL,*curDef=NULL; /* lista delle espansioni */\r
+\r
+int stringFound; /* flag: stringa non vuota */\r
+int charFound; /* flag: carattere non vuoto */\r
+int expand=0; /* flag: espansione in corso */\r
+int startExpansion; /* variabile: espansione iniziale (per evitare loop) */\r
+int eatTrueEndif=0; /* contatore: #endif validi (a seguito di true) */\r
+int eatFalseEndif=0; /* contatore: #endif validi (a seguito di false) */\r
+int expOnly=0; /* flag: preprocessare senza generare token */\r
+\r
+char tempBuf[2000]; /* spazio temporaneo per formattare l'output/error */\r
+\r
+/* espansioni riservate */\r
+char *reserved[]={\r
+ "array",\r
+ "of",\r
+ "struct",\r
+ "if",\r
+ "elsif",\r
+ "else",\r
+ "while",\r
+ "const",\r
+ "b2w",\r
+ "b2dw",\r
+ "w2b",\r
+ "w2dw",\r
+ "dw2b",\r
+ "dw2w",\r
+ "byte",\r
+ "word",\r
+ "dword",\r
+ NULL };\r
+\r
+/* ************************************************************************* */\r
+\r
+void addOkBuf(char *token,char *ytext);\r
+void errExit(char *ytext);\r
+void addDefId(char *ytext);\r
+int checkDefId(char *ytext);\r
+void checkRsvd(char *ytext);\r
+void delDefId(char *ytext);\r
+void addDefTxt(char *ytext);\r
+void usage(char *name);\r
+\r
+/* ************************************************************************* */\r
+\r
+%}\r
+\r
+KEYWORD ("array"|"of"|"struct"|"if"|"elsif"|"else"|"while"|"const"|"b2w"|"b2dw"|"w2b"|"w2dw"|"dw2b"|"dw2w")\r
+TYPE ("byte"|"word"|"dword")\r
+BRACKET ("{"|"}"|"["|"]"|"("|")")\r
+MONOOP ("!"|"~")\r
+DUALOP ("&"|"|"|"^"|"+"|"-"|"*"|"/"|"!="|"=="|"<"|">"|"<="|">="|"<<"|">>"|"=")\r
+SEPARATOR (";"|"."|",")\r
+\r
+IDWORD ("0x"[a-fA-F0-9][a-fA-F0-9][a-fA-F0-9][a-fA-F0-9][a-fA-F0-9][a-fA-F0-9][a-fA-F0-9][a-fA-F0-9])\r
+IWORD ("0x"[a-fA-F0-9][a-fA-F0-9][a-fA-F0-9][a-fA-F0-9])\r
+IBYTE ("0x"[a-fA-F0-9][a-fA-F0-9])\r
+INUM ([0-9]+)\r
+ID ([_a-zA-Z][_a-zA-Z0-9]*)\r
+\r
+BLANK ([ \t])\r
+NL (\n|\r\n)\r
+\r
+/* ************************************************************************* */\r
+\r
+%%\r
+\r
+<INITIAL>"#define" {\r
+ /* ************** */\r
+ /* DEFINIZIONE ID */\r
+ /* ************** */\r
+\r
+ if(expand)\r
+ { sprintf(tempBuf,"%s",ERR_UNA); errExit(tempBuf); exit(ERR_EXIT); }\r
+\r
+ inRow[inNesting]+=yyleng;\r
+ BEGIN(defbegin);\r
+ }\r
+<defbegin>{BLANK}+ { inRow[inNesting]+=yyleng; }\r
+<defbegin>{ID} { checkRsvd(yytext); addDefId(yytext); inRow[inNesting]+=yyleng; BEGIN(defend); }\r
+<defbegin>{NL} { sprintf(tempBuf,"%s",ERR_NL); errExit(tempBuf); exit(ERR_EXIT); }\r
+<defbegin>. { sprintf(tempBuf,"%s(%s)",ERR_UNK,yytext); errExit(tempBuf); exit(ERR_EXIT); } \r
+<defend>[^\r\n]+ { addDefTxt(yytext); inRow[inNesting]+=yyleng; }\r
+<defend>{NL} { inLine[inNesting]++; inRow[inNesting]=1; BEGIN(INITIAL); }\r
+\r
+\r
+\r
+<INITIAL>"/*" {\r
+ /* *************** */\r
+ /* INIZIO COMMENTO */\r
+ /* *************** */\r
+ \r
+ if(!expand)\r
+ { inRow[inNesting]+=yyleng; }\r
+ \r
+ BEGIN(combegin);\r
+ }\r
+<combegin>{NL} {\r
+ if(expand)\r
+ { sprintf(tempBuf,"%s",ERR_NL); errExit(tempBuf); exit(ERR_EXIT); }\r
+\r
+ inLine[inNesting]++;\r
+ inRow[inNesting]=1;\r
+ }\r
+<combegin>[^\r\n] {\r
+ if(!expand)\r
+ { inRow[inNesting]+=yyleng; }\r
+ }\r
+<combegin>"*/" {\r
+ if(!expand)\r
+ { inRow[inNesting]+=yyleng; }\r
+ \r
+ BEGIN(INITIAL);\r
+ }\r
+\r
+\r
+\r
+<INITIAL>{KEYWORD} {\r
+ /* ******************** */\r
+ /* TOKEN DEL LINGUAGGIO */\r
+ /* ******************** */\r
+\r
+ addOkBuf("KEW",yytext);\r
+ \r
+ if(!expand)\r
+ { inRow[inNesting]+=yyleng; }\r
+ }\r
+<INITIAL>{TYPE} {\r
+ addOkBuf("TYP",yytext);\r
+ \r
+ if(!expand)\r
+ { inRow[inNesting]+=yyleng; }\r
+ }\r
+<INITIAL>{BRACKET} {\r
+ addOkBuf("BRK",yytext);\r
+ \r
+ if(!expand)\r
+ { inRow[inNesting]+=yyleng; }\r
+ }\r
+<INITIAL>{MONOOP} {\r
+ addOkBuf("MOP",yytext);\r
+ \r
+ if(!expand)\r
+ { inRow[inNesting]+=yyleng; }\r
+ }\r
+<INITIAL>{DUALOP} {\r
+ addOkBuf("DOP",yytext);\r
+ \r
+ if(!expand)\r
+ { inRow[inNesting]+=yyleng; }\r
+ }\r
+<INITIAL>{SEPARATOR} {\r
+ addOkBuf("SEP",yytext);\r
+ \r
+ if(!expand)\r
+ { inRow[inNesting]+=yyleng; }\r
+ }\r
+<INITIAL>{IDWORD} {\r
+ addOkBuf("W32",yytext);\r
+ \r
+ if(!expand)\r
+ { inRow[inNesting]+=yyleng; }\r
+ }\r
+<INITIAL>{IWORD} {\r
+ addOkBuf("W16",yytext);\r
+ \r
+ if(!expand)\r
+ { inRow[inNesting]+=yyleng; }\r
+ }\r
+<INITIAL>{IBYTE} {\r
+ addOkBuf("BY8",yytext);\r
+ \r
+ if(!expand)\r
+ { inRow[inNesting]+=yyleng; }\r
+ }\r
+<INITIAL>{INUM} {\r
+ addOkBuf("INU",yytext);\r
+ \r
+ if(!expand)\r
+ { inRow[inNesting]+=yyleng; }\r
+ }\r
+\r
+\r
+\r
+<INITIAL>{ID} {\r
+ /* ********************* */\r
+ /* TOKEN ID O ESPANSIONE */\r
+ /* ********************* */\r
+\r
+ defELEM *tmp;\r
+ int found=0;\r
+ int index;\r
+\r
+ for(index=0,tmp=rootDef;tmp;tmp=tmp->next,index++)\r
+ {\r
+ if(!strcmp(tmp->defId,yytext))\r
+ {\r
+ found=1;\r
+\r
+ /* Prima espansione */\r
+ if(!expand)\r
+ {\r
+ startExpansion=index;\r
+ break;\r
+ }\r
+ /* Espansione dentro un'espansione */\r
+ else if(startExpansion!=index)\r
+ { break; }\r
+ /* Loop detected */\r
+ else\r
+ { sprintf(tempBuf,"%s",ERR_LOP); errExit(tempBuf); exit(ERR_EXIT); }\r
+ }\r
+ }\r
+\r
+ /* espandi */\r
+ if(found)\r
+ {\r
+ expand++;\r
+\r
+ if(inNesting>=MAX_DEPTH)\r
+ { sprintf(tempBuf,"%s",ERR_INC); errExit(tempBuf); exit(ERR_EXIT); }\r
+\r
+ inStream[inNesting++]=YY_CURRENT_BUFFER;\r
+ inLine[inNesting]=inLine[inNesting-1];\r
+ inRow[inNesting]=inRow[inNesting-1];\r
+ if(expand<2)\r
+ { inRow[inNesting-1]+=yyleng; }\r
+ strcpy(inName[inNesting],inName[inNesting-1]);\r
+ \r
+ /* cambio di input stream -> espansione */\r
+ if(tmp->defTxt)\r
+ { yy_scan_string(tmp->defTxt); }\r
+ else\r
+ { yy_scan_string(""); }\r
+ }\r
+ /* genera token */\r
+ else\r
+ {\r
+ addOkBuf("IID",yytext);\r
+\r
+ if(!expand)\r
+ { inRow[inNesting]+=yyleng; }\r
+ }\r
+ }\r
+\r
+\r
+\r
+<INITIAL>{BLANK}+ {\r
+ /* **************** */\r
+ /* NEW LINE E BLANK */\r
+ /* **************** */\r
+\r
+ if(expOnly)\r
+ { printf("%s",yytext); }\r
+ if(!expand)\r
+ { inRow[inNesting]+=yyleng; }\r
+ }\r
+<INITIAL>{NL} {\r
+ if(expOnly)\r
+ { printf("%s",yytext); }\r
+ if(expand)\r
+ { sprintf(tempBuf,"%s",ERR_NL); errExit(tempBuf); exit(ERR_EXIT); }\r
+\r
+ inLine[inNesting]++;\r
+ inRow[inNesting]=1;\r
+ }\r
+\r
+\r
+\r
+<INITIAL,defend><<EOF>> {\r
+ /* **************************************** */\r
+ /* EOF = FINE DI UN'ESPANSIONE O INCLUSIONE */\r
+ /* **************************************** */\r
+\r
+ if((--inNesting)<0)\r
+ {\r
+ if(eatTrueEndif||eatFalseEndif)\r
+ { ++inNesting; sprintf(tempBuf,"%s",ERR_END); errExit(tempBuf); exit(ERR_EXIT); }\r
+\r
+ yyterminate();\r
+ }\r
+ else\r
+ {\r
+ yy_delete_buffer(YY_CURRENT_BUFFER);\r
+ yy_switch_to_buffer(inStream[inNesting]);\r
+ }\r
+\r
+ if(expand)\r
+ { expand--; }\r
+ }\r
+\r
+\r
+\r
+<INITIAL>. {\r
+ /* ************************** */\r
+ /* CARATTERE NON RICONOSCIUTO */\r
+ /* ************************** */\r
+ \r
+ sprintf(tempBuf,"%s(%s)",ERR_UNK,yytext);\r
+ errExit(tempBuf);\r
+ exit(ERR_EXIT);\r
+ } \r
+\r
+%%\r
+\r
+/* ************************************************************************* */\r
+\r
+/* formattazione dell'errore ed uscita */\r
+void errExit(char *ytext)\r
+{\r
+ printf("%s:%d,%d\tERR\t%s\t%c",inName[inNesting],inLine[inNesting],inRow[inNesting],ytext,SEP);\r
+}\r
+\r
+/* ************************************************************************* */\r
+\r
+/* controllo se ID e' gia' stato definito come espansione */\r
+int checkDefId(char *ytext)\r
+{\r
+ defELEM *tmp;\r
+\r
+ for(tmp=rootDef;tmp;tmp=tmp->next)\r
+ {\r
+ if(!strcmp(tmp->defId,ytext))\r
+ { return(1); }\r
+ }\r
+\r
+ return(0);\r
+}\r
+\r
+/* ************************************************************************* */\r
+\r
+/* controllo se ID corrisponde ad un elemento di reserved[] */\r
+void checkRsvd(char *ytext)\r
+{\r
+ int index;\r
+\r
+ for(index=0;reserved[index];index++)\r
+ {\r
+ if(!strcmp(reserved[index],ytext))\r
+ { sprintf(tempBuf,"%s(%s)",ERR_RES,yytext); errExit(tempBuf); exit(ERR_EXIT);}\r
+ }\r
+\r
+ return;\r
+}\r
+\r
+/* ************************************************************************* */\r
+\r
+/* allocazione di ID come nuova espansione */\r
+void addDefId(char *ytext)\r
+{\r
+ if(!rootDef)\r
+ { \r
+ if(!(curDef=rootDef=(defELEM *) malloc(sizeof(defELEM))))\r
+ { sprintf(tempBuf,"%s",ERR_MEM); errExit(tempBuf); exit(ERR_EXIT); }\r
+ }\r
+ else\r
+ {\r
+ defELEM *tmp;\r
+\r
+ for(tmp=rootDef;tmp;tmp=tmp->next)\r
+ {\r
+ if(!strcmp(tmp->defId,ytext))\r
+ { sprintf(tempBuf,"%s",ERR_ALR); errExit(tempBuf); exit(ERR_EXIT); }\r
+ }\r
+\r
+ if(!(curDef->next=(defELEM *) malloc(sizeof(defELEM))))\r
+ { sprintf(tempBuf,"%s",ERR_MEM); errExit(tempBuf); exit(ERR_EXIT); }\r
+\r
+ curDef=curDef->next;\r
+ }\r
+\r
+ curDef->next=NULL;\r
+ curDef->defTxt=NULL;\r
+\r
+ if(!(curDef->defId=(char *) malloc(strlen(ytext)+1)))\r
+ { sprintf(tempBuf,"%s",ERR_MEM); errExit(tempBuf); exit(ERR_EXIT); }\r
+\r
+ strcpy(curDef->defId,ytext);\r
+\r
+ return;\r
+}\r
+\r
+/* ************************************************************************* */\r
+\r
+/* rimozione di ID come espansione */\r
+void delDefId(char *ytext)\r
+{\r
+ defELEM *tmp,*prevTmp;\r
+\r
+ for(prevTmp=NULL,tmp=rootDef;tmp;prevTmp=tmp,tmp=tmp->next)\r
+ {\r
+ if(!strcmp(tmp->defId,ytext))\r
+ {\r
+ if(prevTmp)\r
+ {\r
+ prevTmp->next=tmp->next;\r
+\r
+ free(tmp->defId);\r
+ if(tmp->defTxt)\r
+ { free(tmp->defTxt); }\r
+ free(tmp);\r
+ }\r
+ else\r
+ {\r
+ rootDef=tmp->next;\r
+\r
+ free(tmp->defId);\r
+ if(tmp->defTxt)\r
+ { free(tmp->defTxt); }\r
+ free(tmp);\r
+ }\r
+\r
+ break;\r
+ }\r
+ }\r
+\r
+ return;\r
+}\r
+\r
+/* ************************************************************************* */\r
+\r
+/* definizione del testo dell'espansione di ID */\r
+void addDefTxt(char *ytext)\r
+{\r
+ if(!(curDef->defTxt=(char *) malloc(strlen(ytext)+1)))\r
+ { sprintf(tempBuf,"%s",ERR_MEM); errExit(tempBuf); exit(ERR_EXIT); }\r
+\r
+ strcpy(curDef->defTxt,ytext);\r
+\r
+ return;\r
+}\r
+\r
+/* ************************************************************************* */\r
+\r
+/* formattazione e aggiunta di yytext alla lista dei token */\r
+void addOkBuf(char *token,char *ytext)\r
+{\r
+ if(expOnly)\r
+ { printf("%s",ytext); }\r
+ else\r
+ {\r
+ sprintf(tempBuf,"%s:%d,%d\t%s\t%s\t%c",inName[inNesting],inLine[inNesting],inRow[inNesting],token,ytext,SEP);\r
+\r
+ if(!rootElem)\r
+ {\r
+ if(!(curElem=rootElem=(listELEM *) malloc(sizeof(listELEM))))\r
+ { sprintf(tempBuf,"%s",ERR_MEM); errExit(tempBuf); exit(ERR_EXIT); }\r
+ }\r
+ else\r
+ {\r
+ if(!(curElem->next=(listELEM *) malloc(sizeof(listELEM))))\r
+ { sprintf(tempBuf,"%s",ERR_MEM); errExit(tempBuf); exit(ERR_EXIT); }\r
+\r
+ curElem=curElem->next;\r
+ }\r
+\r
+ curElem->next=NULL;\r
+\r
+ if(!(curElem->scanTxt=(char *) malloc(strlen(tempBuf)+1)))\r
+ { sprintf(tempBuf,"%s",ERR_MEM); errExit(tempBuf); exit(ERR_EXIT); }\r
+\r
+ strcpy(curElem->scanTxt,tempBuf);\r
+ }\r
+\r
+ return;\r
+}\r
+\r
+/* ************************************************************************* */\r
+\r
+void usage(char *name)\r
+{\r
+ printf("\nC Scanner - Cosimo Oliboni\n\n");\r
+ printf("\"%s -e source\" preprocessing di un sorgente\n",name);\r
+ printf("\"%s source\" scanning di un sorgente\n",name);\r
+ printf("\"%s\" scanning da stdin\n\n",name);\r
+\r
+ return;\r
+}\r
+\r
+/* ************************************************************************* */\r
+\r
+int main(int argc, char **argv)\r
+{\r
+ inLine[inNesting]=0;\r
+ inRow[inNesting]=0;\r
+\r
+ if(argc>1)\r
+ {\r
+ /* "scanner -e sorgente" */\r
+ if(argc==3)\r
+ {\r
+ if(strcmp(argv[1],EXP_OPT))\r
+ { sprintf(tempBuf,"%s",ERR_OPT); errExit(tempBuf); usage(argv[0]); exit(ERR_EXIT); }\r
+\r
+ expOnly=1;\r
+\r
+ strcpy(inName[inNesting],argv[2]);\r
+ }\r
+ /* "scanner sorgente" */\r
+ else if(argc==2)\r
+ { strcpy(inName[inNesting],argv[1]); }\r
+ else\r
+ { sprintf(tempBuf,"%s",ERR_ARG); errExit(tempBuf); usage(argv[0]); exit(ERR_EXIT); }\r
+\r
+ if(!(yyin=fopen(inName[inNesting],"r")))\r
+ { sprintf(tempBuf,"%s(%s)",ERR_OPEN,inName[inNesting]); errExit(tempBuf); usage(argv[0]); exit(ERR_EXIT); }\r
+ }\r
+ /* "scanner <stdin>" */\r
+ else\r
+ { strcpy(inName[inNesting],"stdin"); }\r
+\r
+ inLine[inNesting]=1;\r
+ inRow[inNesting]=1;\r
+\r
+ /* scanning */\r
+ yylex();\r
+\r
+ /* se non ci sono stati errori && se non si effettua solo preprocessing */\r
+ /* output della lista di token creata */\r
+ /* preceduta da OK && seguita da EOF */\r
+\r
+ if(!expOnly)\r
+ {\r
+ inNesting++;\r
+\r
+ addOkBuf("EOF","EOF");\r
+\r
+ printf("%s:0,0\tOK\tOK\t%c",inName[0],SEP);\r
+\r
+ for(curElem=rootElem;curElem;curElem=curElem->next)\r
+ { printf("%s",curElem->scanTxt); }\r
+ }\r
+\r
+ return(OK_EXIT);\r
+}\r
+\r
+yywrap()\r
+{\r
+ return(1);\r
+}\r