Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

YACC PROBLEM - % type

jen__jen__ Member Posts: 1
[u]Good morning all!

I would like your help on yacc program I have made!
[/u]
%{

#include
#include
#include
/* #include "parser.h" */

%}

%start program

%union {
char *string;
int integer;
}


%token ID
%token NUMBER
%token STRING
%token IF ELSE WHILE FOR
%token FUNCTION RETURN
%token TRUE FALSE NIL AND NOT OR

%left '='
%left "||"
%left "&&"
%nonassoc "==" "!="
%nonassoc '>' ">=" '<' "<="
%left '+' '-'
%left '*' '/' '%'
%right '!' "++" "--" UMINUS
%left '.'

%%

program: statements
;

statements: statements stmt
|
;

stmt:
';'
|expr ';'
|whilestmt
|forstmt
|returnstmt
|block
|funcdef
|ifstmt
;

expression:
assignexpr
|expr '+' expr { $$ = $1 + $3;} /* HERE IS THE PROBLEM *
/
|expr '-' expr
|expr '*' expr
|expr '/' expr /*{ if($3==0)
yyerror(.divide 0.);
else
$$ = $1 / $3;}
*/
|expr '%' expr
|expr '>' expr
|expr ">=" expr
|expr '<' expr
|expr "<=" expr
|expr "==" expr
|expr "!=" expr
|expr "&&" expr
|expr "||" expr
;


expr: expression
;
%%


int main() {

}



[u]But it does not work!
In the example.y file we have the terminal symbols and non-terminal symbols.

The terminal symbols I use are NUMBER,ID,STRING.
I have defined a type for them
e.g. ID has a string value, NUMBER has an integer value etc[/u]
[u]Well I'm confused as far as the % type is concerned.
We have to give a type to non terminals too.


If we do yacc example.y we take the output:[/u]


hello.y:58.36-37: $$ of `expression' has no declared type
hello.y:58.41-42: $1 of `expression' has no declared type
hello.y:58.46-47: $3 of `expression' has no declared type

This happens cause expression which is a non-terminal symbol has no type!

What type can this symbol take?shall i define a struct for this?

If i do : %type <integer> expression i take :

hello.y:61.18-30: warning: type clash on default action: != <>
hello.y:62.18-30: warning: type clash on default action: != <>
hello.y:63.18-30: warning: type clash on default action: != <>
hello.y:67.18-30: warning: type clash on default action: != <>
hello.y:68.18-30: warning: type clash on default action: != <>
hello.y:69.18-31: warning: type clash on default action: != <>
hello.y:70.18-30: warning: type clash on default action: != <>
hello.y:71.18-31: warning: type clash on default action: != <>
hello.y:72.18-31: warning: type clash on default action: != <>
hello.y:73.18-31: warning: type clash on default action: != <>
hello.y:74.18-31: warning: type clash on default action: != <>
hello.y:75.18-31: warning: type clash on default action: != <>


I would be really grateful if there is someone out there who can help me!

Thank you!

Have a nice day

Jenny
Sign In or Register to comment.