typedef struct Type{
int type;
} Type;
%union {
Type typ;
}
%token <typ> expr
// and then for example in the scanner :
[0-9]+ { yylval.typ.type = INTEGER; return expr; }
expr : '-' expr %prec UNARY {$$ = -$2}
| '!' expr %prec UNARY {$$ = !$2 }
| expr '+' expr {
if ($1.type != INTEGER || $3.type != INTEGER)
{
fprintf(stderr, "Invalid type for addition, must be an integer\n");
}
else
$$.type = INTEGER;
}
| expr '-' expr {
if ($1.type != INTEGER || $3.type != INTEGER)
{
fprintf(stderr, "Invalid type for substraction, must be an integer\n");
}
else
$$.type = INTEGER;
}
| expr '*' expr {
if ($1.type != INTEGER || $3.type != INTEGER)
{
fprintf(stderr, "Invalid type for multiplication, must be an integer\n");
}
else
$$.type = INTEGER;
}
| expr '/' expr {
if ($1.type != INTEGER || $3.type != INTEGER)
{
fprintf(stderr, "Invalid type for division, must be an integer\n");
}
else
$$.type = INTEGER;
}
| expr EQ expr
| expr NE expr
| expr LE expr
| expr '<' expr
| expr GE expr
| expr '>' expr
| expr AND_OP expr
| expr OR_OP expr
| ID opt_idexpr
| '(' expr ')'
| '(' error {fprintf(stderr, "Missing expression after '(' \n");}
| INTCON {
$1.type = INTEGER;
}
| CHARCON {
$1.type = CHARMARK;
}
| STRINGCON {
$1.type = STRINGMARK;
}
;
typedef struct Type{
int type;
} Type;
%union {
Type typ;
}
%token <typ> expr
// and then for example in the scanner :
[0-9]+ { yylval.typ.type = INTEGER; return expr; }
%union
{
char* stringcon;
char charcon;
char *id;
struct Type{
int type;
} typ
}
%union
{
char* stringcon;
char charcon;
char *id;
struct Type{
int type;
} typ
}
%type <typ> expr
expr : '-' expr %prec UNARY {$$=$2;}
| '!' expr %prec UNARY {$$=!$2;}
| expr '+' expr {
if ($1.type != INTEGER || $3.type != INTEGER)
{
fprintf(stderr, "Invalid type for addition, must be an integer\n");
}
else
$$.type = INTEGER;
}
| expr '-' expr {
if ($1.type != INTEGER || $3.type != INTEGER)
{
fprintf(stderr, "Invalid type for substraction, must be an integer\n");
}
else
$$.type = INTEGER;
}
parse.y:183: error: wrong type argument to unary minus
parse.y:184: error: wrong type argument to unary exclamation mark
parse.y:185: error: invalid operands to binary + (have âstruct Typeâ and âstruct Typeâ)
parse.y:193: error: invalid operands to binary - (have âstruct Typeâ and âstruct Typeâ)
parse.y:201: error: invalid operands to binary * (have âstruct Typeâ and âstruct Typeâ)
parse.y:209: error: invalid operands to binary / (have âstruct Typeâ and âstruct Typeâ)
parse.y:217: error: invalid operands to binary == (have âstruct Typeâ and âstruct Typeâ)
parse.y:225: error: invalid operands to binary != (have âstruct Typeâ and âstruct Typeâ)
parse.y:233: error: invalid operands to binary <= (have âstruct Typeâ and âstruct Typeâ)
parse.y:241: error: invalid operands to binary < (have âstruct Typeâ and âstruct Typeâ)
parse.y:249: error: invalid operands to binary >= (have âstruct Typeâ and âstruct Typeâ)
parse.y:257: error: invalid operands to binary > (have âstruct Typeâ and âstruct Typeâ)
parse.y:265: error: used struct type value where scalar is required
parse.y:273: error: used struct type value where scalar is required
typedef struct Data {
int type;
union {
float f;
char *str;
int v;
} d;
} Data;
%}
%union
{
Data *typ;
char* stringcon;
char charcon;
char *id;
}
%type <typ> expr
typedef struct Type{
int type;
union {
char* id;
char *str;
int val;
} value;
} Type;
%} ==> notice this part here
%union
{
Type typ;
}
%type <typ> expr
struct _symbol {
char id[MAXIDLENGTH]; /* identifier name */
int type; /* INTEGER, REAL, BOOLEAN, NOTYPE */
int class; /* VAR, FUNC, PAR, CONST, TEMP, UNDEF */
int used; /* zero if not in scope */
int level; /* static level */
int offset; /* offset in block */
int nrparms; /* Number of parameters */
int value; /* Value for integer constant */
char *string; /* Value for string constant */
int live; /* one if symbol is live */
int live_on_exit; /* one if symbol is live */
struct _icode *next_use;/* next use of symbol */
int framesize; /* size of a-record for function */
struct _callgraph *callgraphnode; /* Node in callgraph */
struct _location *loc; /* List of locations for symbol */
struct _symbol *nextsym; /* next symbol */
};
typedef struct _symbol *SYMBOL;
#define SNULL (SYMBOL) NULL
struct _expression {
SYMBOL place;
int type; /* boolean or arithmetic */
};
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
replace content of certain columns in a *.csv file by a defined string | 12 | 66 | |
How do disable only TLSv1.0 in Oracle Sun One 7.1 Server | 9 | 73 | |
How to learn Linux? | 10 | 43 | |
ORA-00972: identifier is too long from Unix but not from Windows in SQLPLUS | 17 | 47 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
15 Experts available now in Live!