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 */
};
Title | # Comments | Views | Activity |
---|---|---|---|
Using Grep to Find a file | 8 | 73 | |
count7 challenge | 12 | 70 | |
Access Required to Read Linux Security Settings and User Permissions | 13 | 88 | |
looking for unix program to search some keywords in side EAR WAR and JAR files | 10 | 52 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
8 Experts available now in Live!