Hi to all!
I have made a yacc program example.y
Code:
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* #include "parser.h" */
%}
%start program
%union {
char *string;
int integer;
}
%token <string> ID
%token <integer> NUMBER
%token <string> 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() {
}
Well 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
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:
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: <integer> != <>
hello.y:62.18-30: warning: type clash on default action: <integer> != <>
hello.y:63.18-30: warning: type clash on default action: <integer> != <>
hello.y:67.18-30: warning: type clash on default action: <integer> != <>
hello.y:68.18-30: warning: type clash on default action: <integer> != <>
hello.y:69.18-31: warning: type clash on default action: <integer> != <>
hello.y:70.18-30: warning: type clash on default action: <integer> != <>
hello.y:71.18-31: warning: type clash on default action: <integer> != <>
hello.y:72.18-31: warning: type clash on default action: <integer> != <>
hello.y:73.18-31: warning: type clash on default action: <integer> != <>
hello.y:74.18-31: warning: type clash on default action: <integer> != <>
hello.y:75.18-31: warning: type clash on default action: <integer> != <>
I would be glad if someone could help me!
Thanks in advance!!!